Ruby on rails 3 设计创业板,增加建设者和改变你
我在rails 3.1.1应用程序上使用“设计”gem来创建用户和登录,但我需要为profile添加字段@user.build\u profile,但我不知道在哪里添加这个,我还需要在登录后将默认重定向更改为。您可以像通常通过迁移一样将所需的字段添加到模型中。如果您通过Ruby on rails 3 设计创业板,增加建设者和改变你,ruby-on-rails-3,devise,ruby-on-rails-3.1,Ruby On Rails 3,Devise,Ruby On Rails 3.1,我在rails 3.1.1应用程序上使用“设计”gem来创建用户和登录,但我需要为profile添加字段@user.build\u profile,但我不知道在哪里添加这个,我还需要在登录后将默认重定向更改为。您可以像通常通过迁移一样将所需的字段添加到模型中。如果您通过rails generate designe:views使用默认的设计视图,那么您需要将字段添加到designe中注册视图内的新建和编辑视图中 至于在登录后更改默认重定向,我将首先阅读。您只需将以下内容添加到应用程序控制器: p
rails generate designe:views
使用默认的设计视图,那么您需要将字段添加到designe中注册视图内的新建
和编辑
视图中
至于在登录后更改默认重定向,我将首先阅读。您只需将以下内容添加到应用程序控制器:
protected
def stored_location_for(resource)
nil
end
def after_sign_in_path_for(resource)
#path to which you wish to redirect
end
end
这将允许您自定义该路线。您还可以查看Desive中的注册控制器,以查看可以覆盖的其他方法
编辑:
覆盖控制器:
protected
def stored_location_for(resource)
nil
end
def after_sign_in_path_for(resource)
#path to which you wish to redirect
end
end
您可以创建自己的控制器,该控制器继承自designe::RegistrationsController
。从这里开始,您只需要指定需要重写的方法,而不是每个方法。例如:
def create
build_resource
resource.build_profile
if resource.save
set_flash_message :notice, :signed_up
redirect_to after_sign_up_path_for(resource)
else
clean_up_passwords(resource)
render_with_scope :new
end
end
然后在路径中的_签名_之后,为存储位置和添加上述行。你还必须在你的
routes.rb中告诉designe使用你的新控制器,designe\u for:users,:controllers=>{:registrations=>'your\u controller\u name}
我在我的博客上有一个例子,完整的源代码在Github上。或者你只是覆盖build\u资源
def build_resource *a, &b
super
resource.build_profile
end
我需要使用嵌套属性从另一个模型添加域,以确保您的用户模型接受\u嵌套属性\u:profile
。然后,您需要覆盖Desive registrations controller,以便对@user.profile.build
执行自己的创建操作,这显然是在用户和配置文件之间创建适当的关系。这就是问题所在。如何覆盖控制器i添加到我的答案中,向您展示覆盖控制器的基本知识。因此,我应该执行类似于def new resource=build_resource({})的操作,使用导航(resource){render_with_scope:new}resource.build_profile end响应_