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
@object.destroy
respond_to do |format|
format.html { redirect_to current_page_url, notice: 'page was successfully destroyed.' }
format.json { head :no_content }
end
end

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.

EDIT:

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
end

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)
end

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

Try

redirect_to customer_path(@license.id)

instead.

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

But if it doesn't, try:

redirect_to show_customer_path(@license.id)

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
end

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

def clear_return_to
session[:return_to] = nil
end

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

http://railstutorial.org/chapters/updating-showing-and-deleting-users#sec:friendly_forwarding

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:

//bands_controller.rb
def index
@bands = Band.search(params[:search])
end

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

//Band.rb
def self.search(search)
if search
find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
find(:all)
end
end

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.

Sources:
Railscast#27
Railscast#240



Related Topics



Leave a reply



Submit