简体   繁体   中英

How do I call update action from another action in rails 3?

So I'm writing a basic member modifying action, and I figured, lets stay DRY and just modify the params hash then pass along to our update method but it doesn't seem to work. I guess there is some rails magic going on that I can't find... From what I've read this should work. I'm using Rails 3.2.

Here's an example of what I'm trying to do:

  # POST /tasks/1/toggle_done
  def toggle_done
    @task = Task.find(params[:id])
    puts "<<<<<", params

    # invert done bool value
    params[:done] = !(@task.done)

    # thought maybe update_attributes retured a full set of 
    # attributes in the params...
    #params[:name] = @task.name + "...test."

    # thought maybe the method call to update was getting 
    # filtered or something. Doesn't seem to help.
    #params[:_method] = "put"
    # redirect to update with these new params

    puts ">>>>>", params

    # Why bother rewriting task.done = x; task.save; 
    # redirect_to show; etc when update already does that.
    update
  end

  # PUT /tasks/1
  # PUT /tasks/1.json
  def update
    @task = Task.find(params[:id])

    puts "======", params

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

I get the following console output:

<<<<<
{"_method"=>"post", "authenticity_token"=>"CVqzsJfSVgM7Bq/kXlrjzkWVoA7Pbne4GNEHqbQB42s=", "action"=>"toggle_done", "controller"=>"tasks", "id"=>"1"}
>>>>>
{"_method"=>"put", "authenticity_token"=>"CVqzsJfSVgM7Bq/kXlrjzkWVoA7Pbne4GNEHqbQB42s=", "action"=>"toggle_done", "controller"=>"tasks", "id"=>"1", "done"=>false, "name"=>"Put Done button in index view...test."}
======
{"_method"=>"put", "authenticity_token"=>"CVqzsJfSVgM7Bq/kXlrjzkWVoA7Pbne4GNEHqbQB42s=", "action"=>"toggle_done", "controller"=>"tasks", "id"=>"1", "done"=>false, "name"=>"Put Done button in index view...test."}

So it seems like the params array is set right. It renders the regular show view with the flash message "Task was successfully updated.", so it seems like the whole method gets executed but non of the model properties are getting changed. I guess something inside update_attributes is failing. Can anyone shed some light on this for me?

Also is this a crazy thing to do? Should I be setting and saving inside my toggle_done method instead of chaining to update?

Rails saves the attributes for the task object in the hash params[:task] . So you in your toggle_done method you need to save the result in params[:task][:done] otherwise rails cannot associate the done attribute with the task.

def toggle_done
  @task = Task.find(params[:id])
  params[:task] = { done: !(@task.done) }
  update
end

But with calling the update method you make 3 database queries where only 2 are neccessary - And the first 2 are identically because you load the Task with the ID in the toggle_done method as well as in update .

To avoid this you can put the save and redirect part into a protected method and call it when you want to save it. Like this:

def toggle_done
  @task = Task.find(params[:id])
  params[:task] =  { done: !(@task.done) }
  save_updated
end

def update
  @task = Task.find(params[:id])
  save_updated
end

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

You're passing params[:task] to update_attributes, which doesn't exist. Try:

params[:task] = {:done => !(@task.done)}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM