Jump to content

The ultimate community for Ruby on Rails developers.


question on Hartl's tutorial form_for(....)

tutorial rails form_for

  • Please log in to reply
2 replies to this topic

#1 jschrock1



  • Members
  • 48 posts
  • LocationIndianapolis

Posted 05 November 2013 - 02:13 AM

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..)





  • james likes this

#2 stevieing



  • Members
  • 40 posts

Posted 05 November 2013 - 08:59 AM   Best Answer

These are good questions. There is a bit of Rails magic going on.


The User model has a has_many association with the Relationships model.


The build method automatically assigns the user_id of the parent object.


The id of the Relationship object will be assigned by the save action.


The followed id from the hidden field is taken from the object you have just created. The :followed_id symbol is synonymous with the followed_id of the current object.


Hope that makes sense



  • james likes this

#3 jschrock1



  • Members
  • 48 posts
  • LocationIndianapolis

Posted 05 November 2013 - 01:04 PM

Ah thank you...  that's what the output is showing..  although since its a relationship type of model, one would seldom assign (especially in this case) the same id for follower_id and followed_id, I like most of rails magic but that one is a bit odd...    Also ok on the hidden field, although we do have a number of variables created that are shared (globally, yuck) and one of those variables is @user which was created just before the page in question was rendered.  I would think (perhaps wrongly) that the create method of relationships could simply use that variable (@user) rather than using a hidden field.  (I think it has access to it...)  Not sure, I need to soak that up a bit, but that's my thinking..  The @user.id and the id of what will be in the hidden field should be the same..  I'll fiddle with this tonight..  Anyway, rambling on here, thanks for your help!!!

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users