form_for but to post to a different action
I want to have a form_for @user
, but post to a custom action in the users controller.
How can I do this?
form_for @user, :url => url_for(:controller => 'mycontroller', :action => 'myaction')
or
form_for @user, :url => whatever_path
The following works for me:
form_for @user, :url => {:action => "YourActionName"}
I have done it like that
<%= form_for :user, url: {action: "update", params: {id: @user.id}} do |f| %>
Note the optional parameter id
set to user instance id attribute.
Alternatively the same can be reached using form_tag
with the syntax:
form_tag({controller: "people", action: "search"}, method: "get", class: "nifty_form")
# => '<form accept-charset="UTF-8" action="/people/search" method="get" class="nifty_form">'
As described in http://guides.rubyonrails.org/form_helpers.html#multiple-hashes-in-form-helper-calls