2

控制器:@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"}
4

2 回答 2

9

您必须执行以下任一操作

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

或者

<%= form_for :micropost, :url=>{ :controller => :microposts, :action => :create}, :remote => true do |f| %>
    <%= f.text_area :content, "", :size=> "20x2" %>
<% end %>
于 2012-08-20T12:06:17.280 回答
2

你必须避免混合form_forinput_tag.

当您声明 aform_for @an_object do |form|时,最佳做法是使用form.text_area :contentwhen:content是您的@an_object.

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

于 2012-08-20T12:15:38.240 回答