Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I didnt do that tutorial so mind me if i dont answer directly to your question.</p> <p>Take a look at the <a href="http://guides.rubyonrails.org/form_helpers.html" rel="nofollow">rails guide about form helpers</a> and it explains in details your questions, probably in a more articulate way than i can.</p> <p><code>form_for(path/to/your/controller/action)</code> is a helper method to create HTML form elements with the url path to the POST or GET request. The helper knows if it should be a new record or an update record based on what you are asking to do in your controller action.</p> <p>For example In your controller</p> <pre><code>def new @my_instance_variable = Myobject.new end </code></pre> <p>In your view new.html.erb</p> <pre><code>&lt;%= form_for @my_instance_variable do |f| %&gt; ... &lt;% end %&gt; </code></pre> <p>In your case the logic was directly written in the helper and you could also directly write</p> <pre><code>&lt;%= form_for Myobject.new %&gt; </code></pre> <p>Both will result with the following html</p> <pre><code>&lt;form action="/myobjects/new" method="post"&gt; # in this case rails knows its a `POST` request because the route new action # is by default a POST request. You can check these routes and their request # by using `rake routes` in terminal. </code></pre> <p>Then the <code>hidden_field</code> is another helper to contain a value, in your case the <code>@user.id</code> that will be passed as parameter then saved as a Create or update action for the given object. The reason it doesnt add the value in the hidden field tag is because you already have a model association that knows the id of user since the link of form uses the build method with user id.</p> <p>Last part you need to understand the form_for link logic </p> <pre><code>current_user.relationships # implies the association of the current_user has many relationships current_user.relationships.build # .build is a method to populate a new object that can be save as a new record # means you will create a new relationship record by populating the user_id # column with the current_user.id and the followed_id with the target @user.id </code></pre>
 

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