Ruby on rails 通过覆盖控制器动作和视图自定义设计Gem
在我的项目中,Ruby on rails 通过覆盖控制器动作和视图自定义设计Gem,ruby-on-rails,ruby,ruby-on-rails-3,devise,Ruby On Rails,Ruby,Ruby On Rails 3,Devise,在我的项目中,注册,登录表单将显示在主页中,而不是像Desive那样显示在用户/注册和用户/登录中。当用户没有在email&password字段中正确输入信息时,我们只会再次显示主页,而不是返回到users/sign\u up 我试图找到如何自定义路径和显示视图,但我做不到。 因此,我通过覆盖`registrations\u controller和注册表单来定制它。以下是我的自定义代码: RegistrationsController:只需将用响应\u更改为`重定向\u至 class Regis
注册
,登录
表单将显示在主页中,而不是像Desive那样显示在用户/注册
和用户/登录
中。当用户没有在email&password字段中正确输入信息时,我们只会再次显示主页,而不是返回到users/sign\u up
我试图找到如何自定义路径和显示视图,但我做不到。
因此,我通过覆盖`registrations\u controller
和注册
表单来定制它。以下是我的自定义代码:
RegistrationsController:只需将用
响应\u更改为`重定向\u至
class RegistrationsController < Devise::RegistrationsController
def create
build_resource(sign_up_params)
if resource.save
yield resource if block_given?
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_flashing_format?
sign_up(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_flashing_format?
expire_data_after_sign_in!
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
redirect_to root_path, error: "Please check again"
end
end
end
在*注册表*(designe/registrations/new.html.haml)中:
仅将资源的表单更改为:资源名称…
为User。新建和:User
%h3 Sign up
= form_for User.new, :as => :user, :url => registration_path(:user) do |f|
.form-group
= f.email_field :email, autofocus: true, placeholder: 'Email', class: 'form-control'
.form-group
= f.password_field :password, placeholder: 'Password', class: 'form-control'
.privacy
By clicking Join Now, you argeed User Argeement InterestWeShare 's Privacy Policy and Cookie Policy.
= f.submit "Join me in!", class: 'btn btn-primary'
我认为我的代码不好,因为我只是在控制器中做了一点更改,但我必须自己重写操作(尽管只是复制代码-不适合最佳实践)。在视图中,我不知道如何更好地自定义资源和资源名称。请帮忙。这可能会帮到你:那么,问题出在哪里?您是否特别注意到了任何错误。有什么你不明白的吗。很久以前我就黑客设计了宝石,可能会对你有所帮助。或者问具体的问题。因为我对Desive gem的黑客攻击太多,我认为gem缺乏灵活性,可能不是一个好方法。
%h3 Sign up
= form_for User.new, :as => :user, :url => registration_path(:user) do |f|
.form-group
= f.email_field :email, autofocus: true, placeholder: 'Email', class: 'form-control'
.form-group
= f.password_field :password, placeholder: 'Password', class: 'form-control'
.privacy
By clicking Join Now, you argeed User Argeement InterestWeShare 's Privacy Policy and Cookie Policy.
= f.submit "Join me in!", class: 'btn btn-primary'