2012-04-15 66 views
1

当您尝试定制其行为时,我在理解设计如何工作时遇到麻烦。设计仅适用于STI机型吗?

我有两个不同的模式来处理:学会合作者,并在报名表必须在他们两个同样的观点。
因此,我必须通过编写处理两种模型的新控制器来覆盖“设计注册控制器创建方法”

这里来的痛苦。
从这里“Devise form within a different controller”和here,我知道我有定义这些新的帮手,使设计作品:

module ContentHelper 
    def resource_name 
    :user 
    end 

    def resource 
    @resource ||= User.new 
    end 

    def devise_mapping 
    @devise_mapping ||= Devise.mappings[:user] 
    end 
end 

而创造的方法,我想重写是这样的:

def create 
build_resource 

if resource.save 
    if resource.active_for_authentication? 
    set_flash_message :notice, :signed_up if is_navigational_format? 
    sign_in(resource_name, resource) 
    respond_with resource, :location => after_sign_up_path_for(resource) 
    else 
    set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format? 
    expire_session_data_after_sign_in! 
    respond_with resource, :location => after_inactive_sign_up_path_for(resource) 
    end 
else 
    clean_up_passwords resource 
    respond_with resource 
end 

结束

我不知道如何使它的工作原理没有妥协的监督功能。 (build_resource)。任何建议?我找不到任何解决方案没有STI使用!

回答

0

这听起来像你将有一个复杂的模型设置,可能会困扰你的轨道。如果您需要两种不同的“用户”模型,也许您仍然可以拥有用户模型,然后还可以使用Society和Collaborator模型中的每个“has_one:user”。这样在注册时创建用户记录,以及社会或协作者方法(无论选择哪一个)。这种方式设计(和你的所有认证)只是链接到用户模型,它保持简单。

一般来说,如果你发现自己在与谷物作斗争,重新评估你正在做的事情是一个好主意。除非你正在做一些开创性的事情,否则很可能事情不会那么困难。

0

可以定义色器件的所有意见的变量,例如

@type = :society 

def resource_name 
    @type 
end 

,那么你可以使用原来的控制器和视图,只是将其添加在AplicationHelper

PD:对于User.new,您可以在eval条件下使用字符串,如

@res = "Society" 

then

@resource ||= eval(@res).new 
相关问题