I already found a workaround for my problem, but I still don't get why this problem occurs. I think i am missing some things about how internals working.
I am calling *check_modify_rights* method from couple of controllers, and I want to have a variable with appropriate name (@post for post_controller instead of a generic one, like @object)
So after before_filter runs, I expect to have a @post instance variable with a Post model. But I get a new Post model (because of @post = Post.new), and when I checked object_id s in the method, they are different.
# post_controller
before_filter do |f|
@post = Post.new
f.check_modify_rights @post
end
# application_controller
def check_modify_rights(obj)
return redirect_to login_path, :notice => "Please login" unless @user
p obj.object_id
obj = obj.class.find(params[:id])
p obj.object_id
return if obj.user.id == @user.id or @user.is_admin?
redirect_to posts_path, :notice => "You don't have permission for this action"
end
end
@post = Post.newwill have a different id from a newly instantiated objectobj = obj.class.find(params[:id])