I have added /views/sitemap/index.xml and want it displayed when i go to the relevant url.
class SitemapController < ApplicationController
def index
respond_to do |format|
format.html
format.xml
end
end
end
And in routes.rb
match "sitemap/" => "sitemap#index"
Using Rails 3
When I go to mydomain.com/sitemap/ I just get a white page. Any ideas?
index.xml
<?xml version="1.0" encoding="UTF-8"?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
<loc>http://www.mydomain.com/</loc>
<changefreq>weekly</changefreq>
</url>
</urlset>
Problem is that you are using your index action to render xml and it will render "index.xml"
file not "sitemap.xml" which is what you have created in your views
While your routes are correct, you are using the wrong filename in views
Try renaming sitemap.xml file to index.xml ( in the views/sitemap folder)
If you define name routes, you need to define :format with it
match "/sitemap/sitemap.[:format]", :to => "sitemap#index"
it will pickup your format from there. Also you can define a default format in the routes
match "sitemap/sitemap.xml", :to => "sitemap#index", :defaults => {:format => :xml}
I may be wrong , but I see 2 reasons:
index action doesn't actually do anything judging by this code sample, it just responds back with no info.
you need to render your object as xml - if you don't rails, doesn't know you want xml - it just treats it as another file extension. It actually lets you do little tricks - like sending json to an xml request ( thou I have no idea why would anyone try to do that). Thou one useful application is that you can make rails send custom rendering of an object to a common format or render regular data in common format for an unusual extension ( we had a client who wanted csv data for a .dat request)
Here is a short example, from a sample home controller:
class HomeController < ApplicationController
def index
#m = {
:color => "yellow",
:total => "20"
}
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #m}
end
end
end
this returns this object as xml:
<hash>
<total>20</total>
<color>yellow</color>
</hash>
Related
I have a list of users being displayed, you can click on "Show user" or "PDF" to see details of that user in HTML or as a PDF document. The show was automatically created with scaffolding, now I'm trying to add the option to view it as a PDF. The problem is adding a second GET option, if I pass the user along as a parameter, it is assumed to be a POST and I get an error that the POST route does not exist. I am not trying to update the user, just to show it in a different way, basically to add a second "show user" option.
How do I tell it that I want a GET, not a POST? Is there an easier way to do what I am trying to do? Thanks.
Please, create a controller like this:
class ClientsController < ApplicationController
# The user can request to receive this resource as HTML or PDF.
def show
#client = Client.find(params[:id])
respond_to do |format|
format.html
format.pdf { render pdf: generate_pdf(#client) }
end
end
end
Please, update route.rb file, action name with post and get, like below :
match 'action_name', to: 'controller#action', via: 'post'
match 'action_name', to: 'controller#action', via: 'get'
More info please read this link : "http://guides.rubyonrails.org/routing.html"
you haven't posted any code or details, so I am guessing you want something like this:
routes
resources :users
controller
class UsersController < ActionController::Base
def show
#user = User.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.pdf # handle the pdf response
end
end
end
view file in views/users/show.pdf.prawn
prawn_document() do |pdf|
#user.each {|r| pdf.text r.id} # will print user id of the user
end
The way above example will work is, if something visits the following URLs, they will get html file:
localhost:3000/users/1 #html is the default format in rails
localhost:3000/users/1.html
but if they visit .pdf, they will be served a pdf format.
localhost:3000/users/1.pdf
If the above assumptions are correct, then check prawn or wicked_pdf pdf gem. the above example uses prawn
Checkout this link http://apidock.com/rails/ActionController/MimeResponds/InstanceMethods/respond_to. You can add a new MIME type and pass on the :format as pdf in all your rails routes.
Hope this will help.
And for the POST-request check your
config/routes.rb
There shoud be a few routes already, so you can infer the route you need.
In your link you can pass an additional parameter called format for pdf. For e.g.
<%= link_to 'Display in PDF', "/user/pdf", :format => "pdf" %>
So there's the basic function like this ....
def show
#position = Position.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #position }
end
end
and if you go to, say, http://localhost/(position_id).xml then you will see the xml... But I wish to have a standard xml filename that I can write the object details to because I want to access it from outside rails. I am very new to rails and have not programmed in a while...seems like this should be easy, right ? So I just want to have a file created with a name like my_xml.xml for all the objects, rather than 1.xml, 2.xml, (id).xml, etc.
Thanks for the help !
I met similar questions, here is my solution(I am a newbie):
I added below in routes.rb:
resources do
collection do
get 'my_xml'
end
end
Then added the my_xml action in controller. In my_xml action, assemble all data, and format it to xml via to_xml method.
No sure whether above is useful for you. Hope it helpful.
I want some users to be able to download data in a yaml file.
I see that you can do this with
send-file (but uses a lot of resources)
direct link_to the file in public folder (not good for me since the file is generated so the request needs to go to a controller.
restful url via controller (this method is partially explained in http://guides.rubyonrails.org/action_controller_overview.html but not enough to get it working!)
I followed this and tried something like
def show
#client = Client.find(params[:id])
respond_to do |format|
format.html
format.yml { render :yml => #client.redis_to_file }
end
end
redis_to_file returns a string with the yaml data
in config mime_types.rb
Mime::Type.register "x-yaml", :yml
then access like
clients/5.yml
All I get is "invalid template". (It's correct, I don't have a yml template in my views.)
Any clues about how to do this so that it works is greatly appreciated.
Try this:
respond_to do |format|
format.html
format.yml { send_data #client.redis_to_file, :type => 'x-yaml' }
end
There are more options in the Docs
Let's say that I have a postback url that comes in as
http://domain/merkin_postback.cgi?id=987654321&new=25&total=1000&uid=3040&oid=123
and other times as:
http://domain/merkin_postback.php?id=987654321&new=25&total=1000&uid=3040&oid=123
If my route definition is
map.purchase '/merkin_postback', :controller => 'credit_purchases', :action => 'create'
it barks that either of the two forms above is invalid.
Should I be using regex to recognize either of the two forms?
This isn't a routing issue, it's a content format issue. You should be using respond_to.
class CreditPurchasesController < ActionController::Base
# This is a list of all possible formats this controller might expect
# We need php and cgi, and I'm guesses html for your other methods
respond_to :html, :php, :cgi
def create
# ...
# Do some stuff
# ...
# This is how you can decide what to render based on the format
respond_to do |format|
# This means if the format is php or cgi, then do the render
format.any(:php, :cgi) { render :something }
# Note that if you only have one format for a particular render action, you can do:
# format.php { render :something }
# The "format.any" is only for multiple formats rendering the exact same thing, like your case
end
end
end
I'm trying to get my head around nested associations in Rails using ActiveResource.
My example is as follows:
What I have is an airport with many runways.
My show action in airports controller contains:
#airport = Airport.find(params[:id])
When I call http://localhost/airports/2.xml I get that piece of XML:
<airport>
<code>DUS</code>
<created-at type="datetime">2009-02-12T09:39:22Z</created-at>
<id type="integer">2</id>
<name>Duesseldorf</name>
<updated-at type="datetime">2009-02-12T09:39:22Z</updated-at>
</airport>
Now, I changed the action to
#airport = Airport.find(params[:id], :include => :runways)
How can I achieve that above loading above URL is giving me something like:
<airport>
<code>FRA</code>
<created-at type="datetime">2009-02-12T09:39:22Z</created-at>
<id type="integer">2</id>
<name>Frankfurt</name>
<updated-at type="datetime">2009-02-12T09:39:22Z</updated-at>
<runways>
<runway>
<id>1</id>
<name>bumpy runway</name>
</runway>
</runways>
</airport>
And on top of that: If I have a client with
class Airport < ActiveResource::Base
..
end
and
class Runway < ActiveResource::Base
..
end
How can I get it to automatically load associations like:
a = Airport.find(1)
puts a.runways.length
=> 1
And (last but not least): Is there a way to store data from the client like:
a = Airport.find(1)
a.runways << Runway.find(1)
a.save
Maybe I'm really too blind, but I'm stuck...
Any idea is warmly welcome.
Thanks
Matt
Resolved it myself finally.
Wasn't aware to put the include into the render statememt:
def show
#airport = Airport.find(params[:id], :include => :runways)
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #airport.to_xml(:include => :runways) }
end
end
The :include option for the finder specifies that it should eagerly fetch the related items from the database. The :include option for to_xml specifies that it should be included in the XML rendering.
If the canonical XML representation includes the related objects, you can override the to_xml method to make your life a little simpler:
class Airport
def to_xml(options={})
super(options.merge(:include => :runways))
end
end
and then since render will call to_xml if you don't, your controller code can simply be
format.xml { render :xml => #airport }