In Rails, can you set flash[:error] when calling link_to?
For instance, let's say I 开发者_如何学运维have the following:
link_to 'Page', page_path(foobar)
Is there a way to set flash[:error] in the link_to call that will be displayed at page_path
?
I'm looking for something along the lines of:
link_to 'Page', page_path(foobar), :error => "Flash message"
In your controller that responds to the link you should do flash[:error] = message
. It should be in the same place where you decide if you need to redirect your admin to the edit
page. Once again it is in the controller
and not in your view
You should set flash[:error] in the controller but you could make it use a param you send it in the link if you want. Move the :error option into the route call:
link_to 'Page', page_path(foobar, :error => "Flash message")
Then in the controller, i you don't find the resource or whatever, you could say
flash[:error] = params[:error] || "Sorry, we couldn't find that page"
The problem with this is that the param will show up in the url, so you will have a url of (eg)
"/pages/123?error=Flash%20message
or something like that.
精彩评论