gpt4 book ai didi

ruby-on-rails - Rails 添加新 View 设计

转载 作者:数据小太阳 更新时间:2023-10-29 08:38:22 26 4
gpt4 key购买 nike

我在安装 devise 后用这个命令生成了 View

rails generate devise:views

我通过

覆盖注册 Controller
class RegistrationsController < Devise::RegistrationsController

def sign_up2

end
end

并用

更新了 routes.rb
  devise_for :users, :controllers => { :registrations => "registrations" }  

我希望看到一个新的路线/ View

  /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

但我想要一个新的 View 和路线

更新:加载 View 时出现问题

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| %>

最佳答案

调用一个 devise_scope block 并在其中声明您的自定义路由:

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 的解释:

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 允许将自定义路由作为 block 传递给 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

这确保您 View 中的 resource 变量不是 nil 并且不会抛出您当前看到的异常。

或者,根据您试图表现的行为,您可以在自定义 Controller 操作中设置您自己的实例变量,然后将其作为资源传递给您的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 中。默认情况下,设计通过 build_resource 函数分配 resource 变量。如果您要覆盖/绕过此函数,您应该考虑从 Devise 中抽象出整个功能,因为您完全避开了它的默认行为。

关于ruby-on-rails - Rails 添加新 View 设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20599714/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com