在添加新注册表单时,在用户#new中获取NoMethodError

时间:2014-02-05 16:21:23

标签: ruby-on-rails ruby-on-rails-4

尝试为新注册添加新表单

<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>

<div class="row">
  <div class="span6 offset3">
    <%= form_for(@user) do |f| %>

      <%= f.label :name %>
      <%= f.text_field :name %>

      <%= f.label :email %>
      <%= f.text_field :email %>

      <%= f.label :password %>
      <%= f.password_field :password %>

      <%= f.label :password_confirmation, "Confirmation" %>
      <%= f.password_field :password_confirmation %>

      <%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
    <% end %>
  </div>
</div>

但是当我运行该程序时,请单击新的注册按钮 在用户中获取错误NoMethodError#new ??

<div class="span6 offset3">
<%= form_for(@user) do |f| %>  <--error os here
<%= f.label :name %>
<%= f.text_field :name %> 

这里要求的是包含user_controller.rb和user.rb的额外代码 user_controller.rb

class UsersController < ApplicationController


  def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end
end

user.rb

class User < ActiveRecord::Base
  before_save { self.email = email.downcase }
  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence:   true,
                    format:     { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  has_secure_password
  validates :password, length: { minimum: 6 }
end
当我尝试进入注册按钮时

跟踪错误

Started GET "/signup" for 127.0.0.1 at 2014-02-05 16:53:21 +0000
Processing by UsersController#new as HTML
  Rendered users/new.html.erb within layouts/application (35.1ms)
Completed 500 Internal Server Error in 39ms

    ActionView::Template::Error (undefined method `users_path' for #<#<Class:0x007f9db9decb60>:0x007f9dbc862098>):
        3: 
        4: <div class="row">
        5:   <div class="span6 offset3">
        6:     <%= form_for(@user) do |f| %>
        7: 
        8:       <%= f.label :name %>
        9:       <%= f.text_field :name %>
      app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb__4582285946471664350_70157724747320'

  Rendered /usr/local/rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.0ms)
  Rendered /usr/local/rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
  Rendered /usr/local/rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (13.1ms)

2 个答案:

答案 0 :(得分:0)

您是否定义了路线?

尝试设置

resources :users
您的routes.rb文件

GL&amp; HF。

答案 1 :(得分:0)

添加到您的config / routes.rb

resources :users

您需要在create

中添加UsersController方法
def create
  @user = User.new(params[:user])
  if @user.save
    redirect_to users_path
  else
    render :new
  end
end