Ruby on rails form_tag - 将模型参数发送到控制器

时间:2012-08-20 12:02:30

标签: ruby-on-rails ruby ajax ruby-on-rails-3

控制器:@micropost = Micropost.new(params[:micropost])

但是这个form_tag正在向我发送params[:content]而不是params[:micropost][:content]

<%= form_tag( {:controller => :microposts, :action => :create}, :remote => true) do %>

    <%= text_area_tag :content, "", :size=> "20x2" %>
    ...
    ...
    ...
    <%= submit_tag "submit" %>
<% end %>

服务器:

Processing by MicropostsController#create as JS
Parameters: {"utf8"=>"✓", "content"=>"sdfsdf", "commit"=>"submit"}

2 个答案:

答案 0 :(得分:9)

您必须执行以下任一操作

<%= text_area_tag "micropost[content]", "", :size=> "20x2" %>

OR

<%= form_for :micropost, :url=>{ :controller => :microposts, :action => :create}, :remote => true do |f| %>
    <%= f.text_area :content, "", :size=> "20x2" %>
<% end %>

答案 1 :(得分:2)

您必须避免混合form_forinput_tag

当您声明form_for @an_object do |form|时,最佳做法是在form.text_area :content:content的属性时使用@an_object

在这种情况下,你也可以写:text_area_tag "an_object[content]",但它有点脏。