私はアクションを変更する基本的なメンバーを書いています。私はDRY状態にしてparamsハッシュを変更してからupdate
メソッドに渡しますが、動作しないようです。私は見つけることができないいくつかのレール魔法があると思う...私はこれを読んだことからうまくいくはずです。私はRails 3.2を使用しています。ここでレール3の別のアクションから更新アクションを呼び出すにはどうすればよいですか?
は私がやろうとしているものの例です:
# 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配列権が設定されているようなので、それはそう。これは、通常のshow
ビューをフラッシュメッセージ "タスクが正常に更新されました。"でレンダリングするので、メソッド全体が実行されたように見えますが、モデルのプロパティのどれも変更されていないようです。私はupdate_attributes内の何かが失敗していると思います。誰も私のためにこれについていくつかの光を当てることができますか?
また、これは狂ったことですか?更新するために連鎖するのではなく、私のtoggle_doneメソッドの中で設定して保存するべきですか?
Doh。明らかに十分な注意を払っていない。私は私の質問がまだ立っていると思う、それを正当な "レールの道"ですか? – Soup
私はあなたのようにやっていると思いますが、私の答えで指摘したように、必要なデータベース要求が増えています。 – klump
それでは、DRY方法は、一般的なコードを自分のメソッドに入れて、代わりにそれらを呼び出すことです。 – klump