Custom 404 action in Rails - ruby-on-rails

Default Rails will render 404.html, then it thinks an error 404 is appropriate. However, I want make it by custom page. Please suggest the proper way do it.

This answer was surprisingly hard to find.
If you want a static page, edit 404.html. Done.
If you want a dynamic page, then:
in config/routes.rb:
match '*not_found', to: 'errors#error_404' unless Rails.application.config.consider_all_requests_local
Comment out the unless clause to test the 404 page on local dev machine.
Generate a bunch of files:
rails generate controller errors error_404
Edit views/errors/error_404.html.erb to customize.
This works for Rails 3.1 and 3.2.2.

You could use render_404 monkey patching.
Or, you could set a default route at the bottom of your routes file that goes to an action that returns a 404 status code.
Ideally if you do not want any custom code in your 404 you can just edit the static 404.html, which is easiest.

Depending on what you need, our gem might help: http://github.com/hedgeyedev/snow

Related

What are the routes I need to set up to preview emails using Rails 4.1 ActionMailer::Preview?

class UserPreview < ActionMailer::Preview
# Accessible from http://localhost:3000/rails/mailers/notifier/welcome_email
def welcome_email
UserMailer.welcome_email(User.first)
end
end
I have this simple mailer preview using Ruby on Rails 4.1.
If I comment out, all of the routes in my routes.rb file and leave only this, the mailer preview works:
MyTestApp::Application.routes.draw do
end
So obviously one of my rights is getting used before the default Rails one for mailer previews.
What do I need to type into the routes rb file?
I know this is an old question, but figured I'd post an answer anyway.
I'm guessing you have a route similar to this near the end of your routes.rb file:
match '/:controller(/:action(/:id))'
That is a 'catch all' route. The rails code appends the mailer preview routes to the end of the routes, so they are never reached due to the 'catch all' route.
It sounds like the 'catch all' route may be retired in rails 5.0? It is probably a good idea to review your routes so you don't need a 'catch all'. Here is a link to the issue where someone mentions the 'catch all' is being retired at some point: https://github.com/rails/rails/issues/15600
So, here is the fix. Use at your own risk!
Insert the mailer routes before your 'catch all'.
get '/rails/mailers' => "rails/mailers#index"
get '/rails/mailers/*path' => "rails/mailers#preview"
That will allow your mailers to work and your 'catch all' will continue working. Now, this is a complete hack which should only be used until you're able to fix the root issue, which is eliminating the need for the 'catch all' route.
I did find the following in the issues list for rails, which looks like has been accepted and merged. Not sure what version it is in, but it seems like they have updated the mailer preview code to prepend the routes instead of appending them.
https://github.com/rails/rails/pull/17896/files
Good luck!

Ruby on Rails match routing error

I'm trying to make a Rails application that serves simple static HTML pages. I followed Mikel's tutorial here (it involves making a Pages controller and setting up some routing) but I keep getting an error message.
I made a app/views/site/pages/_about.html.erb file to contain my About page. After starting the rails server, I try to go to http://localhost:3000/about/ but it gives me a Routing Error because I have an "uninitialized constant Site."
My project is uploaded to GitHub if you want to take a look at the code.
Edit: here's my config/routes.rb file:
NINAgallery::Application.routes.draw do
match ':page_name' => 'site/pages#show'
end
And here's the important part of my app/controllers/pages_controller.rb file:
class PagesController < ApplicationController
layout 'site'
def show
#page_name = params[:page_name].to_s.gsub(/\W/,'')
unless partial_exists?(#page_name)
render 'missing', :status => 404
end
end
# extra code for handling 404 errors goes here
end
site/pages#show means the show action in Site::PagesController
You either need to put your controller in the namespace your routes imply or change the route
The last line in the PagesController is this:
ValidPartials = Site::PagesController.find_partials
That means that the PagesController is contained in a Site module. But there is no Site module in your app.
I think simple removing Site:: should fix the problem:
ValidPartials = PagesController.find_partials
Plus the route:
match ':page_name' => 'pages#show'
Your application is called NINAgallery.
Replace Site in pages_controller.rb line 27 by NINAgallery.
PS:
I just took a peek at the so-called tutorial. You are taking really really really bad habits.
Some resources to take very good basics:
http://guides.rubyonrails.org/
http://api.rubyonrails.org/
If you like tutorials: http://ruby.railstutorial.org/
And there are plenty of books about rails. All good.
Besides the namespace problem, you also needed to add the 'app' Gem to the Gemfile, as explained in the tutorial.
I don't know why you removed the caching of the static pages in your working code. I made a pull request with the app working and maintaining the cache problem. If another person is interested, the code is here
Also ryan bates has a tutorial called "Semi static pages" that does something similar. I would encourage you to follow his solutions because there are very rarely mistaken.

error on rails 3

Hi this is ROR beginner's question.
I have creat controller.rb and view hello.rhtml following the tutorial, but when I try to open localhost:3000/say/hello, it come up with with error: No route matches [GET] "/say/hello"
could any one advice please?
Well you need to setup a route for that in your config/routes.rb file.
For first try i would say use a script generator, enter on the command line as being in the project library > rails g controller helloworld index. This will create a route for itself, and a controller file.
After this script runs, there should be a line in your config/routes.rb
Cloud::Application.routes.draw do
get "helloworld/index"
end
Then you need to enter localhost:3000/helloworld/index in your browser url bar. Then ( as default ) rails will render the view located in app/views/helloworld/index.*. If you want to change this behaviour, go to the helloworld controller.
For more info there is a useful guide: ROUTING GUIDES
You need to define a route definition so that the URL you are requesting gets mapped to an action in the controller you have created which would render hello.rhtml. Say your controller name is says_controller.rb (thats how Rails gives the filename). In that if you define and action hello which would by default render hello.rhtml, then the fallback routes which are defined in the routes.rb file at the end would make a request to say/hello to look for the say_controller and hello action, thus rendering hello.rhtml.
For detailed help you can refer to the Rails Guides. There is a lot of helpful material and it is explained very well.
I started developing RoR recently and the best practise I got was the tutorial # rails for zombies and video's # railscasts. I suggest you watch some / make some and you get a general idea how to get started :)
-edit- on this issue: You're trying to render the hello view from the say controller.
since routing is handled by default on :controller/:action, do you have a action called hello in say? No action means no route means no view rendered.
class SayController < ApplicationController
def hello
#do nothing or add some code
logger.debug "I'm in the say controller, hello action!"
end
end
This should get it to render the hello file. You might want to take a look at restful actions / crud though, rails uses those by default.

Blank page when navigating to http://localhost:3000/say/hello

I'm new to Ruby on Rails and am doing my first tutorial and am running the latest version of rails 3 and ruby 1.9.2. After creating my controller and navigating to http://localhost:3000/say/hello I'm receiving a blank page. I do see the Welcome to Rails message when I just go to http://localhost:3000. I've done some Google searches and people have similar problems but there is no clear fix. I've never really worked with MVC before so the concept of routing is fairly new to me.
Below is my controller:
class SayController < ApplicationController
def hello
end
def goodbye
end
end
My view:
<h1>Say hello to Rails!</h1>
You should delete the public/index.html file as that will mess with your routing and display by default. Have you set up your routes already, and what is the exact location and filename of the template?
You will need something like in your config/routes.rb file to correctly route that url to your template/view:
match '/say/hello' => 'say#hello'
First delete the index.html file from your public folder. Then, go to the app/views and check the views for the say controller. You should have a hello.html.erb.
The answer to your particular question was answered already by Bitterzoet, but I thought you might want some alternative learning resources.
I'm not sure which tutorial you're starting with, but I find it odd that they're not using RESTful routes. You can find out what routes you have set up at the moment by going to the console and typing "rake routes". If you would like a different tutorial, I recommend the one here:
http://www.wiki.devchix.com/index.php?title=Rails_3_Curriculum
I'd also recommend http://railsforzombies.org/ as a good first-time rails experience.
A fun general line to add to config/routes is:
match ':controller(/:action(/:id(.:format)))'
While developing, this will allow you to display the controller/action in address bar for ALL controller/action/id.format etc.
Like Bitterzote wrote, if controller is "say" and action is "hello", http://localhost:3000/say/hello .
If you use controller "say" and action "move", http://localhost:3000/say/move .
I've found this route to be very useful during development, but change this if you launch your application! (Rails warns: "Note: This route will make all actions in every controller accessible via GET requests.")

rails: displaying a user friendly message for a routing error in rails

i am working in rails 2.3 on mac osx leopard.
every time i type a url that does not exist in a rails application i get the following error
Routing Error
No route matches "/whatever_i_typed" with {:method=>:get}
this is find for development, but i was wondering how i can make sure users see a friendlier 'oops! not found' page. i was thinking about doing a begin...rescue block but i didn't know where to put it, not did i know the error code (i.e ActiveRecord::RecordNotFound)
thanks!
yuval
This error will never appear in production. Instead, users will see the public/404.html page.
To try this out on your localhost, put passenger/mongrel into production mode. Override the local_request? method on your ApplicationController like so:
class ApplicationController
def local_request?
false
end
end
If you'd like to experiment with dynamic behavior you can check out the rescue_from class method on ActionController.
How about
map.connect '*url', :controller => "not_found"
as a last routes.rb entry? I think it should do the trick, shouldn't it?
I found the below url helpful for Rails 3.0.. users.
http://techoctave.com/c7/posts/36-rails-3-0-rescue-from-routing-error-solution

Resources