Optional parameter in form_for in Rails

Is it possible to imagine another parameter outside the form data in rails? My problem is that I process different forms for different classes and send them to the same creation method. I would like to send a class with a form (as a value not as a key in a hash). Something like type: parameter (actually doesn't work)

<%= form_for(@an_object, :url => { :controller => :a_controller, :action => :create }, :type => @an_object.class.to_s.underscore) do |f| %> 

The message will look like this:

 {"commit"=>"Create Class of an Object", "authenticity_token"=>"/iqu0A8/AocDT3HyjL5/+bKZiLkyr4FE71u/mc8Wx0Y=", "utf8"=>"รขล“"", "class_of_an_object"=>{"name"=>"a name", "description"=>"a description"}} 

and I would have "type" => "class_of_an_object" , but directly in the hash, not in the hash of "class_of_an_object".

+7
source share
2 answers
 <%= form_for @an_object, :url => { :controller => :a_controller, :action => :create, :type => @an_object.class.to_s.underscore } do |f| %> 

And I prefer using named routes

 <%= form_for @object, :url => object_path(@object, :type => "whtever"), :html => {:method => :post} do |f| %> 
+13
source

This works for me:

 <%= form_for @foo, :url => foo_path(:type => "whatever"), :html => {:method => :post} do |f| %> 
+4
source

All Articles