错误&#34;模型<jointable>中的表<join_table>的未知主键&#34;当我使用&#34; collection_check_boxes&#34; </jointable> </join_table>时

时间:2014-09-18 16:37:42

标签: ruby-on-rails ruby ruby-on-rails-4 primary-key

我为HABTM案例设置了collection_check_boxes,我收到了这个错误:

Started POST "/cars" for 127.0.0.1 at 2014-09-18 13:11:23 -0300
Processing by CarsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"b6MdXqwIDw2GvgEGvY1dGEr3v08+h9IkC5UPFyjX6MA=", "car"=>{"brand"=>"4", "model"=>"1117", "version"=>"8417", "license_plate"=>"hjg1234", "year"=>"133", "year_fabrication"=>"134", "steelplated"=>"false", "price"=>"12.34", "car_body_type_id"=>"2", "car_gearshift_id"=>"1", "condition"=>"new", "ports_number"=>"2", "color"=>"31", "car_fuel_type_id"=>"3", "renavam"=>"123421352", "optional"=>"", "tag_ids"=>["2", "4", "6", ""]}, "commit"=>"Confirmar"}
  User Load (1.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
  User Load (0.9ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
  Tag Load (0.5ms)  SELECT "tags".* FROM "tags" WHERE "tags"."id" IN (2, 4, 6)
Completed 500 Internal Server Error in 18ms

ActiveRecord::UnknownPrimaryKey (Unknown primary key for table cars_tags in model CarsTag.):
  app/controllers/cars_controller.rb:62:in `create'


  Rendered /home/yanksan/.rvm/gems/ruby-2.1.2@carbon/gems/actionpack-4.0.8/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.1ms)
  Rendered /home/yanksan/.rvm/gems/ruby-2.1.2@carbon/gems/actionpack-4.0.8/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.6ms)
  Rendered /home/yanksan/.rvm/gems/ruby-2.1.2@carbon/gems/actionpack-4.0.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.4ms)
  Rendered /home/yanksan/.rvm/gems/ruby-2.1.2@carbon/gems/actionpack-4.0.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (15.6ms)

添加代码:

查看:

<%= f.collection_check_boxes :tag_ids, Tag.all, :id, :name %>

Controller(添加了tag_ids:[]):

def car_params
  params.require(:car).permit(:carname, :brand, :model, :version, :year, :year_fabrication, :condition, :mileage,
                              :price, :color, :optional, :license_plate, :steelplated, :car_body_type_id,
                              :car_gearshift_id, :ports_number, :car_fuel_type_id, :renavam, tag_ids: [])
end

...

def create
  @user = User.find(current_user)
  @car = @user.cars.new(car_params)
  respond_to do |format|
    if @car.save
      CarsUser.where('user_id = ? AND car_id = ?', current_user, @car).update_all(date: Time.now)
      car = @user.cars.find(@car)
      format.html { redirect_to car, notice: 'Car was successfully created.' }
      format.json { render action: 'show', status: :created, location: @car }
    else
      format.html { render action: 'new' }
      format.json { render json: @car.errors, status: :unprocessable_entity }
    end
  end
end

车型:

class Car < ActiveRecord::Base
  has_many :cars_tags
  has_many :tags, through: :cars_tags
end

标记模型:

class Tag < ActiveRecord::Base
  has_many :cars_tags
  has_many :cars, through: :cars_tags
end

CarsTag模型:

class CarsTag < ActiveRecord::Base
  belongs_to :car
  belongs_to :tag
end

我的代码中有这样的联想,但只有当我使用collection_check_boxes时才会出现错误。

Rails版本: 的 4.0.8

Ruby版本: 的 2.1.2p95

任何人都可以帮助我吗?

0 个答案:

没有答案