我收到一个错误
Started POST "/actions" for 127.0.0.1 at 2012-10-29 15:04:01 +0600
Processing by ActionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"lfHYF3EQn1/lMReK3alX3NsGa4wSMejC/0fEeoAFYUY=", "commit"=>"Create Action"}
Completed 500 Internal Server Error in 1ms
NoMethodError (undefined method `stringify_keys' for "create":String):
app/controllers/actions_controller.rb:43:in `new'
app/controllers/actions_controller.rb:43:in `create'
此代码完全由 Rails 生成,我不明白为什么它不起作用
这是模型
class Action < ActiveRecord::Base
attr_accessible :name, :user_id
end
这是控制器部分
# GET /actions/new
# GET /actions/new.json
def new
@action = Action.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @action }
end
end
def create
@action = Action.new(params[:action]) # here we get an error !
respond_to do |format|
if @action.save
format.html { redirect_to @action, notice: 'Action was successfully created.' }
format.json { render json: @action, status: :created, location: @action }
else
format.html { render action: "new" }
format.json { render json: @action.errors, status: :unprocessable_entity }
end
end
end
这是表单代码
<%= form_for(@action) do |f| %>
<% if @action.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@action.errors.count, "error") %> prohibited this action from being saved:</h2>
<ul>
<% @action.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :user_id %><br />
<%= f.number_field :user_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
当我按帖子时,萤火虫显示此请求参数
utf8:✓
authenticity_token:lfHYF3EQn1/lMReK3alX3NsGa4wSMejC/0fEeoAFYUY=
action[name]:sdf
action[user_id]:23
commit:Create Action
如果我理解正确,这必须有效,并且 Rails 必须将 action[name] 和 action[user_id] 转换为哈希表并将其放入参数 [:action],然后 Action.new 将此哈希表作为参数。怎么了 ?