Rails flash notice via ajax

匿名 (未验证) 提交于 2019-12-03 02:49:01

问题:

Long story short, I have a button. On clicking it, I want an ajax request to be triggered which gets flash[:notice] and displays it in a div in$

Here is my shortened view:

 

My ajax request in the view:

$("#search").submit(function(){                             $.ajax({                                 type: "POST",                                 url: //url to my show action                                 success: function(data){                                       /*$("#notice").html("");                                         $("#content").html(data);*/                                 }                             });                             return false;                     }); 

My controller:

def HomeController  'search'   end end 

My show.js.erb

#app/views/dashboard_home/show.js.erb $("#notice").html("");  $("#content").html(""); 

The problem is when I click on button, the notice is displayed fine. But the same notice persists on the next clicks too. The search partial contains the table Please help!

回答1:

Sessions

the same notice persists on the next clicks too

This is caused by the flash being stored in the session variable of Rails:

The flash is a special part of the session which is cleared with each request. This means that values stored there will only be available in the next request, which is useful for passing error messages etc.

The problem you have is that since I don't think ajax counts as a new request (need reference for this), the data will persist into the next time you request via HTTP.

--

Fix

I would initially try this:

def show     respond_to do |format|         format.js {  flash[:notice] = "my secret number "+rand(0,5)+" !" }     end end 

The main problem you have is you're processing the flash variable in your JS using the ERB preprocessor. This is an issue as it means you won't be able to use asset precompile to help it work.

After looking at this question, why not try using the after_filter callback, like this:

#app/controllers/home_controller.rb Class Home 

--

Update

You should include the success functionality in your show.js.erb:

#app/views/home/show.js.erb $("#notice").html(""); 

This means you can remove the whole ajax call from the application.js, and replace with the remote: true for your search form:

#app/views/search/index.html.erb  

The reason this works is because when you use the format.js respond block, Rails will load the [action].js.erb file in your views. Considering this only happens after the action has been completed, it's equivalent to the success function of your ajax.

By doing this, you'll be able to remove the entire ajax function from your application.js, and replace with the UJS version, as described above



回答2:

Here is an example that I got working, thanks to Rich Peck's answer. I needed to use flash.now to make sure the flash notice didn't persist.

AJAX trigger in the view:

 

Controller:

# app/controllers/users_controller class UsersController 

Rendered view:

# app/views/users/index.js.erb $("#flash").html(''); 

where the flash notice is displayed in the layout:

# app/views/layouts/application.html.erb 
# app/views/shared/_notice_banner.html.erb
×


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!