提交"创建"后发生错误帐号,无论我输入密码与否,错误信息如下:
No route matches [POST] "/users/create"
我构建了注册页面,这是new.html.erb
文件中的users
<div class="center hero-unit">
<% @page_title = "Sign up" %>
<h1>Sign Up</h1>
<%= form_for(:user, :url => {:controller => 'users', :action => 'create'}) do |f| %>
<p> First name:</br> <%= f.text_field :first %> </p>
<p> Last name:</br> <%= f.text_field :last %> </p>
<p> Email:</br> <%= f.text_field :email %> </p>
<p> Password:</br> <%= f.password_field :password %></p>
<p> Password Confirmation:</br> <%= f.password_field :password_confirmation %> </p>
<%= f.submit :Create, class:"btn btn-large btn-primary" %>
<%= link_to "Cancel", root_path, class:"btn btn-large btn-primary" %>
<% end %>
<% if @user.errors.any? %>
<ul class="Signup_Errors">
<% for message_error in @user.errors.full_messages %>
<li>* <%= message_error %></li>
<% end %>
</ul>
<% end %>
</div>
route.rb
是:
Rails.application.routes.draw do
get 'sessions/login'
get 'sessions/main'
get 'sessions/profile'
get 'sessions/setting'
get 'users/new'
get 'home/index'
root 'home#index'
match '/about', to: 'home#about', via: 'get'
match '/signup', to: 'users#new', via: 'get'
match ':controller(/:action(/:id))(.:format)', via: 'get'
match '/login', to: 'sessions#login', via: 'get'
match '/logout', to: 'sessions#logout', via: 'get'
match '/main', to: 'sessions#main', via: 'get'
match '/profile', to: 'sessions#profile', via: 'get'
match '/setting', to: 'sessions#setting', via: 'get'
end
而users_controller.rb
是:
class UsersController < ApplicationController
before_filter :save_login_state, :only => [:new, :create]
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
flash[:notice] = 'You signed up succefully!'
flash[:color] = 'valid'
else
flash[:notice] = 'Form is invalid.'
flash[:color] = 'invalid'
render 'new'
end
end
private
def user_params
params.require(:user).permit(:email, :password, :password_confirmation)
end
end
模型中的user.rb
是:
class User < ActiveRecord::Base
#attr_accessible :email, :password, :password_confirmation
attr_accessor :password
EMAIL_REGEX = /\b[A-Z0-9._%a-z\-]+@(?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,4}\z/
validates :username, :presence => true, :uniqueness => true, :length => { :in => 3..20 }
validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
validates :password, :confirmation => true #password_confirmation attr
validates_length_of :password, :in => 6..20, :on => :create
before_save :encrypt_password
after_save :clear_password
#attr_accessible :email, :password, :password_confirmation
def encrypt_password
if password.present?
self.salt = BCrypt::Engine.generate_salt
self.encrypted_password= BCrypt::Engine.hash_secret(password, salt)
end
end
def clear_password
self.password = nil
end
def self.authenticate(email="", login_password="")
if EMAIL_REGEX.match(email)
user = User.find_by_email(email)
end
if user && user.match_password(login_password)
return user
else
return false
end
end
def match_password(login_password="")
encrypted_password == BCrypt::Engine.hash_secret(login_password, salt)
end
#attr_accessible :email, :password, :password_confirmation
end
我还在模型中创建了password_hashing.rb
:
require 'digest/sha1'
encrypted_password= Digest::SHA1.hexdigest(password)
和password_hasing_with_salt.rb
salt= Digest::SHA1.hexdigest("# We add {email} as unique value and #{Time.now} as random value")
encrypted_password= Digest::SHA1.hexdigest("Adding #{salt} to {password}")
在我的migrate
中,事情是:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :first
t.string :last
t.string :email
t.string :encrypted_password
t.string :salt
t.timestamps
end
end
end
此外,错误消息包括:
Routes
Routes match in priority from top to bottom
Helper HTTP Verb Path Controller#Action
Path / Url
sessions_login_path GET /sessions/login(.:format) sessions#login
sessions_main_path GET /sessions/main(.:format) sessions#main
sessions_profile_path GET /sessions/profile(.:format) sessions#profile
sessions_setting_path GET /sessions/setting(.:format) sessions#setting
users_new_path GET /users/new(.:format) users#new
home_index_path GET /home/index(.:format) home#index
root_path GET / home#index
courses_path GET /courses(.:format) courses#index
POST /courses(.:format) courses#create
new_course_path GET /courses/new(.:format) courses#new
edit_course_path GET /courses/:id/edit(.:format) courses#edit
course_path GET /courses/:id(.:format) courses#show
PATCH /courses/:id(.:format) courses#update
PUT /courses/:id(.:format) courses#update
DELETE /courses/:id(.:format) courses#destroy
about_path GET /about(.:format) home#about
signup_path GET /signup(.:format) users#new
GET /:controller(/:action(/:id))(.:format) :controller#:action
login_path GET /login(.:format) sessions#login
logout_path GET /logout(.:format) sessions#logout
main_path GET /main(.:format) sessions#main
profile_path GET /profile(.:format) sessions#profile
setting_path GET /setting(.:format) sessions#setting
我该如何解决这个问题? 谢谢你的时间!
答案 0 :(得分:0)
form_for
接受几种类型的参数来涵盖不同的情况,但在您的情况下,您希望将@user
模型传递给它,因此form_for
知道如何设置正确的路径以及如何为text_field
底线,而不是
form_for(:user, :url => {:controller => 'users', :action => 'create'}) do
...
您必须使用
form_for(@user) do
...
此外,当@user
是新对象(即由User.new
在您的控制器中创建)时,form_for
会自动发布到您的create
操作。当@user
是现有记录(即通过User.find(params[:id]
从数据库加载)时,form_for
将PATCH到您的update
操作。它是自动的
第二点,您的问题中rake routes
的输出显示您的路线配置似乎不正确。你可以发布你的 route.rb 所以我们可以提供建议吗?
因此,您需要从路线中删除get 'users/new'
,并在您的routes.rb文件
resources :users
而且这也不是很干净:match ':controller(/:action(/:id))(.:format)', via: 'get'
我不知道你必须添加它的原因是什么,但我建议你找一个替代方案并将其删除。