Custom name for params hash from Rails form_for

JellicleCat picture JellicleCat · Jun 28, 2012 · Viewed 13.6k times · Source

Ordinarily, using form_for(@foo) means that on the back end of the form's action, you'll have the form data in params[:foo], but in my case I'd like to have a custom namespace applied to these params, i.e. params[:bar], not params[:foo].

I'm not talking about making the namespace longer by supplying the :namespace argument to the form_for method. To the contrary, my current name is overlong, and I want to shorten it. More importantly, I'm actually swapping a new model in place of an existing one, so the controller is filled with calls to params[:quoter], whereas our new model supplies params[:company_quoter_intf_quoter]. Any ideas?

Specs: Ruby 1.9.3, Rails 3.2.3

Answer

Ylan S picture Ylan S · Jun 28, 2012

What you are looking for is:

form_for(@foo, :as => :bar)

The documentation for this can be found at:

http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html