【问题标题】:has one route error message "No route matches [POST] "/users/create""有一条路由错误消息“没有路由匹配 [POST]“/users/create””
【发布时间】:2014-09-04 06:49:32
【问题描述】:

提交“创建”账号后出现错误,无论我是否输入密码,错误信息如下:

No route matches [POST] "/users/create"

我建立了注册页面,这是我在users文件中的new.html.erb

<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

请问我该如何解决这个问题? 感谢您的宝贵时间!

【问题讨论】:

  • 你在 routes.rb 中有什么?
  • 不应该去/create - 应该只是去/POST动词^_^
  • 请也向我们展示您的控制器。
  • 问题很可能是因为您在get ':controller(/:action(/:id))(.:format)' 下方有resources :users。此语句应始终在您的路线中排在最后(更不用说,它不应该真正使用)。但是,您的代码还有更多问题,请发布您的控制器代码,以便我们帮助您修复它们。
  • 您有一个新操作users_new_path GET /users/new(.:format) users#new,但没有create 操作。要么将创建别名添加到新的(如果它是相同的操作),或者在视图中将create 替换为new。或者只是添加与new 不同的create 操作。

标签: ruby-on-rails ruby routes passwords registration


【解决方案1】:

form_for 接受多种类型的参数以涵盖不同的场景,但在您的情况下,您希望将您的 @user 模型传递给它,因此 form_for 知道如何设置正确的路径以及如何给出正确的路径值到你的text_fields

底线,而不是

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 将修补到您的update 操作。这是自动的


第二点,您的问题中rake routes 的输出显示您的路由配置似乎不正确。您可以发布您的 route.rb 以便我们提供建议吗?

所以你需要从你的路由中删除get 'users/new',并在你的routes.rb文件的顶部添加resources :users

而且这也不是很干净:match ':controller(/:action(/:id))(.:format)', via: 'get' 我不知道您必须添加它的原因是什么,但我建议您找到替代方法并将其删除。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多