我在安装了devise
后用这个命令生成了视图rails generate devise:views
我通过
覆盖注册控制器class RegistrationsController < Devise::RegistrationsController
def sign_up2
end
end
并使用
更新routes.rb devise_for :users, :controllers => { :registrations => "registrations" }
我希望看到
的新路线/视图 /users/sign_up2
但是我没有看到它
这里是设计路线
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PATCH /users(.:format) registrations#update
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
但我想要一个新的观点和路线
更新: 加载视图时出现问题
First argument in form cannot contain nil or be empty
在这一行
<%= form_for(resource, :as => resource_name,:html => { :class => "form-horizontal col-sm-12",:role=>"form"}, :url => user_registration_path(resource_name)) do |f| %>
答案 0 :(得分:4)
调用devise_scope
块并在以下位置声明您的自定义路线:
devise_for :users, :controllers => { :registrations => "registrations" }
devise_scope :user do
get "users/sign_up2"=> "users/registrations#sign_up2", :as => "sign_up2_registration"
end
文档中Configuring routes部分提供了devise_scope
的以下说明:
如果您需要更深入的自定义,例如除了“/ users / sign_in”之外还允许“/ sign_in”,您需要做的就是正常创建路由并将它们包装在devise_scope块中路由器
以前,Devise允许将自定义路由作为块传递给devise_for
,但this behavior has been deprecated。
<强>更新强>:
要解决First argument in form cannot contain nil or be empty
错误,您需要确保自定义sign_up2
操作正确设置resource
变量。假设您想模仿registrations/new
操作,您可以执行类似于以下操作的操作:
def sign_up2
build_resource({})
respond_with self.resource
end
这可确保您视图中的resource
变量不是nil
,并且不会抛出您当前正在目睹的异常。
或者,根据您尝试显示的行为,您可以在自定义控制器操作中设置自己的实例变量,然后将其作为资源传递到form_for
标记:
# app/controllers/users/registrations_controller.rb
def sign_up_2
@new_registrant = Registrant.new
end
# app/views/users/sign_up2.html.erb
<%= form_for(@new_registrant, :as => resource_name,:html => { :class => "form-horizontal col-sm-12",:role=>"form"}, :url => user_registration_path(resource_name)) do |f| %>
然而,如果你遵循这种方法,你应该考虑为什么你需要将它推到Devise中。默认情况下,设计通过resource
函数指定build_resource
变量。如果您要覆盖/绕过此函数,您应该考虑从Devise中抽象出整个功能,因为您完全绕过了它的默认行为。