试图学习rails并继续遇到noMethodError

时间:2012-05-09 20:04:00

标签: ruby-on-rails

我已经完成了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>

signup_controller.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

../视图/ 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 %>

routes.rb中:

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

$ bundle exec rake routes | 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"}

3 个答案:

答案 0 :(得分:1)

看起来你混合了两件事:设计提供了自己的注册/注册页面,你也创建了自己的注册页面。

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

我建议你首先尝试使用它自己的页面实现设计 - 暂时不要单独登录和注册页面。您没有看到设计页面,因为它们隐藏在gem中。

如果要自定义它们,可以按照以下步骤获取项目中安装的设计页面(采用haml格式):

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

在你的routes.rb中

你想拥有

root :to => 'signup#index'

此外,undefined method users_path错过了从路由中获取用户或很可能遗失resources :users的路线.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)

对于UsersController添加

 def new
    @user = User.new
    ..
  end