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