dasibre
2013-Jun-04 15:31 UTC
form_for instance model help; move logic from form to controller
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
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/213638fe-9758-4107-83d7-c6ee1f20d9b3%40googlegroups.com?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
andreo-FdlSlcb4kYpknbxzx/v8hQ@public.gmane.org
2013-Jun-09 20:39 UTC
Re: form_for instance model help; move logic from form to controller
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 %> On Tuesday, 4 June 2013 17:31:07 UTC+2, dasibre wrote:> > 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 > > > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/97395a3c-65c2-4fd8-81ac-922479a0bab7%40googlegroups.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.