Rails has_many:through,form with checkboxes not working

时间:2016-04-03 18:35:44

标签: ruby-on-rails forms ruby-on-rails-4 has-many-through

我现在已经在这里待了大约5个小时,并且几乎尝试了所有事情。我是一名前端开发者,有着有限的铁轨经验,所以我可以100%折扣基地。

以下是我的模特:

class Apartment < ActiveRecord::Base
  validates :name, :amenities, presence: true

  has_many :apartment_amenities
  has_many :amenities, through: :apartment_amenities

  accepts_nested_attributes_for :amenities
end

class Amenity < ActiveRecord::Base
  validates :name, presence: true

  has_many :apartment_amenities
  has_many :apartments, through: :apartment_amenities
end

class ApartmentAmenity < ActiveRecord::Base

  belongs_to :apartment
  belongs_to :amenity

end

模式:

  create_table "amenities", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "apartment_amenities", force: :cascade do |t|
    t.integer  "apartment_id"
    t.integer  "amenity_id"
    t.datetime "created_at",   null: false
    t.datetime "updated_at",   null: false
  end

  add_index "apartment_amenities", ["amenity_id"], name: "index_apartment_amenities_on_amenity_id", using: :btree
  add_index "apartment_amenities", ["apartment_id"], name: "index_apartment_amenities_on_apartment_id", using: :btree

  create_table "apartments", force: :cascade do |t|
    t.string   "name"
    t.string   "address"
    t.string   "website"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

公寓管制员:

class Admin::ApartmentsController < AdminController
  before_action :set_apartment, only: [:edit, :update, :destroy]

  def new
    @apartment = Apartment.new
  end

  def create
    @apartment = Apartment.new(apartment_params)

    respond_to do |format|
      if @apartment.save
        format.html { redirect_to apartments_path, notice: 'Apartment was successfully created.' }
        format.json { render :show, status: :created, location: @apartment }
      else
        format.html { render :new }
        format.json { render json: @apartment.errors, status: :unprocessable_entity }
      end
    end
  end

  private
    def set_apartment
      @apartment = Apartment.find(params[:id])
    end

    def apartment_params
      params.require(:apartment).permit(:name, :address, :website, amenities_attributes: [:id])
    end
end

最后但并非最不重要的是新的公寓表格

<%= form_for([:admin, @apartment]) do |f| %>
  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>

  <div class="field">
    <%= f.collection_check_boxes(:amenities, Amenity.all, :id, :name ) %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

当我加载页面并保存公寓但是设施实际上没有得到保存时,一切看起来都是正确的。谢谢参观。

2 个答案:

答案 0 :(得分:1)

这是因为你的表格字段不适合你强大的参数。查看表单的源代码。我怀疑你会发现n个复选框看起来像

apartment[amenities]

但是你强大的params将amenities_attributes作为嵌套哈希。查看日志中的params哈希,您将看到表单数据的格式。您需要更改表单以使用fields_for或更改强参数

params.require(:apartment).permit(:name, :address, :website, amenities: [])

答案 1 :(得分:0)

我认为你需要允许let的名称如下:

amenities_attributes