def destroy
@dignity.destroy
end
對不起,這不是代碼,這就是我現在的感受。我知道在Devise上有很多初學者的問題,我想我幾乎看過每一個。爲什麼我的user_id無?
我在Rails的3.一個非常簡單的設計設置我做:
軌生成設計用戶
我也運行軌道3 GeoKit插件,(不知道這是相關的,只是知道我有這個其他模型),所以我有另一個模型稱爲位置,它acts_as_mappable。
在我發佈代碼之前,基本的問題是,我似乎無法讓user_id自動增加。我的理解是,如果我在Location類中添加一個名爲user_id的列,那麼一些Rails魔術應該爲我處理這個問題。 (我通過遷移完成的),然後簡單地設置has_many和belongs_to。 (見下文)
我想不通爲什麼user_id總是爲零。它與Devise引擎的工作方式有什麼關係?我非常肯定,當我不使用Devise時,我曾經以同樣的方式創建了這種類型的關聯。
user.rb:
class User < ActiveRecord::Base
has_many :locations
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable, :lockable and :timeoutable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
end
location.rb:
class Location < ActiveRecord::Base
belongs_to :user
attr_accessible :street_adress, :city, :state, :zip, :item, :user_id
acts_as_mappable :auto_geocode => true
def address
return "#{self.street_adress}, #{self.city}, #{self.state}, #{self.zip}, #{self.item}"
end
end
這裏是添加列的遷移:
class AddUseridToLocation < ActiveRecord::Migration
def self.up
add_column :locations, :user_id, :integer
end
def self.down
remove_column :locations, :user_id
end
end
最後,這裏是架構。 rb:
ActiveRecord::Schema.define(:version => 20110213035432) do
create_table "locations", :force => true do |t|
t.string "street_adress"
t.string "city"
t.string "state"
t.string "zip"
t.float "lat"
t.float "lng"
t.datetime "created_at"
t.datetime "updated_at"
t.string "item"
t.integer "user_id"
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 "password_salt", :default => "", :null => false
t.string "reset_password_token"
t.string "remember_token"
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"
t.datetime "updated_at"
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
編輯:我很好,RTFM響應,只要我可以在正確的方向有點推動。我有一個懷疑,我需要在我的locations_controller.rb的創建操作中告訴rails一些東西?有人在這裏給我一點提示!
我應該在我的location.rb中添加一下,我在`attr_accessible`上添加`:user_id`只是爲了讓它工作,這就是爲什麼在那裏,如果你想知道... – Kevin 2011-02-16 02:04:14
發佈代碼您在哪裏創建位置。 – 2011-02-16 02:43:37