Rails Controller action render last page? - ruby-on-rails

I want to redirect to page from where I come is that possible?
What I do:
Calling a controller function from an view, and when action is finished I want to render the same page like a back function?
Thanks

Use
redirect_to :back
You can also pass more params to it. Read more here: http://api.rubyonrails.org/classes/ActionController/Redirecting.html

If what you want is actually render (not redirect) to the last rendered view you can do it in the following way:
render request.referer.split('/').last
It guesses the last rendered view from the request HTTP referer (the URL from which the controller action has been called). Note this approach it's quite fragile, but can be handy in most cases.

Related

Ruby on rails button that will not redirect to another page but does it's action on the same view

before anyone get's mad I would like to start by apologizing by how stupid it may seem to some but i'm really having a hard time finding out how.
I have been looking through tutorials online on how to program in ruby on rails but I can't seem to find out how to make a button that does it's action and won't redirect to another page. For example when I want my program to count the number of people present in my database I need to make another view where the action would be executed. I would like to make it do the action on the same view and not redirect to another page.
Every tutorial that I've gone through only redirects the action to another page.
Could anyone please help me? Thanks in advance.
Suppose that you want to have that button in an index page of some controller. If you add something like this (I'm using HAML here):
= link_to "Something"
Then clicking the link will refresh the page but calling the index method of your controller again before rendering. Now the only problem is to figure out if the index method has been called by clicking on the Something link or just by calling the page? You can do this by passing some parameters (e.g. ?mylink=true) when clicking the link.
HOWEVER I personally prefer to implement a custom controller method (via routes.rb) and then redirect to the desired page (e.g. index). Something like this:
routes.rb
resource :my_resource
collection do
get "test"
end
end
my_resource_controller.rb
...
def test
# Do stuff
redirect_to action: "index
end
my_resource/index.html.haml
= link_to "Something", text_my_resource_path
Now clicking on Something link gets you to test method and after doing your stuff you are redirected back to where you were (i.e. index). Read more about redirect_to HERE.

Rails - How does the show and new action work

Very general question I was hoping someone could clarify for me. I'm looking at the basic generated scaffold code for a model called products. I noticed the new and show actions in the controller don't have much written in them. In fact, show is entirely empty and new only has the line "#product = Product.new". I know these 2 actions are supposed to go to a separate view. A view to show the resource, and a new form view to input info and create a resource, respectively.
So, I'm curious how that actually happens. Other actions have redirect_to :some_path which makes sense, but how exactly does "render action 'show', location: #product " bring up the items show page when the action is empty? Also how is that different from redirect_to #product ?
thanks
Will
Render produces a string that will displayed as the response to the request to the application.
redirect_to produces a response header resulting in a new request to the application.
The render action 'show', location: #product uses the the file app/views/products/show.html.erb with #product as a parameter to produce the html which will be returned.
The reason some of the controller functions are empty is that rails are using defaults. So if you don't tell rails what to render then rails will look for a file in the appropriate location.
Methods ending with redirect_to are usually post/patch requests saving something in your database and after the requested action has been performed they redirect the user to a method meant for displaying information.

Rails: call controller action without changing url / redirect to controller index

I would like to have 'Next' and 'Previous' buttons on my page but when clicked, I want to have the appropriate controller action called but without redirecting to the domain/controller/action.
The next and previous actions would pop from a session array that contains ids of what to display.
Is it possible to provide my link: reader_next_path (redirects to localhost/reader/next)
but then have the Reader next method render the index with my specified #instance_variables?
I really don't want to have to create a new template for all of these actions.
render "index"
http://guides.rubyonrails.org/layouts_and_rendering.html#using-render
I prefer using a symbol in this case:
render :index

Rails redirect without changing URL

I've some piece of ruby/rails code.
In one of the controllers say foo i have an action doo which does something and I want to redirect to another controller say bar and action say dar.
When I use redirect_to then URL in the address bar changes to /bar/dar while if I use render then I don't know how to render another controller's view.
I am using rails 2.3.5 so render_component is unavailable for use (which i found could be really really useful for me) -- so as a shortcut if you have any idea of alternate for render_component that will help me infinitely.
Any ideas?
[If am unclear please ask me details]
Instead of a real redirect, could you use an AJAX call to hit the action and pull in the appropriate views?
You can render the same partial from both views. You'll have a view for each action and in each of the views you'll have something like <%= render :partial => "partials/form" %>.
You can just call the action (like calling a function) and then render the template. But accordingly to the MVC pattern you should manage your controller-side logic on the controller and then loads the propper view.

Doing an action in Rails without changing site

Good day...
Still a beginner question. I mostly asking to save myself hours of trial and error... I had those already and there was no result.
What I wanna do... I have a controller that has an action, of course. I want to send this action two IDs, it should make some database entry of those. No problem, the action works well.
But I don't want a view to it, I want the user to click on a link and not even leave the site. Ideally there should be some Ajax (using jQuery a lot on the site) that changes the link to a 'Thanks' or anything.
So yes, my main question is: How can I activate an action of an other controller without leaving the site, from a link. Long intro, but I hope you can give me some hints.
Take a look at link_to_remote for the view side and then you can just have your controller function render nothing => true.
To keep it un-obtrusive & return something from the controller action... I think you probably want something like:
def my_action
# your code... e.g. #model = Model.find(params[:id])
render :json => { :whatever => #model.whatever }
end
In your javascript, do what thou wilt with the returned information & prevent the default action of your hyperlink by returning false or using jQuery's prevent default functionality for the click event.
Look up link_to_remote method in rails. That should make an ajax call out to a controller function and not leave the page.

Resources