I am trying to go back to the previous page after updating a link. Here is my link controller:
def update
if #link.update(link_params)
redirect_to :back
else
format.html { render :edit }
format.json { render json: #link.errors, status: :unprocessable_entity }
end
end
Although the link does update, it the page only refreshes, instead of going back to the previous page. Could someone help point out what I am doing wrong? Thanks!
Referrer
Each time you load a controller action in your application, you'll get a request object, which should have the referer attribute:
def update
if #link.update(link_params)
redirect_to request.referer
else
format.html { render :edit }
format.json { render json: #link.errors, status: :unprocessable_entity }
end
end
The problem is that you are sending a request from your browser in a way that it wants to handle the response as if you were going to a new page. redirect_to :back simply tells rails to send the referrer as the redirect URL. If the code read redirect_to 'http://google.com' you would expect the browser to go to google.com, would you not? The correct thing to is to make an asynchronous call using javascript and use javascript to go back in the event of success.
How this happens depends on which JavaScript library you are using. Simply make the call, and in your success function, call window.history.back() and the browser will go back.
Related
I want to redirect to another page admin_antenna_reader_rfids_path at the end of the create method. I did:
def create
#antenna_reader_rfid = AntennaReaderRfid.new(antenna_reader_rfid_params)
if #antenna_reader_rfid.save
render json: {status: true}
redirect_to admin_antenna_reader_rfid_path(q#antenna_reader_rfid)
else
render json: {errors: #antenna_reader_rfid.errors.full_messages, status: false}
end
end
I get an error AbstractController :: DoubleRenderError:
Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".
How can I solve this?
You have to remove the line render json: {status: true} as currently you're trying to make your controller render a json and redirect to an HTML page at the same time. You have to pick one.
To handle multiple request format, you can use respond_to
if #antenna_reader_rfid.save
respond_to do |format|
format.json { render json: { status: true } }
format.html { redirect_to where_you_want_path }
end
else
# same way as above
end
Within the respond_to block, you can render all the request formats as you want, then based on the request header, the controller will choose the corresponding logic to respond to you.
You can't render nor return more than once in a method.
Quick question, I've been trying for the last couple hours to discern what is causing the following behavior but it's just beyond my grasp.
I have this two actions on my 'UsersController':
def new
#user = User.new
end
def create
#user = User.new(user_params)
respond_to do |format|
if #user.save
flash[:success] = 'Wellcome, %s! You have successfully
registered.' % [#user.name]
format.html { redirect_to login_path }
format.json { render :show, status: :created, location: #user }
else
flash.now[:error] = 'Hmm... There seems to be some errors.'
format.html { render :new }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
Basically, we render a clean 'new' view, try to register a new user, and if the creation of a user fails, the application should flash a message for the current action (the create action), and render the 'new' view, updating the previous one with the flash information and the errors of the #user variable.
The problem is that, although the server processes the response just fine, the browser does not update the page, never re-renders the page, it keeps the stale 'new' view. I've looked the response with chrome's web tools and it bears the updated view, but for some reason the browser just won't render it.
I think it has something to do with caching, but really I'm out of my element here. If instead of rendering I just redirect to the new action, the flash works fine (removing the .now(), that is), but this way I lose the #user, which I would like to keep with it's full functionality.
Any ideas why this behaves like this, or at least how to solve it?
If you redirect_to the new action, when the user submits, it will still post to the create action and the user_params would still take effect. Simplying rendering :new on the already new page will not perform a fresh request/response. To initiate a fresh request you will have to use redirect_to.
I have a controller action: create which is responding to json format and html.
respond_to do |format|
if #pr.save
format.html {redirect_to(some_path)}
format.json {render json: #pr, status: :ok}
else
format.html {}
format.json {}
end
end
So from a angular service I send a post request to this action, if it is successful I would like to redirect the user to some_path in my server log I see that redirect bing made, but the page is not changing.
How can I fix this? I am using turbo links as well so I would like not loading all the files again.
If you are using Turbolinks you can do:
Set a location in your controller:
format.json {render json: #pr, status: :ok, location: some_path}
Get the location from your header response in js:
header('Location')
Use Turbolinks visit:
Turbolinks.visit(header('Location'));
And now you are not loading the assets.
You should have a route where you want to redirect your user. Then In controller You can call the page
if( <Your Condition true/false> )
$location.path('/dashboard');
remember you should have added dependency $location in your controller
Or you can take a look here :
How to integrate Rails views with angularjs ng-view?
I took over someone else's Rails project and I have a question about HTTP requests.
It SEEMS that I should be able to pass parameters through HTTP requests, I'm just unsure how. For example: rake routes shows
PUT /auction2s/:id(.:format) auction2s#update
Which seems to correspond to this function
# PUT /auction2s/1
# PUT /auction2s/1.json
def update
#auction2 = Auction2.find(params[:id])
print "Hello World"
respond_to do |format|
if #auction2.update_attributes(params[:auction2])
format.html { redirect_to #auction2, notice: 'Auction2 was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #auction2.errors, status: :unprocessable_entity }
end
end
end
But I can't figure out the URL I would need to pass to, for instance, change
id=18445&done=true
into that function.
Any thoughts? Is the function structured right? Do I just need to pass the request in a Ruby format, not through the browser or AJAX (which is what I'm trying)?
You should have a form for this action. Most likely in this location -> app/views/auction1s/edit.html.erb. It will be edit.html.haml if you are using haml template engine. The form will be rendered in the view and user input will be sent as parameters to this action on submit of the form.
I finally came to ask this question since I found no answer and it kept bothering me. What's the reason behind this behavior ? Is it REST-thingy-driven :) ?
I found this "workaround" but no explanation : How to make a render :edit call show the /edit in the address bar
Thanks
EDIT
My question was not that well written, sorry. Why the default Rails behavior is not to redirect to the edit template? That would feel more logical, to me at least :)
render doesn't redirect, so there's no reason the URL bar address would change.
The default update method looks like this:
# PUT /posts/1
# PUT /posts/1.json
def update
#post = Post.find(params[:id])
respond_to do |format|
if #post.update_attributes(params[:post])
format.html { redirect_to #post, notice: 'Post was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
The URL is /posts/1, which is what displays in the URL bar. If update_attributes fails, e.g., a validation error, it renders the "edit" template, with no redirect.