mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
133 lines
8.2 KiB
HTML
133 lines
8.2 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
|
<title>
|
|
File: outside_controller_use
|
|
|
|
— Documentation by YARD 0.8.7.6
|
|
|
|
</title>
|
|
|
|
<link rel="stylesheet" href="css/style.css" type="text/css" charset="utf-8" />
|
|
|
|
<link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8" />
|
|
|
|
<script type="text/javascript" charset="utf-8">
|
|
hasFrames = window.top.frames.main ? true : false;
|
|
relpath = '';
|
|
framesUrl = "frames.html#!file.outside_controller_use.html";
|
|
</script>
|
|
|
|
|
|
<script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
|
|
|
|
<script type="text/javascript" charset="utf-8" src="js/app.js"></script>
|
|
|
|
|
|
</head>
|
|
<body>
|
|
<div id="header">
|
|
<div id="menu">
|
|
|
|
<a href="_index.html">Index</a> »
|
|
<span class="title">File: outside_controller_use</span>
|
|
|
|
|
|
<div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
|
|
</div>
|
|
|
|
<div id="search">
|
|
|
|
<a class="full_list_link" id="class_list_link"
|
|
href="class_list.html">
|
|
Class List
|
|
</a>
|
|
|
|
<a class="full_list_link" id="method_list_link"
|
|
href="method_list.html">
|
|
Method List
|
|
</a>
|
|
|
|
<a class="full_list_link" id="file_list_link"
|
|
href="file_list.html">
|
|
File List
|
|
</a>
|
|
|
|
</div>
|
|
<div class="clear"></div>
|
|
</div>
|
|
|
|
<iframe id="search_frame"></iframe>
|
|
|
|
<div id="content"><div id='filecontents'>
|
|
<p><a href="../README.md">Back to Guides</a></p>
|
|
|
|
<h2 id="label-Using+ActiveModelSerializers+Outside+Of+A+Controller">Using ActiveModelSerializers Outside Of A Controller</h2>
|
|
|
|
<h3 id="label-Serializing+a+resource">Serializing a resource</h3>
|
|
|
|
<p>In ActiveModelSerializers versions 0.10 or later, serializing resources
|
|
outside of the controller context is fairly simple:</p>
|
|
|
|
<pre class="code ruby"><code class="ruby"><span class='comment'># Create our resource
|
|
</span><span class='id identifier rubyid_post'>post</span> <span class='op'>=</span> <span class='const'>Post</span><span class='period'>.</span><span class='id identifier rubyid_create'>create</span><span class='lparen'>(</span><span class='label'>title:</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>Sample post</span><span class='tstring_end'>"</span></span><span class='comma'>,</span> <span class='label'>body:</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>I love Active Model Serializers!</span><span class='tstring_end'>"</span></span><span class='rparen'>)</span>
|
|
|
|
<span class='comment'># Optional options parameters
|
|
</span><span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span>
|
|
|
|
<span class='comment'># Create a serializable resource instance
|
|
</span><span class='id identifier rubyid_serializable_resource'>serializable_resource</span> <span class='op'>=</span> <span class='const'>ActiveModelSerializers</span><span class='op'>::</span><span class='const'>SerializableResource</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='id identifier rubyid_post'>post</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
|
|
|
|
<span class='comment'># Convert your resource into json
|
|
</span><span class='id identifier rubyid_model_json'>model_json</span> <span class='op'>=</span> <span class='id identifier rubyid_serializable_resource'>serializable_resource</span><span class='period'>.</span><span class='id identifier rubyid_as_json'>as_json</span>
|
|
</code></pre>
|
|
|
|
<h3 id="label-Looking+up+the+Serializer+for+a+Resource">Looking up the Serializer for a Resource</h3>
|
|
|
|
<p>If you want to retrieve a serializer for a specific resource, you can do
|
|
the following:</p>
|
|
|
|
<pre class="code ruby"><code class="ruby"><span class='comment'># Create our resource
|
|
</span><span class='id identifier rubyid_post'>post</span> <span class='op'>=</span> <span class='const'>Post</span><span class='period'>.</span><span class='id identifier rubyid_create'>create</span><span class='lparen'>(</span><span class='label'>title:</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>Another Example</span><span class='tstring_end'>"</span></span><span class='comma'>,</span> <span class='label'>body:</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>So much fun.</span><span class='tstring_end'>"</span></span><span class='rparen'>)</span>
|
|
|
|
<span class='comment'># Optional options parameters
|
|
</span><span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span>
|
|
|
|
<span class='comment'># Retrieve the default serializer for posts
|
|
</span><span class='id identifier rubyid_serializer'>serializer</span> <span class='op'>=</span> <span class='const'>ActiveModel</span><span class='op'>::</span><span class='const'>Serializer</span><span class='period'>.</span><span class='id identifier rubyid_serializer_for'>serializer_for</span><span class='lparen'>(</span><span class='id identifier rubyid_post'>post</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
|
|
</code></pre>
|
|
|
|
<p>You could also retrieve the serializer via:</p>
|
|
|
|
<pre class="code ruby"><code class="ruby"><span class='const'>ActiveModelSerializers</span><span class='op'>::</span><span class='const'>SerializableResource</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='id identifier rubyid_post'>post</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_serializer'>serializer</span>
|
|
</code></pre>
|
|
|
|
<p>Both approaches will return an instance, if any, of the resource's
|
|
serializer.</p>
|
|
|
|
<h2 id="label-Serializing+before+controller+render">Serializing before controller render</h2>
|
|
|
|
<p>At times, you might want to use a serializer without rendering it to the
|
|
view. For those cases, you can create an instance of
|
|
<code>ActiveModelSerializers::SerializableResource</code> with the resource
|
|
you want to be serialized and call <code>.as_json</code>.</p>
|
|
|
|
<pre class="code ruby"><code class="ruby"><span class='kw'>def</span> <span class='id identifier rubyid_create'>create</span>
|
|
<span class='id identifier rubyid_message'>message</span> <span class='op'>=</span> <span class='id identifier rubyid_current_user'>current_user</span><span class='period'>.</span><span class='id identifier rubyid_messages'>messages</span><span class='period'>.</span><span class='id identifier rubyid_create!'>create!</span><span class='lparen'>(</span><span class='id identifier rubyid_message_params'>message_params</span><span class='rparen'>)</span>
|
|
<span class='id identifier rubyid_message_json'>message_json</span> <span class='op'>=</span> <span class='const'>ActiveModelSerializers</span><span class='op'>::</span><span class='const'>SerializableResource</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='id identifier rubyid_message'>message</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_as_json'>as_json</span>
|
|
<span class='const'>MessageCreationWorker</span><span class='period'>.</span><span class='id identifier rubyid_perform'>perform</span><span class='lparen'>(</span><span class='id identifier rubyid_message_json'>message_json</span><span class='rparen'>)</span>
|
|
<span class='id identifier rubyid_head'>head</span> <span class='int'>204</span>
|
|
<span class='kw'>end</span>
|
|
</code></pre>
|
|
</div></div>
|
|
|
|
<div id="footer">
|
|
Generated on Thu Jun 16 09:05:06 2016 by
|
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
|
0.8.7.6 (ruby-2.2.4).
|
|
</div>
|
|
|
|
</body>
|
|
</html> |