【问题标题】:Rails (One Month Rails) NoMethodError in PinsController#create - can't add new pinRails(一个月 Rails)PinsController#create 中的 NoMethodError - 无法添加新引脚
【发布时间】:2013-09-01 19:05:17
【问题描述】:

当我尝试添加新的 pin 时,我不能。相反,我收到此错误:

NoMethodError in PinsController#create

undefined method `name' for #<pin:0x000001011b9638>

应用程序跟踪 |框架跟踪 |全程跟踪

app/controllers/pins_controller.rb:48:in `block in create'
app/controllers/pins_controller.rb:47:in `create'

请求

参数:

{"utf8"=>"✓",
"authenticity_token"=>"n9E2nob/KBzu20PEzYoQWXnibAUR5TH6iPWNd66383k=",
"pin"=>{"description"=>"stea"},
"commit"=>"Create Pin"}

pins_controller.rb

def create

@pin = current_user.pins.new(params[:pin])

respond_to do |format|
  if @pin.save
    format.html { redirect_to @pin, notice: 'Pin was successfully created.' }
    format.json { render json: @pin, status: :created, location: @pin }
  else
    format.html { render action: "new" }
    format.json { render json: @pin.errors, status: :unprocessable_entity }
  end
 end
end

app/model/user.rb

class User < ActiveRecord::Base

devise :database_authenticatable, :registerable,
     :rememberable, :trackable, :validatable

attr_accessible :email, :password, :password_confirmation, :remember_me, :name

has_many :pins, :dependent => :destroy
end

routes.rb

Omrails::Application.routes.draw do
 resources :pins

 devise_for :users

 root :to => 'pages#home'
 get 'about' => 'pages#about'

app/models/pin.rb

class Pin < ActiveRecord::Base
  attr_accessible :description

  validates :description, presence: true
    validates :name, presence: true

  belongs_to :user
    validates :user_id, presence: true
 end

db/migrate/create_pins

class CreatePins < ActiveRecord::Migration
  def change
    create_table :pins do |t|
      t.string :description

      t.timestamps
    end
  end
end

db/migrate/add_user_id_to_pins.rb

class AddUserIdToPins < ActiveRecord::Migration
  def change
    add_column :pins, :user_id, :integer
    add_index :pins, :user_id
  end
end

db/migrate/add_name_to_users.rb

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

关于哪里出了问题有什么想法吗?

不确定它是否相关,但这曾经有效。我可以一直关注Mattan Griffel's One Month Rails course -- Add Assoc bt Pins and Users video 直到 29 分 50 秒,但后来我意识到我必须跳回自定义设计,因为我忘了添加简单的表单。

现在已经添加了简单的表单,我正在尝试继续前进 - 并被困在这里:(

更新:我运行了 migrate redo 来创建 pin 并将用户 ID 添加到 pin。然后我删除了验证名称行。现在我在创建 pin 时收到以下错误

ActiveRecord::UnknownAttributeError in PinsController#new

unknown attribute: user_id 
app/controllers/pins_controller.rb:29:in `new'

pins_controller.rb

  def new
    @pin = current_user.pins.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @pin }
    end
  end

非常感谢您的帮助

db/schema.rb

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

  create_table "pins", :force => true do |t|
    t.string   "description"
    t.datetime "created_at",  :null => false
    t.datetime "updated_at",  :null => false
  end

  create_table "users", :force => true do |t|
    t.string   "email",                  :default => "", :null => false
    t.string   "encrypted_password",     :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.datetime "created_at",                             :null => false
    t.datetime "updated_at",                             :null => false
  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

【问题讨论】:

  • 您是否生成并运行了迁移——bundle exec rake db:migratename 添加到Pin 模型?
  • 感谢@cortex,分享。
  • 感谢@Nerve - 我刚刚添加了我之前所做的迁移。他们看起来还好吗?我应该重新运行它们吗?

标签: ruby-on-rails ruby devise nomethoderror


【解决方案1】:

您正在验证:namePin 模型中的存在,但它没有:name 字段。你的User 有。

只需从您的Pin 模型中删除validates :name, presence: true(第5 行)。

Rails 在尝试保存 Pin 模型时会运行所有验证。当它遇到:name 上的存在验证时,它会检查@pin.name 是否为空。但问题是您的 Pin 模型没有名称方法。所以它会引发这个错误。

如果您确实希望您的 Pin 模型具有 name,请将其添加到 pins 表中:

$ rails g migration add_name_to_pins name:string
$ rake db:migrate

【讨论】:

  • 谢谢@delba。我不知道如何解决这个问题。你能帮我具体指点吗?我只是试图重做迁移,但这似乎让事情变得更加混乱。
  • @SharlaS 我刚刚更新了我的答案。如果您在迁移过程中迷路,请运行 rake db:setup。它将重新创建您的表并运行迁移。如果数据库中存储了关键数据,请不要使用它。
  • 感谢您的解释,@delba。我听从了你的建议,但现在我有一个新的“未知属性错误”。我将在上面添加详细信息 - 希望很容易解决。
  • @SharlaS unknown attribute :user_id => 你不小心从 pin_table 中删除了 user_id 字段。你跑rake db:setup了吗?告诉我你的schema (db/schema.rb)
  • 我认为我没有运行 rake db:setup 但也许它是在安装 rails 和使用 Bootstrap 和 Devise 进行自定义时完成的,如果这有意义吗?我发布了 db/schema.rb。谢谢你帮助我!
猜你喜欢
  • 2013-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-18
  • 2021-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多