【问题标题】:Rails add new view to deviseRails 添加新视图以进行设计
【发布时间】:2013-12-15 20:57:48
【问题描述】:

我在安装 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| %>

【问题讨论】:

  • 您要添加什么视图和路线?
  • 明白。看看我的回答。

标签: ruby-on-rails ruby devise


【解决方案1】:

调用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 中。默认情况下,Devise 通过build_resource 函数分配resource 变量。如果您要覆盖/绕过此功能,则应考虑将整个功能从 Devise 中抽象出来,因为您完全规避了它的默认行为。

【讨论】:

  • 非常感谢,我现在可以加载视图,但有一点问题,您能帮忙吗?
  • 你的问题是什么?另外,如果这个答案已经解决了您最初的问题,请接受它是正确的。
  • 查看我的更新。如果它完全解决了您的问题,请接受此答案为正确的。
猜你喜欢
  • 2013-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-31
  • 1970-01-01
相关资源
最近更新 更多