Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's how you can pass a parameter around via the link_to method in order to, say, go to another form in order to create a new object with the passed parameter. This strategy would allow you to pass variables among actions in your controller and create objects with predefined attributes:</p> <p>Say you have a variable called <em>@foo</em> that you want to pass to your <em>new</em> controller action, and your <em>new</em> view contains a form for your user to fill out. In which case, you can use</p> <pre><code>&lt;%= link_to "Link Text", new_widget_path(:foo =&gt; @foo) %&gt; </code></pre> <p>which would store @foo in params[:foo], allowing you to use params[:foo] in your controller.</p> <p>Clicking on <em>Link Text</em> will direct Rails to the <em>new</em> action of your WidgetController. You can have</p> <pre><code>def new @widget = Widget.new(:foo =&gt; params[:foo]) end </code></pre> <p>which would define a new Widget object with the :foo attribute pre-filled with params[:foo].</p> <p>Then, in your <em>new.html.erb</em> view file, you can allow the user to create a new Widget object with this pre-defined <em>foo</em> attribute already filled out via a hidden form field:</p> <pre><code> &lt;%= form_for(@widget) do |f| %&gt; &lt;%= f.label :other_attribute %&gt;&lt;br /&gt; &lt;%= f.text_field :other_attribute %&gt; &lt;%= f.hidden_field :foo %&gt; &lt;%= f.submit %&gt; &lt;% end %&gt; </code></pre> <p>Allowing the user to create a new Widget object with a custom other_attribute and the <em>foo</em> attribute already filled out!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload