0
這裏是新手入門。困惑,爲什麼我得到一個NoMethodError在用戶#新的未定義的方法`users_path」爲#<#:0x00000101a5e4a0>用戶中的NoMethodError#new使用rails創建簡單登錄
模型類
class User < ActiveRecord::Base
before_save { self.username = username.downcase }
VALID_INPUTS = /[[:ascii:]]+/
validates :username, presence: true, length: { maximum: 128 },
format: { with: VALID_INPUTS }, uniqueness: true
has_secure_password
validates :password, presence: true, length: { maximum: 128 },
format: { with: VALID_INPUTS }
validates :password_confirmation, presence: true
end
用戶控制器類
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(user_params)
end
private
def user_params
params.require(:user).permit(:username, :password, :password_confirmation)
end
end
新視角(new.html.erb)
<h1>Users#new</h1>
<%= form_for(@user) do |u| %>
<%= u.label :username %>
<%= u.text_field :username %>
<%= u.label :password %>
<%= u.password_field :password %>
<%= u.label :password_confirmation, "Confirmation" %>
<%= u.password_field :password_confirmation %>
<%= u.submit "Create Account", class: "btn btn-large btn-primary" %>
<% end %>
錯誤是在視圖中,指定cally我想我在一個nil對象上調用form_for()方法。 Hartl教程中使用了完全相同的語法,但它工作正常,所以我不確定要做什麼。提前致謝。如果這是愚蠢的道歉。
的form_for,當有未保存的對象調用,將尋找users_path。你有沒有在你的config/routes.rb文件中設置這個路由? –
你可以添加你的routes.rb文件嗎? –
請同時添加堆棧跟蹤 – bridiver