Ruby on rails 如何从rails 3中的另一个操作调用更新操作?

Ruby on rails 如何从rails 3中的另一个操作调用更新操作?,ruby-on-rails,Ruby On Rails,因此,我正在编写一个基本的成员修改操作,我想,让我们保持冷静,只修改params散列,然后传递给我们的更新方法,但它似乎不起作用。我想有一些rails的魔力,我找不到。。。从我读到的来看,这应该行得通。我使用的是Rails 3.2 下面是我尝试做的一个例子: # POST /tasks/1/toggle_done def toggle_done @task = Task.find(params[:id]) puts "<<<<<", para

因此,我正在编写一个基本的成员修改操作,我想,让我们保持冷静,只修改params散列,然后传递给我们的更新方法,但它似乎不起作用。我想有一些rails的魔力,我找不到。。。从我读到的来看,这应该行得通。我使用的是Rails 3.2

下面是我尝试做的一个例子:

  # 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
我得到以下控制台输出:

<<<<<
{"_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."}
看来params数组设置正确了。它在flash消息任务已成功更新的情况下呈现常规的show视图,因此似乎整个方法都已执行,但没有任何模型属性发生更改。我猜update_属性中的某些东西失败了。有人能帮我解释一下吗


这也是一件疯狂的事情吗?我是否应该在toggle\u done方法中设置并保存,而不是链接到update?

您正在传递参数[:task]以更新不存在的属性。尝试:

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

您正在传递参数[:task]以更新不存在的属性。尝试:

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

Rails将任务对象的属性保存在哈希参数[:task]中。因此,在toggle_done方法中,需要将结果保存在params[:task][:done]中,否则rails无法将done属性与任务关联

def toggle_done
  @task = Task.find(params[:id])
  params[:task] = { done: !(@task.done) }
  update
end
但是,通过调用update方法,您可以进行3次数据库查询,其中只有2次是必需的,前2次是相同的,因为您在toggle_done方法和update中都使用ID加载任务

为了避免这种情况,可以将save和redirect部分放入受保护的方法中,并在需要保存时调用它。像这样:

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

Rails将任务对象的属性保存在哈希参数[:task]中。因此,在toggle_done方法中,需要将结果保存在params[:task][:done]中,否则rails无法将done属性与任务关联

def toggle_done
  @task = Task.find(params[:id])
  params[:task] = { done: !(@task.done) }
  update
end
但是,通过调用update方法,您可以进行3次数据库查询,其中只有2次是必需的,前2次是相同的,因为您在toggle_done方法和update中都使用ID加载任务

为了避免这种情况,可以将save和redirect部分放入受保护的方法中,并在需要保存时调用它。像这样:

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

Doh。显然没有引起足够的注意。我想我的问题仍然存在,这是rails合法的方法吗?我想这只是像你那样做,但正如我在回答中指出的那样,你需要做更多的数据库请求。所以rails干法的方法是将公共代码放入自己的方法中,并调用这些方法…Doh。显然没有引起足够的注意。我想我的问题仍然存在,这是一种合法的rails方法吗?我想这只是像你那样做,但正如我在回答中指出的那样,你需要做更多的数据库请求。所以rails的干法方法是将公共代码放进自己的方法中,然后调用这些方法。。。