125

I want to have a form_for @user, but post to a custom action in the users controller.

How can I do this?

sscirrus
  • 55,407
  • 41
  • 135
  • 228
Blankman
  • 259,732
  • 324
  • 769
  • 1,199

6 Answers6

181
form_for @user, :url => url_for(:controller => 'mycontroller', :action => 'myaction')

or

form_for @user, :url => whatever_path
Austin
  • 3,860
  • 3
  • 23
  • 25
  • 15
    I dont think `url_for` is necessary. Also since controller is the same, you could use `form_for @user, :url => :action => 'myaction'` – rubyprince Mar 16 '11 at 03:32
  • 1
    Above answer was helpful but I had to tweak it a little as otherwise the POST was always routing to the edit controller action otherwise: <%= form_for(@user, url: {action: "myaction"}, method: :post) do |user_form| %>. Please note that this is for a view file within the views/users/ folder. – AarCee Aug 31 '15 at 15:00
  • it saved me on 2022. thanks – Ezio Auditore Jan 22 '23 at 23:26
42

The following works for me:

form_for @user, :url => {:action => "YourActionName"}
Peter Lee
  • 12,931
  • 11
  • 73
  • 100
  • 1
    This works. Check it out in the official docs by searching for `action:` (*action colon*) http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for – user664833 Feb 20 '14 at 23:12
  • 3
    Hmm not sure. I have put `form_for @user, url: {action: 'myaction'}` but it gives me `No route matches {:action=>"myaction", :controller=>"users"}` error. – lulalala Jun 26 '15 at 01:57
  • @lulalala, of course, you must set routes as well. in this case, like: `resources :users do` `collection do` `get :myaction` `end` `end` – tagaism Apr 05 '19 at 11:57
11

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.

4

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| -%>
Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
4

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

juliangonzalez
  • 4,231
  • 2
  • 37
  • 47
  • This it true, but the question asks to about a particular object - in this case `@user`. Using a `form_tag` will not bind the object's attributes to the form fields. – Dylan Pierce Nov 30 '17 at 17:44
3

new syntax

<%= form_for :user, url: custom_user_path, method: :post do |f|%>
<%end%>
gsumk
  • 809
  • 10
  • 15