How to raise a 403 error?

1,562 views
Skip to first unread message

Satsou Sa

unread,
Jun 11, 2010, 6:59:38 PM6/11/10
to rubyonra...@googlegroups.com
Hello all,

I am working on Rails 3, and I would like to return a forbidden error
message from a controller.

I had trying something like that:

def my_action
respond_to do |format|
format.html { redirect_to(login_path, :warning =>
I18n.t('.forbidden'), :status => :forbidden) }
format.xml { render :xml => I18n.t('.forbidden'), :status =>
:forbidden }
end
end

But it is not want I need, because I don't want to redirect to a
particular page, such as login. I just want to return a standard 403
error (with raise maybe). Is that possible?

Thank you for your help.
--
Posted via http://www.ruby-forum.com/.

Mickael Gerard

unread,
Jun 12, 2010, 4:36:33 AM6/12/10
to rubyonra...@googlegroups.com
Hello,

I did not check if it was still there in Rails 3, but in rails 2, 
ActionController::Base#render accepts a :nothing options used to render empty text :
usage : 

render :nothing => true, :status => whatever

And you have a short cut method for "only headers" response ActionController::Base#head
usage :

format.xml { head :forbidden }

Mickael


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.


Satsou Sa

unread,
Jun 12, 2010, 4:45:04 AM6/12/10
to rubyonra...@googlegroups.com
Mickael Gerard wrote:
>
> render :nothing => true, :status => whatever
>
> And you have a short cut method for "only headers"
> response ActionController::Base#head
> usage :
>
> format.xml { head :forbidden }
>
> Mickael

You solved my question. Many thanks.

Reply all
Reply to author
Forward
0 new messages