Additional Parameter in form_for in Rails

10,922

Solution 1

<%= 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| %>

Solution 2

This works for me:

<%= form_for @foo, :url => foo_path(:type => "whatever"), :html => {:method => :post} do |f| %>
Share:
10,922
bertolami
Author by

bertolami

Software Engineer at Zuehlke Engineering AG in Bern

Updated on June 19, 2022

Comments

  • bertolami
    bertolami almost 2 years

    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.