Reputation: 1332
The task model has just one field : title.
I've made a form to add a new task with one single field : title
But in the create method, we can see that title is filled by "test"
but in the query, we can see "nil" ... any ideas ?
thanks
Started POST "/tasks" for 127.0.0.1 at 2013-01-03 13:16:44 -0500
Processing by TasksController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"iWaK1QX6VCyeUCueLrRNErJEtdm/ZNxg4d3LU0vKjnY=", "task"=>{"title"
=>"test"}, "commit"=>"Add a new task "}
(0.1ms) begin transaction
SQL (0.9ms) INSERT INTO "tasks" ("created_at", "title", "updated_at") VALUES (?, ?, ?) [["created_at", Thu, 03 Jan 2013 18:16:44 UTC +00:00], ["title", nil], ["updated_at", Thu, 03 Jan 2013 18:16:44 UTC +00:00]]
(0.8ms) commit transaction
Redirected to http://0.0.0.0:3000/tasks
Completed 302 Found in 8ms (ActiveRecord: 1.8ms)
here is the create method
def create
@task = Task.new(params[:post])
if @task.save
redirect_to tasks_path, :notice => "Task successfully saved"
else
render "new"
end
end
Upvotes: 1
Views: 262
Reputation: 8122
In your tasks_controller.rb , you must have create method which will handle POST
request and accept parameters which are passed though request .
def create
task = Task.new(params[:task])
task.save
end
Upvotes: -2
Reputation: 14402
The problem is that you are fetching post
instead of task
@task = Task.new(params[:task])
Upvotes: 6
Reputation: 2821
Make sure
attr_accessible :title
is in your Task model (task.rb)
UPDATE: change params[:post] to params[:task]:
@task = Task.new(params[:task])
Upvotes: 0
Reputation: 211560
Make sure your attribute is accessible or you won't be able to mass-assign changes to it:
class Task < ActiveRecord::Base
attr_accessible :title
end
You should have unit tests that properly exercise your models to be sure that they can be updated as you do in the controller. Those will quickly uncover any attributes which have not been correctly flagged.
Rails 2.3 and prior were not strict about this, you could mass-assign anything, but Rails 3 will not assign these attributes unless they are specifically allowed.
Upvotes: 1