Noob
How should I send a single string variable to another site from my rails app? ie the outside service sends a get request to my controller/route and then the controller must respond with the string (and I assume a response code). The string is intended to be added to their html code.
In my controller should I
render :text => "string"
or
respond_with("string) #as xml or json
or something completely different?
Just try the following code. Here your application gives the text and json as per the request.
respond_to do |format|
format.json do
render :json => 'string'
end
format.html do
render :text => 'string'
end
end
Related
So I have been going through Rails Zombies and have gotten to the part explaining format.html and .json
My question is what do these lines of code do, and why do we have them? If I write these methods or actions without these format codes they work perfectly fine, as i'd assume they simply display in html format by default? If somebody could clear up exactly what this code does I'd be grateful, I also do not fully understand what JSON is.
def create
#zombie = Zombie.new(zombie_params)
respond_to do |format|
if #zombie.save
format.html { redirect_to #zombie, notice: 'Zombie was successfully created.' }
format.json { render :show, status: :created, location: #zombie }
else
format.html { render :new }
format.json { render json: #zombie.errors, status: :unprocessable_entity }
end
end
In simple terms:
If the request wants an HTML page, it will perform the instructions set by the block given to format.html.
If the request wants application/json (like when you make an Ajax request), the response will be given as instructed in the block given to format.json.
You should know what JSON means before delving into creating any web service. See http://www.json.org/
I am trying to get around this weird error for some time now. This is the basic controller I have:
get :index, provides: :json do
#requests = Request.order(:created_at)
render 'requests/index'
end
get :show, with: :id, provides: :json do
#request = Request.find_by(id: params[:id])
render 'requests/show'
end
This is how my rabl json files look like in each case:
index.json.rabl:
collection #requests
attributes :created_at, :updated_at, :user_id, :request_type
show.json.rabl:
object #request
attributes :id
The first route i.e. :index returns the array of Request objects nicely but the second route i.e :show throws the following error:
NoMethodError at /show/1
undefined method `head?' for #<Request:0x007f6814485840>
Can someone point at what this error could be about? Why is it looking for head? function?
Don't use #request instance variable. Rack stack uses it to store HTTP request there.
I am trying to upload a file using dropzone. When the file is uploaded, I want to redirect the page to root url. When I upload the file, the request is sent and the response is in json format. I have in my controller:
respond_to do |format|
format.json{ render :json => { :location => root_url, :flash => {:success => "The file was uploaded successfully"} }}
end
Can please anyone help me with what is wrong with my respond_to block?
This json response will not in itself redirect, as it responds with json to (presumably) an ajax request. When the response comes back, the client will need to do something with that response, such as set window.location to the :location attribute.
ie in the success callback of the dropzone request
I have this rule:
match '*urlnames' => 'home#searching_names'
The URL address looks like website.com/john.html.
The problem is, that in the log I see
Parameters: {"urlnames"=>"john"}
without the .html extension. Text extension is important, I would need to test it in the controller.
I tried to add to the routing rule this part:
match '*urlnames' => 'home#searching_names', :defaults => { :format => "html" }
But still the same, in the log is
Parameters: {"urlnames"=>"john"}
How can I catch the extension in the controller?
You have access to the requested format via request.parameters[:format] or (as a MIME type) via request.format.
However, you can also use a respond_to block:
def show
file = params[:urlnames]
respond_to do |format|
format.html { ... }
format.txt { ... }
end
end
where ... is code to render some text, or send some data or a file.
If you're just trying to show some static files, just place them in the public dir, and bypass Rails entirely.
I'm encountering a strange behavior in my controllers. They seem to occasionally want to redirect instead of render a json response.
respond_to :json, :html, :js
def create
#favorite = current_user.favorites.build(:location_id=>params[:location_id])
if #favorite.save
respond_with(#favorite)
else
respond_with(#favorite.errors)
end
end
I think it works most of the time but today I was notified of this error:
NoMethodError: undefined method `favorite_url' for #<FavoritesController:0x00000006171dc0>
The params hash was logged as:
{"format"=>"json",
"action"=>"create",
"user_id"=>"56",
"auth_token"=>"iGSty8CMIaWsbShYZEtw",
"location_id"=>"47943",
"controller"=>"favorites"}
Especially strange since it seems to work most of the time... I have changed a few of my other controllers to use the old format.json { render :json => #object } syntax but I'd like to avoid that if possible.
How could this be?
On paths that are not GETs, respond_with tries to redirect to the url for whatever it is given. You can override this with a custom Responder