Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I currently have a login popup in my header bar which is on every page in my website. I want to be able to reload the current page that the person is on after a successful login. How do I do this in the controller?

def create   #declaring and defining user variable stuff   if user.save     #reload current page <--how do I do this?   end end 

Thanks

like image 401
EverTheLearner Avatar asked Sep 18 '11 23:09

EverTheLearner


People also ask

What does reload do in Rails?

Reloads the attributes of object(here @user) from the database. It always ensures object has latest data that is currently stored in database.

What does .first mean in Ruby?

The first() is an inbuilt method in Ruby returns an array of first X elements. If X is not mentioned, it returns the first element only. Syntax: range1.first(X) Parameters: The function accepts X which is the number of elements from the beginning. Return Value: It returns an array of first X elements.

Can Ruby on Rails?

CanCan is an authorization library for Ruby on Rails which restricts what resources a given user is allowed to access. All permissions are defined in a single location (the Ability class) and not duplicated across controllers, views, and database queries.


2 Answers

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.

like image 94
datalost Avatar answered Oct 04 '22 20:10

datalost


If you're looking for a way to get the page to refresh (typically redirect_to :back) with an XHR request, you don't have to look for a way to change the response type - just tell the page to reload with inline JS.

format.js {render inline: "location.reload();" }

Like Elena mentions, this should go in a respond_to block, like so:

respond_to do |format|   format.js {render inline: "location.reload();" } end 
like image 34
Archonic Avatar answered Oct 04 '22 19:10

Archonic