I am getting an error
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'
This code is totally generated by Rails, and i do not understand why it doesnt work
Here is model
class Action < ActiveRecord::Base
attr_accessible :name, :user_id
end
Here is controller part
# 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
And here is the form code
<%= 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 %>
When i press post, firebug shows this request parameters
utf8:✓
authenticity_token:lfHYF3EQn1/lMReK3alX3NsGa4wSMejC/0fEeoAFYUY=
action[name]:sdf
action[user_id]:23
commit:Create Action
if i understand correctly this must work and rails must turn action[name] and action[user_id] into hash table and place it into parameters[:action], then Action.new get this hash table as parameter. What is wrong ?
Ah, I know. You have a name clash.
actionis a reserved name. Along withcontroller,idand maybe others. Use another name for your form.When you post to
/actions/create, thenparams[:controller]should be ‘posts’ andparams[:action]should be ‘create’. These params are assigned by Rails.