Rails Raise Http Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings rails render status 404 and policies of this site About Us Learn more about Stack Overflow
Rails Raise 500
the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation rails render forbidden Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it rails forbidden exception only takes a minute: Sign up How to return correct HTTP error codes from Ruby on Rails application up vote 22 down vote favorite 4 I have RoR 3.0 web application which is acting as an OAuth API provider. Now, in API I'd like to return correct HTTP error codes to the API consumer. How do I do this? Here
Rails 403 Forbidden
is example: def destroy_oauth @item = Item.find(params[:id]) if(!@item.nil? && @item.user_id == current_user.id) @item.destroy respond_to do |format| format.js format.xml end else raise ActionController::RoutingError.new('Forbidden') end end So, in case of error I'm trying to return Forbidden 403 code. Still, when running this I'm getting always 404 Not Found returned. How do I return the correct code? Or is this somehow webserver configurable thing? ruby-on-rails ruby http share|improve this question asked Feb 3 '12 at 14:29 Alexander Savin 2,72021334 add a comment| 5 Answers 5 active oldest votes up vote 25 down vote accepted You should render page with correct status. render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false) share|improve this answer answered Feb 3 '12 at 14:33 Sandip Ransing 4,04212238 Should I do this also for OAuth API xml responses? –Alexander Savin Feb 3 '12 at 14:59 2 As a side-note here, specifying the .html extension like this is deprecated (see discussion at github.com/rails/rails/issues/7288), so we should use render(:file => File.join(Rails.root, 'public/403'), :formats => [:html], :status => 403, :layout => false) (note the way I've sp
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss rails raise 400 the workings and policies of this site About Us Learn more rails render error json about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack
Rails 403 Page
Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping http://stackoverflow.com/questions/9130191/how-to-return-correct-http-error-codes-from-ruby-on-rails-application each other. Join them; it only takes a minute: Sign up How to redirect to a 404 in Rails? up vote 367 down vote favorite 159 I'd like to 'fake' a 404 page in Rails. In PHP, I would just send a header with the error code as such: header("HTTP/1.0 404 Not Found"); How is that done with http://stackoverflow.com/questions/2385799/how-to-redirect-to-a-404-in-rails Rails? ruby-on-rails http-status-code-404 share|improve this question edited Jul 8 '12 at 18:12 Andrew Marshall 63.9k12134153 asked Mar 5 '10 at 9:52 Yuval Karmi 9,6022697151 add a comment| 9 Answers 9 active oldest votes up vote 864 down vote accepted Don't render 404 yourself, there's no reason to; Rails has this functionality built in already. If you want to show a 404 page, create a render_404 method (or not_found as I called it) in ApplicationController like this: def not_found raise ActionController::RoutingError.new('Not Found') end Rails also handles AbstractController::ActionNotFound, and ActiveRecord::RecordNotFound the same way. This does two things better: 1) It uses Rails' built in rescue_from handler to render the 404 page, and 2) it interrupts the execution of your code, letting you do nice things like: user = User.find_by_email(params[:email]) or not_found user.do_something! without having to write ugly conditional statements. As a bonus, it's also super easy to handle in tests. For example, in an rspec integration test: # RSpec 1 lambda { visit '/something/you/want/to/404' }.should raise_error(ActionController::RoutingError) # RSpec 2+ expect { get '/something
if you believe this is an error. Please include your IP address in your email.