开发者

Item attributes not being saved on update

I am building a simple task management app and use a checkbox to indicate whether a task is done.

Here is code I came up with:

<%= check_box_tag "id", "id", task.done, 
    :onclick => remote_function(
      :update => "task", 
      :url => { :action => :update, :controller => :tasks, :id => task.id }, 
      :with => "'task[done]=true'", 
      :complete => "alert('hi')"  ) %>
开发者_如何学Go

It does print the checkbox, and it does check it automatically depending on the status of task.done. But when I fire the onclick and watch the logs, the update method gets called, but the done information is not updated on the task.

Here is the code for update on the task controller:

  def update
    @task = Task.find(params[:id])
    @stream = Stream.find(@task.stream_id)

    respond_to do |format|
  if @task.update_attributes(params[:task])
    format.html { redirect_to(@stream, :notice => 'Task was successfully updated.') }
    format.json { head :ok }
  else
    format.html { render :action => "edit" }
    format.json  { render :json => @task.errors, :status => :unprocessable_entity }


     end
    end
  end

Why is the done=true not being updated on the task record?

Thanks a lot guys


2 things i would check :

1) Do you have attr_accessible values on the model ? If so, make sure that that value you want to change is included.

2) Do you pass your validations with that update ? You can try save! as a test, to see whether you get back an explicit rails validations error.


An update for those using Rails 4: rather than looking for attr_accessible, look for the params.require items in the controller. Caught me out!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜