19

I am trying to display a notice after redirecting to a page but it doesnt appear.

Here is the redirect -

redirect_to :action => :index, :notice => "My redirect"

You can see the message in the url but there doesnt seem to be any code inside active admin to display it.

Any ideas how to render it inside active admin ?

Akhil Jain
  • 13,872
  • 15
  • 57
  • 93
Alex
  • 6,205
  • 7
  • 43
  • 53

2 Answers2

30

There seems to be some issue that I haven't tracked down yet, but if you are looking for a work-around until then, this is what I did:

member_action :test do
  flash[:notice] = "This is a test notice!"
  redirect_to :action => :index
end

The problem that I am seeing is that when you put :notice in the redirect_to method, the notice message is url encoded and added to the URL

member_action :test do
  redirect_to :action => :index, :notice => "This is a test notice!"
end

results in

/admin/model?notice=This+is+a+test+notice!

which is less than ideal. I noticed a change to the active_admin documentation that includes putting {} around the first parameter to redirect_to to fix this problem, however, for me, this results in an error.

member_action :test do
  redirect_to {:action => :index}, :notice => "This is a test notice!"
end

which results in

syntax error, unexpected tASSOC, expecting '}'
    redirect_to {:action => :index}, :notice => "This...

I posted a comment on that particular pull request @ active_admin on github and hopefully someone might have another suggestion, since I am stumped.

In any event, maybe one of these solutions will work for you. Good luck.

sorens
  • 4,975
  • 3
  • 29
  • 52
  • flash[:notice] work around worked for me. I had spent an hour messing with this until I found this answer. – jevy Dec 21 '11 at 15:09
  • 4
    You're having trouble with the ruby syntax. Try to add brackets: `redirect_to({action: :index}, notice: 'Whatever')` – chrpes Jun 25 '13 at 08:09
  • Using `flash[:notice]` in the member action didn't work correctly for me (wouldn't go away), but the above solution from @chrpes did. – SexxLuthor Jun 24 '16 at 23:52
-4

Active Admin doesn't render flash messages, it believes they are rendered in t layout renders them. When you run active_admin:install generator it mentions that:

$ rails g active_admin:install
...
Some setup you must do manually if you haven't yet:
...
3. Ensure you have flash messages in app/views/layouts/application.html.erb. For example:

   <p class="notice"><%= notice %></p>
   <p class="alert"><%= alert %></p>
  • 2
    I have added them to my layout but I want to show a flash message on a view created by active admin – Alex Oct 27 '11 at 17:47
  • 2
    the output you reference above is from the devise install portion of the active_admin install. in other words, devise is recommending that you update your layouts to include a notice/alert fields. this is has nothing to do with how active_admin displays its notice/alert messages. – sorens Nov 17 '11 at 23:39