HTTP request - Ruby on Rails - ruby-on-rails

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.

Related

Redirect to another page in Ruby on Rails

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.

Rails, respond_to blocks and |format|

Rails scaffold generated the following:
respond_to do |format|
if #student.save
format.html { redirect_to #student, notice => 'Student was successfully created.' }
format.json { render :show, status: :created, location: #student }
else
format.html { render :new }
format.json { render json: #student.errors, status: :unprocessable_entity }
end
end
After reading this I understand how the respond_to is working (sort of), but I don't get what format is doing. Shouldn't it be either format.html or format.json and not both? What are these two lines actually doing?
format.html { render :new }
format.json { render json: #student.errors, status: :unprocessable_entity }
Is there an implied if in there? Is it something like
if (format == html) {}
if (format == json) {}
Side note: Why does update require the respond_to block while show will handle /students/1.json or /students/1 without any logic at all?
format is a local variable that respond_to yields. When you do format.html {} you are actually registering a callback block for a format.
Rails goes through the registered formats and tries to find a compatible format to the MIME type in the request. If there is no handler it will raise an error.
This could be explained as something like using syntactic sugar on top of a case statement (the Ruby equivalent of a switch statement). But the analogy is not completely accurate since Rails does a bit of work in matching the request type.
Also the code inside your block is not executed when the format.html block is registered (as it would be if it was just a conditional statement) but rather when respond_to finishes or not at all if you are using for example E-Tag caching.
Why does update require the respond_to block while show will handle
/students/1.json or /students/1 without any logic at all?
Rails handles many actions by using a convention over configuration approach and guessing the intent of the action.
def PostsController < ApplicationController
def index
# rails auto-magically fills in the controller with something
# like this
#posts = Post.all
respond_to do |format|
format.html { render :index }
format.json { render json: #posts }
end
end
def show
# convention over configuration is awesome!
#post = Post.find(params[:id])
respond_to do |format|
format.html { render :show }
format.json { render json: #post }
end
end
def new
#post = Post.new
render :new
end
def edit
#post = Post.find(params[:id])
render :edit
end
end
Rails assumes that there is a resource with the same name as the controller and auto-magically fills in the controller action. It also assumes there is a view in app/views/posts/(:action).html.[erb|haml|slim|jbuilder]. This is known as implicit rendering.
The comments show roughly what action rails attempts.
It does not fill in actions which operate on data (create, update, destroy) since the actual implementation can vary greatly and it's hard to make useful guesses.
Well, it depends on the format of the request. If a request demands HTML from the server, format.html block will be executed, and in the same way, if a request demands JSON format, format.json will be executed.
Rails will automatically(read: magically) handle the if (format == html) part for you. All you have to do is fill in the blanks. Same way, you can write a block for XML starting with format.xml.
And for the side note, I think you have said it otherwise. update method doesn't require respond_to block, while show requires. And the reason is very simple: update method is there to update the Model, and then, redirect you to somewhere, while show will always return you something. In your case, /students/1 will return you the first student created in the database, and the response will be HTML, while /students/1.json will return you the same result, but response will be JSON this time.
Well you could very well replace 'format' with 'foo' or 'banana' or whatever you want. It is just the variable name in this case because the variable that is sent to your block by respond_to is passing along the format as requested by the incoming http request's Accept header.
Sometimes you'll see 422 "Unacceptable" errors in your logs because you are receiving a request with an Accept header that does not request a mime type your app knows about.
As it is, your callers should be using a browser or be a JSON consumer sending the proper headers to receive responses from the boilerplate.

Previous page after updating rails

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.

Why does Rails want to return "head :no_content" for JSON PUT requests?

After I run
rails generate scaffold User
The generated controller function in Rails 3.2.11 for updating a user looks like this:
def update
#user = User.find(params[:id])
respond_to do |format|
if #user.update_attributes(params[:user])
format.html { redirect_to #user, notice: 'User was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
The line I'm curious about is returning head :no_content for a successful JSON update request. I've done some googling, as I was guessing that this is some sort of RESTful property, to not return the updated object, but I couldn't find anything that claimed that was the case.
Why is this the default, versus returning the JSON representation of the User object post-update?
Good question, apparently the purpose is to return a HTTP status code 200 with an empty body, see this discussion. Maybe for brevity or security purposes. head :no_content seems to create a HTTP response 200 (success) with an empty body, returning this response header:
Status Code:200 OK
see also this related question.

Action won't render on validation fail

I have a fairly standard controller with a create method and some validations.
def create
#type = Type.new(params[:type])
respond_to do |format|
if #type.save
format.html { redirect_to types_path, notice: 'Type was successfully created.' }
format.json { render json: #type, status: :created, location: #type }
else
format.html { render action: "new" }
format.json { render json: #type.errors, status: :unprocessable_entity }
end
end
end
The problem is that when a validation fails, I get the errorMissing template ontology/types/create, as if the render action: "new" weren't there. If I replace it with a redirect_to then it works as expected, but then it seems I can't pass the form errors along.
I know that there is a #type instance (with #type.errors) from the original call of new, and throwing it just before the render call confirms this.
The same thing is happening when a validation fails on update It seems like the render call is just being ignored!
NOTE: my routing structure is a little unconventional, but I see not reason why this should be related.
This looks very similar: Path defined in controller and action is getting ignored, Ruby on Rails
Based on the answer to that question, I'm guessing that something is missing that is needed for rendering the new view, and as a result rails is just skipping the render call altogether and rendering create.
Can you show the new controller action and view?

Resources