为什么我的user_id为零?

Kev*_*vin 6 ruby-on-rails has-many belongs-to devise ruby-on-rails-3

def destroy
  @dignity.destroy
end
Run Code Online (Sandbox Code Playgroud)

对不起,那不是代码,这就是我现在的感受.我知道关于Devise有很多初学者的问题,我想我几乎每个人都看过.

我在Rails 3中有一个非常简单的Devise设置.我做了:

rails生成设备用户

我也在运行rails 3 GeoKit插件,(不确定这是否相关,只知道我有这个其他型号)所以我有另一个名为Location的模型,它的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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

这是添加列的迁移:

class AddUseridToLocation < ActiveRecord::Migration
  def self.up
    add_column :locations, :user_id, :integer
  end

  def self.down
    remove_column :locations, :user_id
  end
end
Run Code Online (Sandbox Code Playgroud)

最后,这是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
Run Code Online (Sandbox Code Playgroud)

编辑:我没有RTFM响应,只要我能朝正确的方向努力.我怀疑我需要在我的locations_controller.rb的创建动作中告诉rails一些东西?有人在这里给我一点提示!

zet*_*tic 10

def destroy
  @dignity.destroy
end
Run Code Online (Sandbox Code Playgroud)

显然,首先要做的是:

raise self.esteem
Run Code Online (Sandbox Code Playgroud)

你说你不能得到user_id"自动增量".我认为你的意思是没有分配user_id(即它总是为零).你能展示为用户分配位置的代码部分吗?这些都应该工作:

@user.locations.build
@user.locations << Location.new
Run Code Online (Sandbox Code Playgroud)

编辑

要稍微扩展一下,请说你有一个如下所示的请求:

POST /users/37/locations
Run Code Online (Sandbox Code Playgroud)

并且提交的表单包含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
Run Code Online (Sandbox Code Playgroud)

'magic'基本上是Rails has_many,它声明需要在locations表的相关行中设置外键列('user_id')的值.当你调用@user.save它时,添加一个新行locations并设置user_id为值@user.id.

  • 不错的第一个建议:) (2认同)