Ruby rails undefined方法' name' @用户

时间:2015-05-10 00:56:54

标签: ruby-on-rails ruby

我在尝试显示注册表单时收到错误:

undefined method `name' for `#<User id: nil, created_at: nil,
updated_at: nil, email: nil>
  

提取的来源(第8行):

     

6 7 8 9 10 11

         

在users / new.html.erb

<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for(@user) do |f| %>
      <%= f.label :name %>
      <%= f.text_field :name %>

      <%= f.label :email %>
      <%= f.email_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-primary" %>
    <% end %>
  </div>
</div>

如果我注释掉那一行,那就行了。我在我的模型中定义了名称字段:

分贝/迁移/ .... create_users.rb

class CreateStructure < ActiveRecord::Migration
  def change
    def up
        drop_table :users
        create_table :users do |t|
            t.integer :id
            t.string :name
            t.string :email
            t.string :password
        end

        create_table :model_forums do |t|
            t.integer :id
            t.string :name
        end
    end
  end
end

但它并没有创建我要求的字段,只有旧字段如created_at和updated_at。

1 个答案:

答案 0 :(得分:0)

migation文件有问题,请参阅Active Record Migrations

total_use_count

然后运行class CreateStructure < ActiveRecord::Migration def up create_table :users do |t| t.integer :id t.string :name t.string :email t.string :password end #It is not recommended to create two tables in one migration file. create_table :model_forums do |t| t.integer :id t.string :name end end def down drop_table :users end end 以删除旧用户表,重新运行rake db:rollback