How to Reload the Current Page in Ruby on Rails

How can I reload the current page in Ruby on Rails?

For my application, I use redirect_to :back and it does the trick. However, I doubt this might have an error in a non general use case(s) (user came from a special page?) but i haven't found it so far in my app.

Refresh page after deleting

At your controller destroy method use the current page's url for redirect.

def destroy
respond_to do |format|
format.html { redirect_to current_page_url, notice: 'page was successfully destroyed.' }
format.json { head :no_content }

How to reload the page after action : create?

All you need to do is a simple redirect, add this line at the end of the action

redirect_to @order

This will redirect to the show action of the new order.


Just noticed you are doing a remote: true request, so instead you need to create a js template create.js.erb for example, and add the javascript you want to be executed to add the created order in the view, here's an example:

$('#orders').append('<%= j( render @order ) %>')

Of coruse this is assuming that the orders div is has an id #orders

Action that refreshes the current page

Not sure to catch your question but can't you just do:

def action_1
# something_to_the_db
redirect_to :back

If you need to access your controller_name and action_name in your action_1 for whatever reason:

<%= link_to 'Refresh' refresh_path(:aktion => action_name, 
:kontroller => controller_name) %>

aktion and kontroller aren't typo. You have to write them like that, otherwise, it will clash.

action_name and controller_name are variables. Write them like that.

It will look like something like that (depending on what's your refresh_path, your current controller and your current action):

<a href="/refresh?aktion=index&kontroller=articles">Refresh</a>

Then in your controller:

def action_1
kontroller = params[:kontroller]
aktion = params[:aktion]
# Do whatever you want in the db
redirect_to :controller => kontroller, :action => aktion
# or redirect_to :back (better IMO)

How to create simple refresh page link with Ruby on Rails

link_to "reload", url_for(params)

How to reload current page when destroy a micropost with AJAX in RAILS

Try to add page param to your delete request like this:

<% if current_user?(micropost.user) %>
<%= link_to "Delete", micropost_path(micropost, page: params[:page]),
remote: true,
method: :delete,
data: { confirm: "You sure?" } %>
<% end %>

Rails controller action redirect_to refresh page


redirect_to customer_path(


Depending on what your routes.rb file says, it should work.

But if it doesn't, try:

redirect_to show_customer_path(

However, here I have to assume that somehow, your customers_controller.rb is somehow showing records from the License model. If License and Customer are separate models, you will have to find the customer_id in some other way.

Perhaps, it is:

redirect_to customer_path(@license.customer_id)

If License is not connected to Customer in any way, you will need to pass it in as part of the post request.

Refresh Same page

you can use 'render' to refresh a page, and 'redirect_to' to redirect to a page going through its controller.

if you want to store a page to redirect back to, you can use:

def store_location
session[:return_to] = request.fullpath

def redirect_back_or(default)
redirect_to(session[:return_to] || default)

def clear_return_to
session[:return_to] = nil

This is taken from Michael Hartl's book, he uses similar code to redirect to the requested page after the user signs in.

Reloading page partially in Rails

You got most of your code very twisted and not really lean as far as I can tell. The short and very lean way to get your search results updated without reloading the rest of the page (including your player) could look like this. The index function should look like this:

def index
@bands =[:search])

You have to create the search-method inside of your Band-Model:

if search
find(:all, :conditions => ['name LIKE ?', "%#{search}%"])

You need to render your _preview.html.erb inside a div with a certain ID somewhere inside your view:

<div id="bands"><%= render 'preview' %></div>

Your form_tag has a lot of arguments that you won't need anymore:

<%= form_tag bands_path, :method => 'get', :id => "search_form" do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
<% end %>

The jquery I use for my forms looks like this.. Pretty simple in my opinion:

$(function() {
$("#search_form input").keyup(function() {
$.get($("#search_form").attr("action"), $("#search_form").serialize(), null, "script");
return false;

This fires the index.js.erb (you will have to create that) everytime you put a letter into your search field:

//index.js.erb (same folder as index.html.erb)
$("#bands").html("<%= escape_javascript(render("preview")) %>");

This should refresh your div. It's maybe a little work but that's the easiest way to implement an ajax search function into your index page that I could think of.


Related Topics

Leave a reply