2011-02-16 39 views
6
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一些東西?有人在這裏給我一點提示!

+0

我應該在我的location.rb中添加一下,我在`attr_accessible`上添加`:user_id`只是爲了讓它工作,這就是爲什麼在那裏,如果你想知道... – Kevin 2011-02-16 02:04:14

+0

發佈代碼您在哪裏創建位置。 – 2011-02-16 02:43:37

回答

10
def destroy 
    @dignity.destroy 
end 

顯然,第一件事要做的是:

raise self.esteem 

你說你不能得到user_id爲 「自動增量」。我認爲你的意思是user_id沒有被分配(即它總是零)。你可以顯示爲用戶分配位置的代碼部分嗎?無論這些應該工作:

@user.locations.build 
@user.locations << Location.new 

編輯

要在這個有點擴大,說你有一個看起來像這樣的請求:

POST /users/37/locations 

而且提交的表單包含input name=user[location][name] value="Paris"。一個典型的Rails控制器創建行動可能是這樣的:

def create 
    @user = User.find(params[:user_id]) 
    @user.locations.build(params[:user][:location]) 
    if @user.save 
    flash[:notice] = "Location created successfully" 
    redirect_to user_locations_path(@user) 
    else 
    render :new 
    end 
end 

的「魔法」基本上是Rails通過has_many聲明,它需要在相關設置外鍵列(「USER_ID」)的值推斷在地點表中排。當您撥打@user.save時,它會在locations中添加一個新行並將user_id設置爲值@user.id