简体   繁体   English

Rails创建has_and_belongs_to_many复选框

[英]Rails creating has_and_belongs_to_many checkbox

I'm trying to save checkbox selections in a has_and_belongs_to_many relationship. 我正在尝试以has_and_belongs_to_many关系保存复选框选择。 I want to link each street to the maps that it is found on. 我想将每条street链接到找到它的maps From the map table a list of checkboxes is shown on new or edit views. map表显示在新的或编辑观点复选框列表。 But I'm missing something in linking this all together. 但是我在将所有这些链接在一起时缺少了一些东西。 It seems like the list of maps should be saved with each street item, but I'm not figuring out how to do that. 似乎应该将地图列表与每个街道项目一起保存,但是我不知道该怎么做。 I have a map field references for that purpose. 我为此有一个map字段references

Models without validations: 未经验证的模型:

class Street < ApplicationRecord
  has_and_belongs_to_many :maps
end

class Map < ApplicationRecord
  has_and_belongs_to_many :streets
end

streets_controller.rb (relevant parts): street_controller.rb(相关部分):

class StreetsController < ApplicationController
  before_action :set_street, only: [:show, :edit, :update, :destroy]

  def new
    @street = Street.new
    gon.streetExtentArray = @street.extent_array
    @maps = Map.all.order(:year)
  end

  def edit
    @maps = Map.all.order(:year)
    gon.streetExtentArray = @street.extent_array
    gon.streetExtentJson = @street.extent_json
  end
  def create
    @street = Street.new(street_params)

    respond_to do |format|
      if @street.save
        format.html { redirect_to @street, notice: "Street was successfully created.  #{undo_link}" }
        format.json { render :show, status: :created, location: @street }
      else
        format.html { render :new }
        format.json { render json: @street.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @street.update(street_params)
        format.html { redirect_to @street, notice: "Street was successfully updated.  #{undo_link}" }
        format.json { render :show, status: :ok, location: @street }
      else
        format.html { render :edit }
        format.json { render json: @street.errors, status: :unprocessable_entity }
      end
    end
  end

  private  
    def street_params
      params.require(:street).permit(:city, :previous_name, :current_name, :date_earliest, :date_latest, :cross_streets, :extent_json, :extent, :extent_length, :extent_array, :number_of_blocks, :references, :ref1, :ref2, :ref3, :notes, {:reference_ids => []})
    end
end

Relevant part of the new and edit view template: 新视图和编辑视图模板的相关部分:

<%= form_with(model: @street, local: true) do |form| %> 

<%= form.label "City" %>
  <%= form.text_field :city, id: :street_city %>
</div>

< and many other  fields >

<%= form.collection_check_boxes( :references, @maps, :id, :name, bootstrap: {check_inline: true}, class: "") do |b|
b.label(:"data-value" => b.value) { b.check_box + b.text }
end %>

<%= form.submit%>

The generated HTML: 生成的HTML:

<input type="hidden" name="street[map_ids][]" value="" />
<input type="checkbox" value="12" name="street[map_ids][]" id="street_map_ids_12" /><label for="street_map_ids_12">1857 Bancroft</label>
<input type="checkbox" value="7" name="street[map_ids][]" id="street_map_ids_7" /><label for="street_map_ids_7">1888 Sanborn</label>
and six more similar checkboxes

The migration for the join table: 联接表的迁移:

class CreateJoinTableMapsStreets < ActiveRecord::Migration[5.1]
  def change
    create_join_table :maps, :streets do |t|
      t.index [:map_id, :street_id]
      t.index [:street_id, :map_id]
    end
  end
end

No errors, but nothing is saved. 没有错误,但没有任何保存。 And I don't really understand where and how this should be saved. 而且我不太了解应将其保存在何处以及如何保存。 I've created a field named references in the Streets table, but there is no reference to this. 我在Streets表中创建了一个名为references的字段,但是没有对此的引用。 And it doesn't seemed like it should be saved in the maps_streets table and it's not. 而且似乎不应该将其保存在maps_streets表中,而事实并非如此。

I considered having the more robust has_many and belongs_to relationship, but I don't think I'm going to need it and can't see how it would help. 我考虑过拥有更健壮的has_manybelongs_to关系,但是我认为我不需要它,也看不出它会如何帮助。

Params? PARAMS? "street"=>{"city"=>"Los Angeles", "previous_name"=>"3rd St", "current_name"=>"Miramar St. One block abandoned", "date_earliest"=>"1921", "date_latest"=>"", "cross_streets"=>"Miramar. And gone one block between Lucas and Bixel", "number_of_blocks"=>"2", "extent_length"=>"", "references"=>["", "7"], "ref1"=>"Baist 1921", "ref2"=>"", "ref3"=>"", "notes"=>"3rd was jogged south starting at Beaudry to what was Crown Hill at Huntley/Boylston", "extent_json"=>"{\\"type\\":\\"LineString\\",\\"coordinates\\":[[-118.26117539280003,34.05901974362122],[-118.2593849946753,34.05823410691563],[-118.25815599257271,34.05768101430694],[-118.25759459655055,34.05717191451128],[-118.25663111959356,34.05654339202722]]}", "extent_array"=>"[[34.05900599436149,-118.26117038726808],[34.057672720136964,-118.25815558433534],[34.057174959049995,-118.25757622718811],[34.0565527535807,-118.25666427612306]]"}, "commit"=>"Update Street", "controller"=>"streets", "action"=>"update", "id"=>"645"} One checkbox checked "street"=>{"city"=>"Los Angeles", "previous_name"=>"3rd St", "current_name"=>"Miramar St. One block abandoned", "date_earliest"=>"1921", "date_latest"=>"", "cross_streets"=>"Miramar. And gone one block between Lucas and Bixel", "number_of_blocks"=>"2", "extent_length"=>"", "references"=>["", "7"], "ref1"=>"Baist 1921", "ref2"=>"", "ref3"=>"", "notes"=>"3rd was jogged south starting at Beaudry to what was Crown Hill at Huntley/Boylston", "extent_json"=>"{\\"type\\":\\"LineString\\",\\"coordinates\\":[[-118.26117539280003,34.05901974362122],[-118.2593849946753,34.05823410691563],[-118.25815599257271,34.05768101430694],[-118.25759459655055,34.05717191451128],[-118.25663111959356,34.05654339202722]]}", "extent_array"=>"[[34.05900599436149,-118.26117038726808],[34.057672720136964,-118.25815558433534],[34.057174959049995,-118.25757622718811],[34.0565527535807,-118.25666427612306]]"}, "commit"=>"Update Street", "controller"=>"streets", "action"=>"update", "id"=>"645"}

I think you need just a few changes. 我认为您只需要进行一些更改。

In your Controller, add map_ids to the permitted params. 在您的Controller中,将map_ids添加到允许的参数中。

private  
  def street_params
    params.require(:street).permit(:city, :previous_name, :current_name,
     :date_earliest, :date_latest, :cross_streets, :extent_json, :extent,
     :extent_length, :extent_array, :number_of_blocks, :notes, :map_ids => [])
  end

In your form, the collection_check_boxes should be for :map_ids 在您的表单中,collection_check_boxes应该用于:map_ids

<%= form_with(model: @street, local: true) do |form| %> 

  <%= form.collection_check_boxes( :map_ids, @maps, :id, :name, bootstrap: {check_inline: true}, class: "") do |b|
      b.label(:"data-value" => b.value) { b.check_box + b.text }
  <% end %>

<% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM