为什么我的 user_id 为零?
def destroy
@dignity.destroy
end
抱歉,这不是代码,这只是我现在的感受。我知道 Devise 上有很多初学者问题,我想我几乎看了每一个问题。
我在 Rails 3 中有一个非常简单的 Devise 设置。我做了:
rails 生成设备用户
3 GeoKit 插件(不确定这是否相关,只知道我有另一个模型),所以我有另一个名为 Location 的模型,它的 act_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
最后,这是 schema.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 一些信息?有人在这里给我一点提示!
def destroy
@dignity.destroy
end
Sorry, that's not code, that's just how I feel right now. I know there are a ton of beginner questions on Devise, I think I looked at almost every single one.
I have a very simple Devise setup in Rails 3. I did:
rails generate devise User
I'm also running the rails 3 GeoKit plugin,(not sure if that's relevant, just know that I have this other model) so I have another model called Location, and it acts_as_mappable.
Before I post the code, the basic problem is that I cannot seem to get user_id to auto-increment. It was my understanding that a bit of Rails magic should take care of this for me, if I add a column called user_id to Location class. (which I did through a migration.) and then simply set has_many and belongs_to accordingly. (see below)
I can't figure out why user_id is always nil. Does it have something to do with the way the Devise engine works? I am pretty sure I've made this type of association work in the past in the same way when I wasn't using 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
here is the migration that added the column:
class AddUseridToLocation < ActiveRecord::Migration
def self.up
add_column :locations, :user_id, :integer
end
def self.down
remove_column :locations, :user_id
end
end
And finally, here is the schema.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
EDIT: I'm okay with a RTFM response, as long as I can get a little push in the right direction. I have a suspicion that I need to tell rails something in the create action of my locations_controller.rb ? Someone just give me a little hint here!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显然,要做的第一件事是:
您说您无法将
user_id
设为“自动增量”。我认为你的意思是 user_id 没有被分配(即它总是为零)。您能显示为用户分配位置的代码部分吗?其中任何一个都应该有效:编辑
要对此进行扩展,假设您有一个如下所示的请求:
提交的表单包含
input name=user[location][name] value="Paris"
。典型的 Rails 控制器创建操作可能如下所示:“神奇”基本上是 Rails 从
has_many
语句推断出它需要在相关的表中设置外键列 ('user_id') 的值。位置表中的行。当您调用@user.save
时,它会在locations
中添加一个新行,并将user_id
设置为@user.id< 的值/代码>。
Clearly the first thing to be done is:
You say you can't get
user_id
to "autoincrement". I think what you meant is that user_id is not being assigned (i.e. it is always nil). Can you show the part of the code that assigns a location to a user? Either of these should work:EDIT
To expand on this a bit, say you have a request that looks like this:
And the submitted form contains
input name=user[location][name] value="Paris"
. A typical Rails controller create action might look like this:The 'magic' is basically Rails inferring from the
has_many
statement that it needs to set the value of the foreign key column ('user_id') in the related row in the locations table. When you call@user.save
it adds a new row inlocations
and setsuser_id
to the value of@user.id
.