开发者

What is causing this redirect_to to fail?

I am trying to use this redirect_to

redirect_to :controller => :note_categories, :action => :destroy, :note_id => params[:id]

This is the URL that results

http://localhost:3000/note_categories/272?note_id=272

and this is the error message

Unknown action
No action responded to show. Actions: destroy

The reason I am redirecting to the note_categories destroy action, and passing in the note id, is that in the destroy action, I am finding all the note_categories related to note, running some code on them, then destroying them. I k开发者_运维百科now this isn't a great way to be doing this, but I couldn't use :dependant => :destroy because the code I have to run on the note_category before I delete it needs access to current_user, which can't happen in the note_category model.

So yeah, can someone please tell me what am I doing wrong in my redirect_to? Thanks for reading.


The redirect_to method is essentially the Rails implementation of the Post/Redirect/Get (PRG) web design pattern. It's used to prevent duplicate form submissions caused by the user clicking the browser's Refresh button after submitting a form.

The typical Rails usage is like this for creating an object:

  • A form for creating an object is displayed (new action/HTTP GET)
  • The user fills in the form
  • The form is submitted (create action/HTTP POST)
  • The object is created and saved
  • A redirect_to is performed with an HTTP 301/302 status to the object's show view or perhaps index

—for editing an object it's:

  • A form for edit an existing object is displayed (edit action/HTTP GET)
  • The user fills in the form
  • The form is submitted (update action/HTTP PUT)
  • The object is updated and saved
  • A redirect_to is performed with an HTTP 301/302 status to the object's show view or perhaps index

You can't redirect directly to the destroy action because in RESTful Rails that's intended to be invoked as a result of an HTTP DELETE request and doesn't render a template when it's invoked. The redirect_to method always redirects to a template.

You haven't shown us the code for destroying notes, but I suspect that what you're trying to achieve can be done with a before filter and by having the controller passing the current user to a model method.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜