2011-05-06 28 views
7

是否可以在rails中的表單數據之外提交另一個參數?我的問題是,我爲不同的類呈現不同的表單並將它們發送到相同的創建方法。我想發送帶有表單的類(作爲值不是散列中的鍵)。 有點像:類型參數(實際上不工作)Rails中form_for中的其他參數

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

到請求消息是這樣的:

{"commit"=>"Create Class of an Object", 
"authenticity_token"=>"/iqu0A8/AocDT3HyjL5/+bKZiLkyr4FE71u/mc8Wx0Y=", 
"utf8"=>"âœ「", 
"class_of_an_object"=>{"name"=>"a name", 
"description"=>"a description"}} 

,我將有一個"type" => "class_of_an_object",而是直接在哈希的內不「class_of_an_object」散列。

+0

你想在URL中發來的GET參數或身體參數一個POST參數? – 2011-05-06 18:23:07

回答

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

,我更喜歡使用命名路由

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

這個工作對我來說:

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