Hi, im stuck or perhaps its late and I need a rest... Going over chapter 11 of Hartl's tutorial and am puzzled at a short form he is rendering. Here is the set up..
Model: relationship table (id, follower_id, followed_id)
This is the form he is rendering:
<%= form_for(current_user.relationships.build(followed_id: @user.id)) do |f| %>
<div><%= f.hidden_field :followed_id %></div>
<%= f.submit "Follow", class: "btn btn-large btn-primary" %>
<% end %>
I thought I was getting comfortable with form_for but this puzzled me. Inside the form_for he is building on object and I think he is assigning the followed_id parameter with the value of @user.id. Assuming @user.id = 44, my thinking is don't we need to also fill a value for the follower_id ??? So for fun I ran the following ruby command in the console..
current_user.relationships.build(followed_id: current_user.id) (again I first created current_user and his id is 44...)
The output after this command is:
=> #<Relationship id: nil, follower_id: 44, followed_id: 44, created_at: nil, updated_at: nil>
ok, so it created an object (not sure if that's what you call it, im a java guy...) of type relationship and im assuming this is how rails now knows that the submit button will be a "create" method in the relationships_controller as that is what happens.
But I noticed that the result also assigned 44 to the follower_id ??? Where did that come from? I would have expected follower_id to be nill just like id is...
Then I have a question on the hidden_field followed_id. When does this :followed_id get its value? Does it get its value when the stuff inside the form_for gets evaluated?
Thanks for you help (I know im close to having at least one light bulb turning on, but need another persons thoughts on this..)