List Info

Thread: Force displaying 404 page in production environment




Force displaying 404 page in production environment
user name
2008-01-01 04:28:11
Hi,

I'm having trouble displaying the 404 page when an exception
occurs.
In my controller I have

user
= User.find_by_username(params[:username])

and in view I have

<%= user.username's page %>

If params[:username] is invalid or does not exist,
NoMethodError
exception is displayed, hence 500 page in production
environment.
However, I want this error to be 400 Page Not Found error
instead.
How can I accomplish that?  I don't want to use redirect_to
'/
404.html' because I want the URL to remain the same.  Any
ideas?

Thanks in advance.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Ruby on Rails" group.
To post to this group, send email to rubyonrailsgooglegroups.com
To unsubscribe from this group, send email to
rubyonrails-unsubscribegooglegroups.com
For more options, visit this group at http://gro
ups.google.com/group/rubyonrails
-~----------~----~----~----~------~----~------~--~---


Re: Force displaying 404 page in production environment
user name
2008-01-01 11:03:36
How about something like this in the controller:

def controller_method
  user = User.find_by_username(params[:username])
  username = user.username # raises NoMethodError when
user.nil?
rescue NoMethodError
  render :file => "/path/to/404.html"
end

James

On Jan 1, 2008 5:28 AM, Will <willycheunggmail.com> wrote:
>
> Hi,
>
> I'm having trouble displaying the 404 page when an
exception occurs.
> In my controller I have
>
> user = User.find_by_username(params[:username])
>
> and in view I have
>
> <%= user.username's page %>
>
> If params[:username] is invalid or does not exist,
NoMethodError
> exception is displayed, hence 500 page in production
environment.
> However, I want this error to be 400 Page Not Found
error instead.
> How can I accomplish that?  I don't want to use
redirect_to '/
> 404.html' because I want the URL to remain the same. 
Any ideas?
>
> Thanks in advance.
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Ruby on Rails" group.
To post to this group, send email to rubyonrailsgooglegroups.com
To unsubscribe from this group, send email to
rubyonrails-unsubscribegooglegroups.com
For more options, visit this group at http://gro
ups.google.com/group/rubyonrails
-~----------~----~----~----~------~----~------~--~---


Re: Force displaying 404 page in production environment
user name
2008-01-02 10:38:18
Did my suggestion work for you?

James

On Jan 1, 5:28 am, Will <willyche...gmail.com> wrote:
> Hi,
>
> I'm having trouble displaying the 404 page when an
exception occurs.
> In my controller I have
>
> user = User.find_by_username(params[:username])
>
> and in view I have
>
> <%= user.username's page %>
>
> If params[:username] is invalid or does not exist,
NoMethodError
> exception is displayed, hence 500 page in production
environment.
> However, I want this error to be 400 Page Not Found
error instead.
> How can I accomplish that?  I don't want to use
redirect_to '/
> 404.html' because I want the URL to remain the same.
 Any ideas?
>
> Thanks in advance.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Ruby on Rails" group.
To post to this group, send email to rubyonrailsgooglegroups.com
To unsubscribe from this group, send email to
rubyonrails-unsubscribegooglegroups.com
For more options, visit this group at http://gro
ups.google.com/group/rubyonrails
-~----------~----~----~----~------~----~------~--~---


[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )