How to get the method of the form_for helper in the view?

7 views
Skip to first unread message

Roman Ppppp

unread,
Mar 23, 2016, 2:41:50 PM3/23/16
to rubyonra...@googlegroups.com
I am using a form partial for updating and creating my users.
Like:
<%= form_for @user do |f| %>
Now rails automatically assigns the method post if the form corresponds
with a nonexistent user and the patch method if the user exists and just
needs to be updated.

My problem is that the update form and the new user form are supposed to
be slightly different, therefore I would like to write code like

if form.method.patch?
...do something
else
.... do something else
end

Is there a way to accomplish this?

--
Posted via http://www.ruby-forum.com/.

nanaya

unread,
Mar 23, 2016, 2:48:59 PM3/23/16
to rubyonra...@googlegroups.com
Hi,
`f.object.persisted?` [1] will allow you to determine whether the record
is new or existing.

[1]
http://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-persisted-3F

Roman Ppppp

unread,
Mar 23, 2016, 3:15:52 PM3/23/16
to rubyonra...@googlegroups.com
Thanks, exactly what I was looking for.
Reply all
Reply to author
Forward
0 new messages