Rails添加了新的设计视图 [英] Rails add new view to devise

查看:92
本文介绍了Rails添加了新的设计视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我安装设备后,我已经用此命令生成了视图。

I have generated views with this command after I installed devise

rails generate devise:views

,我通过

class RegistrationsController < Devise::RegistrationsController

  def sign_up2

  end
end

并且更新的route.rb与

And updated routes.rb with

  devise_for :users, :controllers => { :registrations => "registrations" }  

我预计会看到

  /users/sign_up2

但我看不到它
这里的路线设计

but I don't see it And here routes for devise

        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

但我想要一个新的视图和路由

But I would like a new view and route

更新:加载视图时
出现

Update: issue when I load view

First argument in form cannot contain nil or be empty

p>

in this line

<%= form_for(resource, :as => resource_name,:html => { :class => "form-horizontal col-sm-12",:role=>"form"}, :url => user_registration_path(resource_name)) do |f| %>


推荐答案

调用 devise_scope 阻止并声明您的自定义路线:

Invoke a devise_scope block and declare your custom route within:

devise_for :users, :controllers => { :registrations => "registrations" }  
devise_scope :user do
  get "users/sign_up2"=> "users/registrations#sign_up2", :as => "sign_up2_registration"
end

配置路径提供了 devise_scope 的以下说明:


如果您需要更深入的定制,例如除/ users / sign_in之外还允许/ sign_in,您需要做的就是创建路由正常,并将它们包装在路由器中的devise_scope块中

If you have the need for more deep customization, for instance to also allow "/sign_in" besides "/users/sign_in", all you need to do is to create your routes normally and wrap them in a devise_scope block in the router

以前,Devise允许将自定义路由作为块传递到code> devise_for ,但此行为已被弃用

Previously, Devise allowed custom routes to be passed as a block to devise_for, but this behavior has been deprecated.

更新

要解决表单中的参数不能包含nil或为空错误,您需要确保您的自定义 sign_up2 操作正确设置e 资源变量。假设你想模仿注册/新的动作,你可以做一些类似于以下的事情:

To address the First argument in form cannot contain nil or be empty error, you need to ensure that your custom sign_up2 action is properly setting the resource variable. Assuming you want to mimic the registrations/new action, you can do something akin to the following:

def sign_up2
    build_resource({})
    respond_with self.resource
end

这样可以确保视图中的资源变量不是 nil 不会抛出你正在目睹的异常。

This ensures that the resource variable in your view is not nil and will not throw the exception you're currently witnessing.

另外,根据你想要表现的行为,您可以在自定义控制器动作中设置自己的实例变量,然后传递它作为您的 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 函数分配资源变量。如果你要覆盖/绕过这个函数,你应该考虑将这个完整的功能从Devise抽象出来,因为你完全绕过它的默认行为。

However, if you follow this approach, you should consider why you need to roll this into Devise. Devise, by default, assigns the resource variable via the build_resource function. If you're going to override/bypass this function, you should consider abstracting this entire functionality out of Devise since you're totally circumventing its default behavior.

这篇关于Rails添加了新的设计视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆