Custom name for params hash from Rails form_for

心已入冬 提交于 2019-12-21 03:10:28

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/11248670/custom-name-for-params-hash-from-rails-form-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!