错误添加值名称设计错误 [英] Error adding value names to devise

查看:129
本文介绍了错误添加值名称设计错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试添加值名称设置错误,我认为所有的事情都使它成功,但它不起作用。

Im getting an error trying to add the value name to devise, I think all did all things to make it work, but it doesnt work.

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

这是我的注册视图:

<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" %>

这里是我的设计注册视图:

And here is my devise registration 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,但仍然无法解决我的问题。对不起,我仍然是一个noob在rails。

I already tryied rake db:migrate but it still doesnt solve my problem. Sorry, Im still a noob on rails.

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

Here is my migration file:

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

EDIT 2: Here is my 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


推荐答案

至少从该迁移文件,您似乎缺少用户表的名称列。

At least from that migration file, it seems like you are missing name column for users table.

添加名称列对于用户表。

Add the name column for users table.

rails g migration AddNameToUsers

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

运行 rake migrate

重启服务器只是为了安全。

Restart server just to be safe.

最终编辑。

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

In the devise migration file, uncomment

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

重启服务器

这篇关于错误添加值名称设计错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆