我遇到了一个奇怪的问题,其中一个模型的destroy动作的before_filter也正在另一个与one_to_one关联的模型的destroy动作上执行。我理解before_filter是继承的,但在这种情况下它只是一个关联,不应该被认为是继承,对吗?任何有关如何调试此问题的说明或提示都将非常感激。
以下是两个模型,user和hostprofile,它们具有一对一的关联。
class User < ActiveRecord::Base
attr_accessible :firstname, :lastname, :email, :password, :password_confirmation
has_secure_password
...
has_one :hostprofile, dependent: :destroy
end
class Hostprofile < ActiveRecord::Base
belongs_to :user
...
end
我将admin_user过滤器应用于用户控制器中的destroy操作:
class UsersController < ApplicationController
before_filter :signed_in_user, only: [:edit, :update, :destroy]
before_filter :admin_user, only: :destroy
...
end
admin_user函数在sessionhelper中定义,该函数包含在应用程序控制器中。过滤器未在hostprofile控制器中设置为destroy。 hostprofile使用以下命令创建:
@hostprofile = current_user.build_hostprofile(params[:hostprofile])
销毁主机的链接是:
<%= link_to 'delete host', hostprofile_path(@user.hostprofile), method: "delete"%>
当点击链接时,应用程序被重定向到root_path,因为它正在命中admin_user函数,如果我在userscontroller中注释掉过滤器,则destroy可以正常工作。这是日志:
在2012-04-28开始为127.0.0.1开始删除“/ hostprofiles / 104” 18:20:49 +0000由HostprofilesController处理#dutroy为HTML
参数: { “authenticity_token”=&gt; “中TSFC + BYPi1f2v / M4bd9C + P36u9YJ1C7hBpZbs4GdVzA =”, “id”=&gt;“104”}用户加载(0.2ms)SELECT“users”。* FROM“users”WHERE “users”。“remember_token”='igPv_PXRFNExWFKlPFnYVQ'LIMIT 1重定向 到http://0.0.0.0:3000/过滤器链停止为:admin_user呈现或 重定向完成302在2ms(ActiveRecord:0.2ms)缓存中找到: [GET /] miss
知道为什么会这样吗?
class HostprofilesController < UsersController
before_filter :not_host_already, only: [:new, :create]
before_filter :is_user_self, only: :destroy
...
def destroy
if @hostprofile.destroy
current_user.toggle!(:is_host)
sign_in current_user
else
flash[:error] = "sorry, host profile failed to delete"
end
redirect_to current_user
end
private
def is_user_self
@hostprofile = Hostprofile.find(params[:id])
unless @hostprofile.user_id == current_user.id
redirect_to root_path
end
end
由于