404 Custom Error Pages in Rails 2.3 - ruby-on-rails

I'm overriding render_optional_error_file to render a custom page whenever there's an error. This works great if there's an error within the application, it renders "shared/error.erb" without problem.
My application controller has a few before_filters which are responsible for setting the colour scheme of the page, defining the menu items, and authenticating users. These also run when there's an application error, which is desired.
However when a 404 page for a file is rendered, none of these filters run, so I get a black page with no menu. Is there a way I can trigger these to run? And are there any reasons why I shouldn't do this?

When Rails encounters a missing file, it runs render_optional_error_file(404) with status 404 on the application controller, but skips all filters, presumably since an error has already occurred.
I added a method called run_filters to my application controller and then call that from render_optional_error_file:
def run_filters
#run filters or whatever
end
def render_optional_error_file(status)
run_filters
render "shared/error", :status => status
end
You can also test this behaviour on your development server by including the following in your application controller:
alias_method :rescue_action_locally, :rescue_action_in_public

Well, you may want to consider to create a special layout for your error pages. After all, it's a common practice to make error pages clearly distinguishable from normal ones.
But I don't understand why your before filters defined in application controller don't trigger. They really should trigger before any error happens in your actions.
Could you provide us with some code from your application controller?

Related

Do I need to generate a controller for a Ruby on Rails Controller call?

I'm setting a session variable to a user's geographical state. I have to use a session variable because I run code on the server specific to that user on page load and I need to know where they are. This code is set up to just update the session variable.
states_controller.rb
class StatesController < ApplicationController
def loc
session[:location] = params[:location]
end
end
routes.rb
post "states/loc" => "states#loc"
The code routes properly and the session variable is updated.
However, when the process is complete I get a 500 error in the console "Missing Template" in the views directory. I haven't seen any tutorials tell users to call the command "rails generate controller" and I'm in the unique situation where I can't call this command.
What possible side effect are there to ignoring this 500 error?
*I'm running an older version of ruby and rails.
What possible side effect are there to ignoring this 500 error?
Each request is crashing your rails server. Thats not good. Since it means that some cases it may have to restart after every failed request - that eats resources like Homer Simpson at a buffet.
Your app should not be raising uncaught exceptions that cause 500 errors - thats just decent professional practice.
So how do I fix it?
Simple, if you don't want the default behavior of rendering a view tell rails to do something else:
class StatesController < ApplicationController
def loc
session[:location] = params[:location]
head :created
end
end
This sends an empty response with the 201 - CREATED http header.
See Rails Guides - Layouts and Rendering in Rails

Getting error NoMethod error in RoR controller

I have been developing in Ruby on Rails for a while now so I am completely confused at why this is happening. I created a controller called ApiController as you can see below.
class ApiController < ActionController::Metal
def documentation
end
def request_manager
end
end
Here are the routes I set to setup the documentation view.
Rails.application.routes.draw do
root 'landing#home'
get 'api' => 'api#documentation'
end
There is an documentation.html.erb file in the api folder as well, but
No matter what I do I get the following error.
It makes no sense to me why it's not routing correctly and has me completely baffled.
As a side note, the landing controller works perfectly fine and routes to the home page with no problem so it's just this controller.
Actually I just figured it out. Turns out that ActionController::Metal turns off must functionality including rendering so when you attempt to make it render as a normal controller, it faults while loading and the error is somewhere in the class files of Ruby's main code. Hence me not being able to find what was going wrong exactly till I looked up more documentation for the Metal controller.
I was using ActionController::Metal for it's speed and just didn't realize how much it stripped away from functionality in the process.

Custom 404 action in 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

Ruby on Rails: having two xmlbuilder templates per action , one for errors one for regular output

What's the best way to handle having two templates (or should it be one, DRY?) for xml builder templates?
I'm building a web api with Rails and wanted to see an example of how to have a view that does regular output vs one that does error output. I've been using #obj.to_xml for a while, but my requirements have changed and require me building my own error templates.
do you typically have both views in one with a condition above for errors such as
app/views/myresource/create.xml.builder
unless #myobj.errors.empty?
// xml for errors here?
end
Can someone show me an xml.builder view example that has a case handled for when there is an error with an ActiveRecord object and or when it's successful?
// regular xml view
you can try something like this:
def create
....
return render :template=>'error' unless #myobj.errors.empty?
....
other code
end
in this case rails will render error template (it can be shared across all project) if you have an error, and render create builder if there is no error.

Why am I suddenly getting Missing Template errors with edge Rails (2.3)?

After freezing edge rails, all my controller examples are failing with
MissingTemplate errors.
e.g., "Missing template attachments/create.erb in view path app/views"
Trying to actually render the views gives me the same error.
I noticed I can fix most of them by using respond_to but I usually
never use it. I almost always only need to respond to one format in
one action so I omit respond_to and let Rails figure out which file to
render.
Does Rails suddenly require respond_to blocks in every action as of 2.3?
Just found this, which answers my question:
http://rails.lighthouseapp.com/projects/8994/tickets/1590-xhrs-require-explicit-respond_to

Resources