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
On Dec 22, 2007, at 3:56 PM, Kad K. 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.
– fxn
Xavier N. wrote:
On Dec 22, 2007, at 3:56 PM, Kad K. 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.
– fxn
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…
On Dec 22, 2007, at 5:44 PM, Kad K. 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
@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.
Xavier N. wrote:
On Dec 22, 2007, at 5:44 PM, Kad K. 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:infind_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:infind’
from (irb):1
Post.find_by_id(999)
=> nil
On Dec 23, 5:40 am, Kad K. [email protected]
wrote:
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.0.1/lib/active_record/base .rb:1194:in
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
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 K. [email protected]