form_for instance model help; move logic from form to controller

13 views
Skip to first unread message

dasibre

unread,
Jun 4, 2013, 11:31:07 AM6/4/13
to rubyonra...@googlegroups.com
I have this form, everything works fine, but I would like to move the current_user.relationships.build logic to the controller.  
<%= 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 %>

Controller Relationships create method
def create
    @user = User.find(params[:relationship][:followed_id])
    current_user.follow!(@user)
    redirect_to @user
end


and...@benjamin.dk

unread,
Jun 9, 2013, 4:39:33 PM6/9/13
to rubyonra...@googlegroups.com
so if in your action you do 

@relationship = current_user.relationships.build(followed_id: @user.id)

you should have a variable called relationship on your view:

<%= form_for(relationship) do |f| %>
  <div><%= f.hidden_field :followed_id %></div>
  <%= f.submit "Follow", class: "btn btn-large btn-primary" %>
<% end %>
Reply all
Reply to author
Forward
0 new messages