I want to have a form_for @user
, but post to a custom action in the users controller.
How can I do this?
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.
If you want to pass custom Controller to a form_for while rendering a partial form you can use this:
<%= render 'form', :locals => {:controller => 'my_controller', :action => 'my_action'}%>
and then in the form partial use this local variable like this:
<%= form_for(:post, :url => url_for(:controller => locals[:controller], :action => locals[:action]), html: {class: ""} ) do |f| -%>
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
new syntax
<%= form_for :user, url: custom_user_path, method: :post do |f|%>
<%end%>