ruby-on-rails - 添加要设计的值名称时出错

标签 ruby-on-rails ruby ruby-on-rails-3 devise

我在尝试将值名称添加到 devise 时遇到错误,我认为所有人都做了所有事情来使其工作,但它不起作用。

NoMethodError in Devise/registrations#new

undefined method `name' for #<User:0x00000002e4e038>

这是我的用户模型:

class User < ActiveRecord::Base

  include Gravtastic
  gravtastic :size => 165, :filetype => :png, :rating => 'R'

  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

  validates :email, :username, :presence => true, :uniqueness => true

  # Setup accessible (or protected) attributes for your model
  attr_accessible :name, :username, :email, :password, :password_confirmation, :remember_me

  has_many :topics, :dependent => :destroy
  has_many :posts, :dependent => :destroy

  def admin?
    true if self.username == 'admin'
  end

end

这是我的注册 View :

<h2>Sign up</h2>

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <p><%= f.label :name %><br />
  <%= f.text_field :name %></p>

  <p><%= f.label :email %><br />
  <%= f.email_field :email %></p>

  <p><%= f.label :password %><br />
  <%= f.password_field :password %></p>

  <p><%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation %></p>

  <p><%= f.submit "Sign up" %></p>
<% end %>

<%= render :partial => "devise/shared/links" %>

这是我的设备注册 View :

div class="module" style="padding:15px 25px 0px 25px;">

  <div style="float:right; width:100%; padding-left:30px; border-left:1px solid #e2e2e2;">
    <%= form_for("user", :as => resource_name, :url => registration_path("user")) do |f| %>
      <%= devise_error_messages! %>
      <p>
     Nome: <br />
        <%= f.text_field :name, :style => "font-size:2.0em", :autocomplete => "off" %><br />
      </p>
      <p>
     Usuario: <br />
        <%= f.text_field :username, :style => "font-size:2.0em", :autocomplete => "off" %><br />
      </p>
      <p>
        Email: (apenas emails @usp.br sao aceitos) <br />
        <%= f.text_field :email, :style => "font-size:2.0em", :autocomplete => "off" %><br />
      </p>
      <p>
        Senha:<br />
        <%= f.password_field :password, :style => "font-size:2.0em", :autocomplete => "off" %><br />
      </p>
      <p>
        Confirmar senha:<br />
        <%= f.password_field :password_confirmation, :style => "font-size:2.0em", :autocomplete => "off" %><br />
      </p>
      <p><%= f.submit "Criar" %></p>
    <% end %>
  </div>
  <div class="clear"></div>
</div>

我已经尝试过 rake db:migrate 但它仍然没有解决我的问题。抱歉,我还是个 Rails 菜鸟。

编辑: 这是我的迁移文件:

class DeviseCreateUsers < ActiveRecord::Migration
  def self.up
    create_table(:users) do |t|
      t.database_authenticatable :null => false
      t.recoverable
      t.rememberable
      t.trackable
      t.confirmable

      # t.encryptable
      # t.confirmable
      # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
      # t.token_authenticatable


      t.timestamps
    end

    add_index :users, :email,                :unique => true
    add_index :users, :reset_password_token, :unique => true
    # add_index :users, :confirmation_token,   :unique => true
    # add_index :users, :unlock_token,         :unique => true
    # add_index :users, :authentication_token, :unique => true
  end

  def self.down
    drop_table :users
  end
end

编辑 2: 这是我的 schema.rb

# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20131121000236) do

  create_table "categories", :force => true do |t|
    t.string   "title"
    t.boolean  "state",      :default => true
    t.integer  "position",   :default => 0
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "forums", :force => true do |t|
    t.string   "title"
    t.text     "description"
    t.boolean  "state",        :default => true
    t.integer  "topics_count", :default => 0
    t.integer  "posts_count",  :default => 0
    t.integer  "position",     :default => 0
    t.integer  "category_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "posts", :force => true do |t|
    t.text     "body"
    t.integer  "forum_id"
    t.integer  "topic_id"
    t.integer  "user_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "topics", :force => true do |t|
    t.string   "title"
    t.integer  "hits",        :default => 0
    t.boolean  "sticky",      :default => false
    t.boolean  "locked",      :default => false
    t.integer  "posts_count"
    t.integer  "forum_id"
    t.integer  "user_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "users", :force => true do |t|
    t.string   "email",                                 :default => "", :null => false
    t.string   "encrypted_password",     :limit => 128, :default => "", :null => false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",                         :default => 0
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.string   "confirmation_token"
    t.datetime "confirmed_at"
    t.datetime "confirmation_sent_at"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "topics_count",                          :default => 0
    t.integer  "posts_count",                           :default => 0
    t.string   "username"
  end

  add_index "users", ["email"], :name => "index_users_on_email", :unique => true
  add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true

end

最佳答案

至少从该迁移文件来看,您似乎缺少用户表的 name 列。

为用户表添加名称列。

rails g migration AddNameToUsers

class AddNameToUsers < ActiveRecord::Migration
  def change
    add_column :users, :name, :string
  end
end

运行rake migrate

为了安全起见重启服务器。


最终编辑。再也不会!!

在设计迁移文件中,取消注释

t.string   :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.string   :unconfirmed_email # Only if using reconfirmable

rake db:drop db:create db:migrate

重启服务器

关于ruby-on-rails - 添加要设计的值名称时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20109650/

相关文章:

ruby-on-rails - Ruby on Rails 4-回形针AWS Image URL错误吗?

ruby-on-rails - 并非所有页面都需要验证 Rails

ruby - Vagrant with Chef - 配置 VM 时出错

mysql - Ruby on Rails 3 可以与 Windows 7 上的 MySQL 一起使用吗?

html - 从路径字符串构建分层列表的最佳方法

jquery - 如何在 Rails find_by_sql 和 group_by 中传递变量

ruby-on-rails - 从 ActiveAdmin default_actions 中删除 'Show' 链接

ruby-on-rails - 当我以这种方式使用 FactoryGirl 时,为什么我的测试失败?

ruby-on-rails - 为什么是 :memory_store cache faster than :dalli_store for memcached? 我应该坚持使用 :memory_store?

python - Ruby 有哪些 Python 没有的,反之亦然?