Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Additional Parameter in form_for in Rails

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

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

The post message looks like:

{"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 a "type" => "class_of_an_object", but directly in the hash an not within the "class_of_an_object" hash.

like image 439
bertolami Avatar asked May 06 '11 14:05

bertolami


2 Answers

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

And I prefer to use named routes

<%= form_for @object, :url => object_path(@object, :type => "whtever"), :html => {:method => :post} do |f| %>
like image 109
fl00r Avatar answered Nov 12 '22 23:11

fl00r


This works for me:

<%= form_for @foo, :url => foo_path(:type => "whatever"), :html => {:method => :post} do |f| %>
like image 38
Alain Beauvois Avatar answered Nov 13 '22 00:11

Alain Beauvois