当我尝试添加新针时,我不能。相反,我得到了这个错误:
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
应用程序/模型/ 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'
应用程序/模型/ 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
分贝/迁移/ create_pins
class CreatePins < ActiveRecord::Migration
def change
create_table :pins do |t|
t.string :description
t.timestamps
end
end
end
分贝/迁移/ 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
分贝/迁移/ 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跟随直到2950,然后我意识到我必须跳回到定制设计,因为我忘了添加简单的表格。
现在添加了简单的表格,我正在努力前进 - 并且卡在这里:(
更新:我运行了迁移重做来创建引脚并将用户ID添加到引脚。然后我删除了验证名称行。现在,当我创建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
非常感谢帮助
分贝/ 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
答案 0 :(得分:3)
您正在验证:name
模型中是否存在Pin
,但它没有:name
字段。您的User
已。
只需从validates :name, presence: true
模型中删除Pin
(第5行)。
Rails在尝试保存您的Pin
模型时会运行所有验证。当遇到:name
上的状态验证时,它会检查@pin.name
是否为空。但问题是你的Pin
模型没有名称方法。所以它引发了这个错误。
如果您确实希望Pin
模型拥有name
,请将其添加到pins
表:
$ rails g migration add_name_to_pins name:string
$ rake db:migrate