【问题标题】:Devise: Routes messed up设计:路线混乱
【发布时间】:2014-03-28 03:12:25
【问题描述】:

我刚刚安装了设计,但是他们给我的默认路线和视图都搞砸了。我在尝试访问视图时遇到大量错误。我正在使用 rails 3.2.13 和最新的设计 (1.1.rc0)

以下是我在使用以下生成的用户模型时遇到的一些错误(使用 rails generate devise User

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :lockable, :timeoutable and :activatable
  devise :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation
  # attr_accessible :title, :body
end

运行迁移后,这已添加到我的路由文件中

devise_for :users

当我尝试访问注册页面时出现此错误

undefined method `user_registration_path' for #<#<Class:0x007f8079d87c68>:0x007f8079badc30>

这是发生错误的视图

<h2>Sign up</h2>

<%= form_for(resource_name, resource, :url => registration_path(resource_name)) do |f| %>
  <%= f.error_messages %>
  <p><%= f.label :email %></p>
  <p><%= f.text_field :email %></p>

  <p><%= f.label :password %></p>
  <p><%= f.password_field :password %></p>

  <p><%= f.label :password_confirmation %></p>
  <p><%= f.password_field :password_confirmation %></p>

  <p><%= f.submit "Sign up" %></p>
<% end %>

<%= render :partial => "devise/shared/links" %>

很明显为什么会失败,因为这是我的 rake 路由的返回

    new_user_session GET    /users/sign_in(.:format)              devise/sessions#new
        user_session POST   /users/sign_in(.:format)              devise/sessions#create
destroy_user_session GET    /users/sign_out(.:format)             devise/sessions#destroy
            password POST   /users/password(.:format)             devise/passwords#create {:name_prefix=>:user}
        new_password GET    /users/password/new(.:format)         devise/passwords#new {:name_prefix=>:user}
       edit_password GET    /users/password/edit(.:format)        devise/passwords#edit {:name_prefix=>:user}
                     PUT    /users/password(.:format)             devise/passwords#update {:name_prefix=>:user}
                     POST   /users/registration(.:format)         devise/registrations#create {:name_prefix=>"user_registration"}
                 new GET    /users/registration/sign_up(.:format) devise/registrations#new {:name_prefix=>"user_registration"}
                edit GET    /users/registration/edit(.:format)    devise/registrations#edit {:name_prefix=>"user_registration"}
                     PUT    /users/registration(.:format)         devise/registrations#update {:name_prefix=>"user_registration"}
                     DELETE /users/registration(.:format)         devise/registrations#destroy {:name_prefix=>"user_registration"}
        confirmation POST   /users/confirmation(.:format)         devise/confirmations#create {:name_prefix=>:user}
    new_confirmation GET    /users/confirmation/new(.:format)     devise/confirmations#new {:name_prefix=>:user}
                     GET    /users/confirmation(.:format)         devise/confirmations#show {:name_prefix=>:user}

显然正确的路线是new_path

这是我在尝试访问登录视图时遇到的另一个错误

wrong number of arguments (3 for 2)

查看

<h2>Sign in</h2>

<%= form_for(resource_name, resource, :url => session_path(resource_name)) do |f| %>
  <p><%= f.label :email %></p>
  <p><%= f.text_field :email %></p>

  <p><%= f.label :password %></p>
  <p><%= f.password_field :password %></p>

  <% if devise_mapping.rememberable? -%>
    <p><%= f.check_box :remember_me %> <%= f.label :remember_me %></p>
  <% end -%>

  <p><%= f.submit "Sign in" %></p>
<% end %>

<%= render :partial => "devise/shared/links" %>

我以前使用过 devise,但我不记得遇到所有这些错误。到底是怎么回事?我不认为它是开箱即用的。

【问题讨论】:

  • 您能分享一下您是如何在 routes.rb 文件中定义路由的吗?
  • 当然,我现在就编辑
  • 您安装了哪个版本的设备?你说最新,然后提到1.1.rc0。最新版本的设计是3.2.4
  • 我安装设计所做的只是将gem 'devise' 添加到我的gemfile 中。然后当我运行 bundle install Installing devise (1.1.rc0) 时它就这样做了
  • 让我们在聊天chat.stackoverflow.com/rooms/48530/ror 上讨论这个问题

标签: ruby-on-rails ruby ruby-on-rails-3 devise


【解决方案1】:

要解决wrong number of arguments (3 for 2) 错误,请将视图中的form_for 更新为

<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>  

路由应该在routes.rb中定义如下:

devise_for :users

devise 版本存在一些问题。我建议您使用特定版本的 Devise 更新Gemfile,如下所示:

gem 'devise', '~> 3.2.4'      ## instead of gem 'devise'

然后运行bundle install

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    • 2013-01-26
    • 2023-04-08
    • 1970-01-01
    相关资源
    最近更新 更多