0

我已经完成了 rails 入门教程,现在我正在尝试使用 devise gem 设置另一个项目进行身份验证。基本上,我想要的只是一个主页,一个用户可以填写表单以进行注册的页面,然后是一个进行身份验证的安全页面。

现在,当我导航到注册页面时,我遇到了问题。这是我收到的错误:

NoMethodError in Signup#index

Showing /Users/tomcaflisch/Sites/project2/app/views/signup/_form.html.erb where line #1 raised:

undefined method `users_path' for #<#<Class:0x007fa734b3e510>:0x007fa734b3a910>

Extracted source (around line #1):

1: <%= form_for(@user) do |f| %>
2:  <% if @user.errors.any? %>
3:  <div id="errorExplanation">
4:      <h2><%= pluralize(@user.errors.count, "error") %> prohibited this post from being saved: </h2>

注册控制器.rb:

class SignupController < ApplicationController

  def index
    @user = User.new
  end

  def new 
    @user = User.new

    respond_to do |format|
        format.html
    end
  end

end

user.rb 模型:

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

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

../views/signup_form.html.erb:

<%= form_for(@user) do |f| %>
    <% if @user.errors.any? %>
    <div id="errorExplanation">
        <h2><%= pluralize(@user.errors.count, "error") %> prohibited this post from being saved: </h2>
        <ul>
            <% @user.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
            <% end %>
        </ul>
    </div>
    <% end %>

    <div class="field">
        <%= f.label :email %><br />
        <%= f.text_field :email %>
    </div>

    <div class="field">
        <%= f.label :username %><br />
        <%= f.text_field :username %>
    </div>

    <div class="field">
        <%= f.label :password %><br />
        <%= f.text_field :password %>
    </div>

    <div class="actions">
        <%= f.submit %>
    </div>
<% end %>

路线.rb:

get "signup/index"
devise_for :users
get "index/index" 
root :to => 'index#index'

$ bundle exec rake 路由 | grep 用户:

    new_user_session GET    /users/sign_in(.:format)       {:action=>"new", :controller=>"devise/sessions"}
            user_session POST   /users/sign_in(.:format)       {:action=>"create", :controller=>"devise/sessions"}
    destroy_user_session DELETE /users/sign_out(.:format)      {:action=>"destroy", :controller=>"devise/sessions"}
           user_password POST   /users/password(.:format)      {:action=>"create", :controller=>"devise/passwords"}
       new_user_password GET    /users/password/new(.:format)  {:action=>"new", :controller=>"devise/passwords"}
      edit_user_password GET    /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
                         PUT    /users/password(.:format)      {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET    /users/cancel(.:format)        {:action=>"cancel", :controller=>"devise/registrations"}
       user_registration POST   /users(.:format)               {:action=>"create", :controller=>"devise/registrations"}
   new_user_registration GET    /users/sign_up(.:format)       {:action=>"new", :controller=>"devise/registrations"}
  edit_user_registration GET    /users/edit(.:format)          {:action=>"edit", :controller=>"devise/registrations"}
                         PUT    /users(.:format)               {:action=>"update", :controller=>"devise/registrations"}
                         DELETE /users(.:format)               {:action=>"destroy", :controller=>"devise/registrations"}
4

3 回答 3

1

看起来您正在混合两件事:设计提供它自己的注册/注册页面,并且您还创建了自己的页面。

有时这是合适的,但很多时候默认设计页面已经足够好——或者至少足够好开始了。

我建议您首先尝试使用它自己的页面来实现设计——暂时不要理会您的登录和注册页面。您看不到设计页面,因为它们隐藏在宝石中。

如果您想自定义它们,您可以按照以下步骤在您的项目中安装设计页面(以haml格式):

https://github.com/plataformatec/devise/wiki/How-To:-Create-Haml-and-Slim-Views

于 2012-05-10T02:53:58.963 回答
0

因为您正在创建自己的注册控制器,所以我相信设计路线不是您喜欢发布路线文件的路线,让我们看看我们是否可以理顺您的路线。

于 2012-05-09T20:11:38.617 回答
0

它在寻找Signup#index.. 它应该在寻找SignupController#index

在您想要拥有的 routes.rb 中

root :to => 'signup#index'

此外,undefined method users_path缺少获取用户的路线,或者很可能resources :users从您的 routes.rb 中丢失

对于新用户,您想要类似的form_for(User.new)东西UsersController#new

__

或者这样做

1)

为了views/users/new.html.erb你想要

<%= form_for(@user) do |f| %>
  <%= render 'fields', :f => f %>
  ....

有一条通往它的路线,比如get '/signup', :to => 'users#new'link_to 'Signup', signup_path

2)

对于用户控制器添加

 def new
    @user = User.new
    ..
  end
于 2012-05-09T20:15:13.493 回答