【发布时间】: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:migrate将name添加到Pin模型? -
感谢@cortex,分享。
-
感谢@Nerve - 我刚刚添加了我之前所做的迁移。他们看起来还好吗?我应该重新运行它们吗?
标签: ruby-on-rails ruby devise nomethoderror