Note that there are some explanatory texts on larger screens.

plurals
  1. POhow form_for works in Ruby on Rails
    primarykey
    data
    text
    <p>I am an newbie. I have read the API documentation. But still don't understand how form_for works.</p> <p>Firstly, from Ruby on Rails Tutorial, the form for follow button:</p> <pre><code>&lt;%= form_for(current_user.relationships.build(followed_id: @user.id)) do |f| %&gt; &lt;div&gt;&lt;%= f.hidden_field :followed_id %&gt;&lt;/div&gt; &lt;%= f.submit "Follow", class: "btn btn-large btn-primary" %&gt; &lt;% end %&gt; </code></pre> <p>I understand <code>current_user.relationships.build(followed_id: @user.id)</code> means a new record. But why can we not just submit and trigger controller to save the record without hidden_field? Why do we still need to post followed_id to controller?</p> <p>Secondly, in hidden_field, what does <code>:followed_id</code> means? I believe that is a symbol, i.e. it equals only "followed_id" not a variable of id. If that is only the name of the input field, then what is its value?</p> <p>Thirdly, how does <code>form_for</code> know where the submission should be sent to? Which controller and action the <code>form_for</code> will post to?</p> <p>Fourth, how does <code>params</code> work with <code>form_for</code>? In this follow button case, <code>params[:relationship][:followed_id]</code> will return <code>@user.id</code> in controller. How does it know the first hash attribute is <code>:relationship</code>? We have neither mentioned <code>form_for :relationship</code> nor <code>form_for @relationship</code>.</p> <p>I know these questions can be very dumb, but I am really stuck. Any help will be appreciated. </p>
    singulars
    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.
 

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