I have a website using unobtrusive ajax.
In short, the flow is:
- remote link
- Request managed by controller
- js.erb view sending response back to browser
I want all my js.erb views to execute similar "maintenance activity", like inserting flash messages when needed, updating the url, etc.
How can I make this DRY? Ideally, I would use a partial with the code to be executed every time but I couldn’t find how to do this.
My js.erb file:
$("#challenge_actions").html("<%= escape_javascript(render partial: "challenges/best_practice_button")%>");
$("#flash_messages").html("<div id='flash_notice'><%= escape_javascript(flash[:notice])%></div>");
How can I extract the line with flash_messages and have it automatically included in all my js.erb files?
You can use js layouts , just like html layouts.
layouts/custom.js.erb
controllers/your_controller.rb
your_action.js.erb