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