rails xml.builder missing template error - ruby-on-rails

Error:
Template is missing
Missing template miscellaneous/sitemap, application/sitemap with {:locale=>[:en], :formats=>[:xml], :handlers=>[:erb, :builder]}. Searched in: * "/Users/yliu/Google Drive/ruby projects/Blog/lenswish/app/views" * "/usr/local/rvm/gems/ruby-1.9.3-p194/bundler/gems/twitter-bootstrap-rails-4b8a511e6518/app/views" * "/usr/local/rvm/gems/ruby-1.9.3-p194/gems/devise-3.1.0/app/views"
rake routes:
GET /sitemap.xml(.:format) miscellaneous#sitemap {:format=>"xml"}
routes.rb:
get "sitemap.xml", :to => "miscellaneous#sitemap", defaults: { format: "xml" }
controller:
class MiscellaneousController < ApplicationController
def sitemap
#card_templates = CardTemplate.all
respond_to do |format|
format.xml
end
end
end
Template position:
app/views/miscellaneous/sitemap.xml.builder
content in template sitemap.xml.builder:
# Sitemaps 0.9 XML format: http://www.sitemaps.org/protocol.php
xml.instruct!
xml.urlset :xmlns => 'http://www.sitemaps.org/schemas/sitemap/0.9' do
xml.url do
xml.loc root_url
xml.changefreq 'daily'
xml.lastmod #card_templates.first.updated_at.iso8601
xml.priority '0.8'
end
end
I already checked file permission issues. Still not working. Anyone help please. Thanks in advance.

This looks wrong to me:
GET /sitemap.xml(.:format) miscellaneous#sitemap {:format=>"xml"}
Shouldn't it be like this?
GET /sitemap(.:format) miscellaneous#sitemap {:format=>"xml"}
I would change your route to:
get "sitemap", :to => "miscellaneous#sitemap"
Your controller code should look about the same
class MiscellaneousController < ApplicationController
def sitemap
#card_templates = CardTemplate.all
respond_to do |format|
format.xml
end
end
end

This turns out to be an IDE issue, the file name I saw from the textmate ui differs from what I saw from the terminal. Fixed after I renamed the file.

Related

Work with default routes but custom routes give uninitialized constant Api::V1::UserController

I know this questions is asked several times but I checked those answers but nothing seems to fix my issue.
users_controller.rb on path app/controllers/api/v1/
module Api
module V1
class UsersController < ApplicationController
def create_user
#users = User.new(user_params)
if #users.save
render json: { status: '201', message: 'User created successfully' }
else
render json: { status: '400', message: 'Invalid user info', data: #users.errors }
end
end
def user_params
params.permit(:first_name, :last_name, :email, :password)
end
end
end
end
routes.rb
namespace 'api' do
namespace 'v1' do
post "user/createuser", to: "user#create_user"
end
end
What I have tried:
Checked the directory structure it is as mentioned
Restarted the server and checked
Folder name: all simple controllers > api > v1
But this works fine when I changed the routes.rb
post "user/createuser", to: "user#create_user"
to resource :users
and
def create_user
to def create
Why things does not work when I define custom routes instead of using default routes? How to get this work with custom routes
Due to Rails conventions I believe you need to update your route with
post "user/createuser", to: "users#create"
instead of
post "user/createuser", to: "user#create_user"

UrlGenerationError: No route matches, missing required keys and link_to

I'm having issues with my static pages strategy on rails.
I have a controller that handles static pages. Basically, it reads templates in the app/views/static directory and render as asked. Like so:
class StaticController < ApplicationController
def show
templ = File.join(params[:controller], params[:page])
puts params, templ
render templ
rescue ActionView::MissingTemplate => e
if e.message =~ %r{Missing template #{templ}}
raise ActionController::RoutingError, 'Not Found'
else
raise e
end
end
end
These are my routes:
Rails.application.routes.draw do
root 'static#show', page: 'home'
get ':page', to: 'static#show', as: :static, constraints: { page: /a-zA-Z\-_\/+/ }
end
The root route works fine, I am able to access the view just fine. I get no errors.
Now, on my header partial, I have this, edited for simplicity/relevance:
<%= link_to('Home', static_path(:home)) %>
There is no other ruby code in the partial or the main template. I'm not sure what I'm doing wrong. The error just does NOT make sense.
ActionController::UrlGenerationError - No route matches {:action=>"show", :controller=>"static", :format=>nil, :page=>:home} missing required keys: [:page]
Where exactly is the required key missing from? I don't have any objects other or models.
Now this works just fine:
<%= link_to('Home', controller: 'static', action: 'show', page: 'home') %>
So how do I make static_path work like that?
Thanks.
I think there's a problem with the regexp your using for the constraint, I'd suggest dropping it from the route definition, as you already check if template is present in the controller.
Also, you should check out high_voltage gem which handles creating static pages quite nicely.
My first guess would be:
<%= link_to 'Home', static_path("home") %> #-> try passing a string, rather than symbol
This should allow you to reference the required path
--
class StaticController < ApplicationController
def show
templ = File.join(params[:controller], params[:page])
puts params, templ
render templ
rescue ActionView::MissingTemplate => e
if e.message =~ %r{Missing template #{templ}}
raise ActionController::RoutingError, 'Not Found'
else
raise e
end
end
end
I would personally make this a lot simpler:
class StaticController < ApplicationController
def show
render "pages#{params[:page]}
end
end

rails routes "uninitialized constant CoursesController"

I'm working in a rails project and I add the following route:
get '/courses/:invitation_code' => "courses#find_invitation"
On my controller I have the following action:
def find_invitation
#course = Course.where(["invitation_code = ?", params[:invitation_code]])
if !#course.empty?
respond_to do |format|
format.html
format.json { render json: #course, success: true, error: false }
end
end
end
But, when I try to go to localhost:3000/course/demo123 I get the following error:
ActionController::RoutingError at /courses/demo123 uninitialized
constant CoursesController
And I don't understand why. This a project with devise and a new in this project, so I don;t know if I have to do something else, in order to make this action work.
Thanks in advance for your help.
You must specify controller's namespace in your route.
Try changing:
get '/courses/:invitation_code' => "courses#find_invitation"
to:
get '/courses/:invitation_code' => 'admin/courses#find_invitation'
Use this:
match '/courses/invitation_code' => "courses#find_invitation"
convert your root as follows
match '/courses/:invitation_code' => "courses#find_invitation"
The url must be with the segment id as follows
localhost:3000/courses/demo123

Rails missing template

I get this when I try and run my view:
Missing template application/login with {:formats=>[:html], :locale=>[:en], :handlers=>[:coffee, :erb, :builder]}. Searched in: * "/home/carladessi/Goods In Final/app/views"
in my controller I have:
def login
# respond_to do |format|
# format.html
end
and in my routes I have:
match "/login/", :controller => 'application', :action => 'login'
I'm guessing I need to put something else in the controller I just don't know what.. sorry if this is a really blatant question!
Restarted the server and it works fine !
It is not really conventional Rails to render views from the application_controller.
However, what is happening is Rails is looking for an actual template or view to be here:
RAILS_ROOT/app/views/application/login.html.erb
What you can do is add/create that template at the above path. Or you can redirect to another controller (which exists and does render an actual template).

rails, allow exception in route globbing

i was wondering if i could add an exception to route globbing in rails. in my routes.rb i have
unless params[:not_found].eql? 'admin_data'
match '*not_found', to: 'errors#error_404'
end
im trying to enforce custom error pages, except when a user visits
myapp.heroku.com/admin_data
it doesn't seem like fetching :not_found as a param works. is there a way to add an exception in routes.rb?
if it helps, in my errors_controller i have..
def error_404
#not_found_path = params[:not_found]
end
thank you
update.
i tried doing just
puts :not_found
puts %{not_found}
but doesn't seem to work either hmmm...im trying to see if i can retrieve the params from the user
It would be much more convenient to define allowed routes in routes.rb and add exception handling in application controller for routing error:
class ApplicationController < ActionController::Base
rescue_from ActionController::RoutingError, :with => :render_not_found
private
def render_not_found
render_error_page_for(404)
end
def render_error_page_for(code)
respond_to do |format|
format.html { render :file => "#{Rails.root}/public/#{code}.html", :status => code, :layout => false }
end
end
i do catch my exception handling in my application controller but unfortunately for admin_data, i don't explicitly set it in routes.rb. it gets configured somewhere in the gem with namespace or something (im not really sure)
but on a positive note... i finally fixed it! i changed my glob and did...
match '*not_found', to: 'errors#error_404', :constraints => {:subdomain => "!(admin_data.)"}
to ignore everything which uses admin_data.

Resources