Making a sitemap.xml with Ruby on Rails 4 - ruby-on-rails

I am trying to make a sitemap for my Ruby on Rails 4 application. I would use a gem like this dynamic_sitemaps but I can't use it with Heroku so I looked around and found this tutorial: http://meghagulati.com/2013/12/05/sitemap-xml-on-heroku-with-ruby-on-rails/ to make my own (with small changes)
But I am getting this error when I go to myapp.com/sitemap.xml, I hope someone can help me to find the error.
ActionController::UnknownFormat in SitemapsController#index
ActionController::UnknownFormat Extracted source (around line #7): respond_to do |format|
#app/controllers/sitemaps_controller.rb
class SitemapsController < ApplicationController
def index
#static_pages = [root_url]
#movies = Movie.all
respond_to do |format|
format.xml
end
#series = Series.all
respond_to do |format|
format.xml
end
end
end
#app/views/sitemaps/index.xml.builder
base_url = "http://#{request.host_with_port}"
xml.instruct! :xml, :version=>'1.0'
xml.tag! 'urlset', 'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9' do
xml.url{
xml.loc("http://myapp.com")
xml.changefreq("weekly")
xml.priority(1.0)
}
xml.url{
xml.loc("http://myapp.com/movies")
xml.changefreq("daily")
xml.priority(0.9)
}
xml.url{
xml.loc("http://myapp.com/series")
xml.changefreq("daily")
xml.priority(0.9)
}
#movies.each do |movie|
xml.url {
xml.loc "#{movie_url(movie)}"
xml.lastmod movie.updated_at.strftime("%F")
xml.changefreq("weekly")
xml.priority(0.5)
}
end
#series.each do |series|
xml.url {
xml.loc "#{series_url(series)}"
xml.lastmod series.updated_at.strftime("%F")
xml.changefreq("weekly")
xml.priority(0.5)
}
end
end
#config/routes.rb
resources :sitemaps, :only => :index
get "sitemap.xml" => "sitemaps#index", format: :xml, as: :sitemap

First of all, you respond_to must only be called once, so you need to change your controller method, e.g.:
class SitemapsController < ApplicationController
def index
#static_pages = [root_url]
#movies = Movie.all
#series = Series.all
respond_to do |format|
format.xml
end
end
end
This change should render an XML file in your browser when you visit the following URL:
http://lvh.me:3000/sitemaps.xml
Furthermore, you need to change your routes specification and use a string for the format rather than a symbol, i.e. change :xml to "xml":
get "sitemap.xml" => "sitemaps#index", :format => "xml", :as => :sitemap
You should see the same XML file in your browser when visiting the following URL:
http://lvh.me:3000/sitemap.xml
(lvh.me resolves to localhost)

Related

When migrating a Rails 3 app to Rails 4.2, index.html.erb renders a blank page

I am trying to migrate a Rails 3 application to Rails 4.2.10. However, when i run rake migrations and pre-compile assets, the rendered welcome page is blank.
routes.rb
root :to => 'welcome#index'
get "/privacy" => 'welcome#privacy'
get "/help" => 'welcome#help'
resources :user_sessions
welcome_controller.rb
class WelcomeController < ApplicationController
skip_before_action :require_login
def index
cookies.delete :range
cookies.delete :lat_lng
respond_to do |format|
format.html { render :nothing => true }
end
# respond_to do |format|
# format.html # index.html.erb
# format.json { render json: #faces }
# end
end
def privacy
respond_to do |format|
format.html # privacy.html.erb
format.json { render json: #faces }
end
end
def help
respond_to do |format|
format.html # help.html.erb
format.json { render json: #faces }
end
end
end
The commented code will look for an actual index.html.erb. The code that is uncommented, just says render nothing: true, an empty response.
I rewrote def index as follows:
def index
cookies.delete :range
cookies.delete :lat_lng
[snip]
[snip]
end
end
render nothing was deprecated in RoR 4. By deleting the two statements entirely, it accomplished the same thing.

How to prohibit any response except html in controller

I have such code:
def show
#attachment = Attachment.find(params[:id])
respond_to do |format|
format.html
end
end
This code allows respond on json, but renders nothing. How to show custom page on any other request except html?
you can use format.any for any other request except html:
def show
#attachment = Attachment.find(params[:id])
respond_to do |format|
format.html { render text: => 'This is html' }
format.any { render :text => "Only html is supported" }
end
end
Try:
before_filter do
render text: 'Wrong type', status: 406 unless request.format == Mime::HTML
end
You can restrict the format of your routes when you define them in your config/routes.rb
scope :format => true, :constraints => {:format => :html} do
resources :attachments
end
Define all the routes whose format you want to restrict within that scope.

returning rails object by id & routing problem?(Ruby rails)

This should have been a relatively simple one but I must be making a mistake with my routes or something. I want to return an active record as json based on an id. So heres what I have and in my eyes it should have worked.
The route:
match '/repository/infoid/(.:id)(.:format)' =>'repo#infoID', :via =>:get
The def within the controller
def infoID
puts (params[:id])
#specificObject = myObject.find_by_id(params[:id])
respond_to do |format|
format.xml{
render :xml =>
{
:returnedObject => #specificObject
}
}
end
end
Why is it that when I go to my address of http://127.0.0.1:3008/repository/infoid/1.xml
I get no route found for /infoid/1.xml
get '/repository/infoid/:id' => 'repo#infoID'
little refacrtoring for controller
def infoID
#specificObject = MyObject.find(params[:id])
respond_to do |format|
format.html{}
format.xml{
render :xml => #specificObject
}
end
end

How do I specify ":layout => false" in Rails' respond_with?

I have this setup:
class UsersController < InheritedResources::Base
respond_to :html, :js, :xml, :json
def index
#users = User.all
respond_with(#users)
end
end
Now I am trying to make it so, if params[:format] =~ /(js|json)/, render :layout => false, :text => #users.to_json. How do I do that with respond_with or respond_to and inherited_resources?
Something like:
def index
#users = User.all
respond_with #users do |format|
format.json { render :layout => false, :text => #users.to_json }
end
end
Assuming you need JSON for an Ajax request
class UsersController < InheritedResources::Base
respond_to :html, :js, :xml, :json
def index
#users = User.all
respond_with(#users, :layout => !request.xhr? )
end
end
This seems like the cleanest solution to me.
Or to prevent you having to hardcode responses for each format in each action.
If you have no layouts for any of the actions in this controller it would be nicer to do:
class UsersController < InheritedResources::Base
respond_to :html, :js, :xml, :json
layout false
def index
#users = User.all
respond_with(#users)
end
end
I love #anthony's solution, but didn't work for me... I had to do:
respond_with(#users) do |format|
format.html { render :layout => !request.xhr? }
end
ps: posting an "answer" instead of a comment because stackoverflow comment formatting and "return key == submit" is infuriating!
I just found this out:
Even if it's JSON, Rails is still looking for a layout. As such, the only layout that it finds, in our case, is application.html.
Solution: Make a JSON layout.
So for instance, if you put an empty application.json.erb with a single = yield inside, next to your HTML one, the HTML layout is bettered by the JSON one. You can even use this to surround your JSON with metadata or things like that.
<%# app/views/layouts/application.json.erb %>
<%= yield %>
No other parameters needed, it automagically works!
Tested in Rails 4 only
class UsersController < InheritedResources::Base
layout -> (controller) { controller.request.xhr? ? false : 'application' }
end
You need to set this on your show action.
def show
render :layout => !request.xhr?
end
:)

Routing to new action (Rails)

I have an action in my PostsController named 'tagged', which I want to return all posts tagged with whatever term.
In my routes.rb I have the following (at the top):
map.connect 'posts/tagged/:tag', { :controller => 'posts', :action => 'tagged', :tag => /[a-z\-]+/ }
Yet navigating to posts/tagged/yes returns a RecordNotFound error:
Couldn't find Post without an ID
In my tagged.html.erb file, I'll eventually be using the find_tagged_with method from acts_as_taggable_on_steroids, but for now I've put a simple Post.find(:all) to eliminate the possibility of error.
It seems like my map.connect is being overridden, and the same error occurs even if I comment the whole of the routes.rb file out except my new line.
Ok, because you can comment out the default routes that means your problem is not in your routes at all. It's that your tagged action in the posts controller probably has something like this.
def tagged
#post = Post.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #post }
end
end
Or perhaps if you spent a little more time it looks like this:
def tagged
#post = Post.find(params[:tagged])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #post }
end
end
Where as what you want is this:
def tagged
#post = Post.find(:all, :conditions => {:tagged => params[:tagged]})
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #post }
end
end
Anyway, you should be writing functional tests for this stuff and not testing in the browser.
Why not add a RESTful route for the "tagged" action?
map.resources :posts, :member => { :tagged => :put }

Resources