I am trying to use the new Rails 2.0 macro : rescue_from
class PostsController < ApplicationController
rescue_from ActiveRecord::RecordNotFound, :with => :deny_access
...
def show
@post = Post.find_by_id(params[:id])
raise ActiveRecord::RecordNotFound if @post.nil? #illegal access
.....
end
def deny_access
respond_to do |format|
format.html
end
end
but the exception is not raised ... did I miss something ?
thanks
kad
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On Dec 22, 2007, at 3:56 PM, Kad Kerforn wrote:> class PostsController < ApplicationController > rescue_from ActiveRecord::RecordNotFound, :with => :deny_access > ... > def show > @post = Post.find_by_id(params[:id]) > raise ActiveRecord::RecordNotFound if @post.nil? #illegal accessThat can be rewritten in a single line, since AR::Base.find precisely raises ActiveRecord::RecordNotFound if the record is not found: @post = Post.find(params[:id])> def deny_access > respond_to do |format| > format.html > end > end > > > but the exception is not raised ... did I miss something ?It should work, please debug this a little more. -- fxn --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Xavier Noria wrote:> On Dec 22, 2007, at 3:56 PM, Kad Kerforn wrote: > >> class PostsController < ApplicationController >> rescue_from ActiveRecord::RecordNotFound, :with => :deny_access >> ... >> def show >> @post = Post.find_by_id(params[:id]) >> raise ActiveRecord::RecordNotFound if @post.nil? #illegal access > > That can be rewritten in a single line, since AR::Base.find precisely > raises ActiveRecord::RecordNotFound if the record is not found: > > @post = Post.find(params[:id]) > >> def deny_access >> respond_to do |format| >> format.html >> end >> end >> >> >> but the exception is not raised ... did I miss something ? > > It should work, please debug this a little more. > > -- fxnthanks, it should work but it doesn''t ... NoMethodError (You have a nil object when you didn''t expect it! The error occurred while evaluating nil.post_type): /app/controllers/posts_controller.rb:181:in `show'' def show @post = Post.find_by_id(params[:id]) @type = @post.post_type the @type is evaluated... despote the fact that @post = nil the exception should be raised but it''s not... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Dec 22, 2007, at 5:44 PM, Kad Kerforn wrote:> thanks, it should work but it doesn''t ... > > NoMethodError (You have a nil object when you didn''t expect it! > The error occurred while evaluating nil.post_type): > /app/controllers/posts_controller.rb:181:in `show'' > > def show > @post = Post.find_by_id(params[:id]) > @type = @post.post_type > > > the @type is evaluated... despote the fact that @post = nil > the exception should be raised but it''s not...Note that is neither your original code, nor the one I suggested. Dynamic finders do no raise ActiveRecord::RecordNotFound, use AR::Base.find instead. -- fxn --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
@post = Post.find_by_id(params[:id]) Yeah...that doesn''t raise the ActiveRecord::RecordNotFound. The special "by" finders just return nil. You need to use the base find as the guy above suggested. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Xavier Noria wrote:> On Dec 22, 2007, at 5:44 PM, Kad Kerforn wrote: > >> >> the @type is evaluated... despote the fact that @post = nil >> the exception should be raised but it''s not... > > Note that is neither your original code, nor the one I suggested. > > Dynamic finders do no raise ActiveRecord::RecordNotFound, use > AR::Base.find instead. > > -- fxnOK.. thanks sorry, I did not use exceptions until now... so I did not notice it I just tested in teh console :>> Post.find(999)ActiveRecord::RecordNotFound: Couldn''t find Post with ID=999 from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.0.1/lib/active_record/base.rb:1194:in `find_one'' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.0.1/lib/active_record/base.rb:1177:in `find_from_ids'' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.0.1/lib/active_record/base.rb:453:in `find'' from /Users/yves/Sites/presdemoi.net/TRY-yves-1.0/vendor/plugins/geokit/lib/geo_kit/acts_as_mappable.rb:109:in `find'' from (irb):1>> Post.find_by_id(999)=> nil -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Nathan Esquenazi wrote:> @post = Post.find_by_id(params[:id]) > > Yeah...that doesn''t raise the ActiveRecord::RecordNotFound. The special > "by" finders just return nil. You need to use the base find as the guy > above suggested.Thanks Nathan ;-)) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Dec 23, 5:40 am, Kad Kerforn <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Xavier Noria wrote: > > On Dec 22, 2007, at 5:44 PM, Kad Kerforn wrote: > > >> the @type is evaluated... despote the fact that @post = nil > >> the exception should be raised but it''s not... > > > Note that is neither your original code, nor the one I suggested. > > > Dynamic finders do no raise ActiveRecord::RecordNotFound, use > > AR::Base.find instead. > > > -- fxn > > OK.. thanks > sorry, I did not use exceptions until now... so I did not notice it > I just tested in teh console : > > >> Post.find(999) > > ActiveRecord::RecordNotFound: Couldn''t find Post with ID=999 > from > /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.0.1/lib/active_record/base .rb:1194:in > `find_one'' > from > /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.0.1/lib/active_record/base .rb:1177:in > `find_from_ids'' > from > /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.0.1/lib/active_record/base .rb:453:in > `find'' > from > /Users/yves/Sites/presdemoi.net/TRY-yves-1.0/vendor/plugins/geokit/lib/geo_ kit/acts_as_mappable.rb:109:in > `find'' > from (irb):1 > > >> Post.find_by_id(999) > > => nil > > -- > Posted viahttp://www.ruby-forum.com/.if you want find_by to raise, can overwrite your method: def find_by_something(something) super || raise(ActiveRecord::RecordNotFound, ''your own error message'') end --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
if you want find_by to raise, can overwrite your class method:
def self.find_by_something(something)
super || raise(ActiveRecord::RecordNotFound, ''your own error
message'')
end
On Dec 23, 5:40 am, Kad Kerforn
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> Xavier Noria wrote:
> > On Dec 22, 2007, at 5:44 PM, Kad Kerforn wrote:
>
> >> the @type is evaluated... despote the fact that @post = nil
> >> the exception should be raised but it''s not...
>
> > Note that is neither your original code, nor the one I suggested.
>
> > Dynamic finders do no raise ActiveRecord::RecordNotFound, use
> > AR::Base.find instead.
>
> > -- fxn
>
> OK.. thanks
> sorry, I did not use exceptions until now... so I did not notice it
> I just tested in teh console :
>
> >> Post.find(999)
>
> ActiveRecord::RecordNotFound: Couldn''t find Post with ID=999
> from
> /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.0.1/lib/active_record/base
.rb:1194:in
> `find_one''
> from
> /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.0.1/lib/active_record/base
.rb:1177:in
> `find_from_ids''
> from
> /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.0.1/lib/active_record/base
.rb:453:in
> `find''
> from
> /Users/yves/Sites/presdemoi.net/TRY-yves-1.0/vendor/plugins/geokit/lib/geo_
kit/acts_as_mappable.rb:109:in
> `find''
> from (irb):1
>
> >> Post.find_by_id(999)
>
> => nil
>
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---