Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
like image 216
user750897 Avatar asked Oct 12 '22 01:10

user750897


2 Answers

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

like image 162
Zepplock Avatar answered Nov 01 '22 18:11

Zepplock


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.

like image 29
Max Williams Avatar answered Nov 01 '22 16:11

Max Williams