Rails的联接表不保存

时间:2018-08-15 03:03:47

标签: ruby-on-rails jointable

嗨,我正在玩耍,并且构建了一个小清单应用程序。

我的应用程序具有一个列表模型,该列表模型通过has拥有许多标签,并且属于许多连接表。

联接表称为listings_tags

我的问题是在创建或更新期间无法保存listing_tag关联。

我可以在控制台中看到

Started PATCH "/listings/92" for 127.0.0.1 at 2018-08-15 12:45:58 +1000
Processing by ListingsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"5isdLs2FiToZxtm1ZVPP0y0lKtnfyoLA8Njv4GBwWVH1M3TIm2IUW9ts5RR06OpQz8tgSBitZ7Rm69uVIifevQ==", "listing"=>{"name"=>"Canteen Coffee", "streetAddres"=>"19 Park Avenue", "suburb_id"=>"31", "post_code_id"=>"2", "region_id"=>"1", "country_id"=>"2", "telephone"=>"+61416650204", "url"=>"http://canteencoffee.com.au/canteen-kitchen/", "tag_ids"=>["", "1"]}, "commit"=>"Update Listing", "id"=>"92"}
  Listing Load (0.2ms)  SELECT  "listings".* FROM "listings" WHERE "listings"."id" = $1 LIMIT $2  [["id", 92], ["LIMIT", 1]]
Unpermitted parameter: :tag_ids
   (1.8ms)  BEGIN
  Suburb Load (5.4ms)  SELECT  "suburbs".* FROM "suburbs" WHERE "suburbs"."id" = $1 LIMIT $2  [["id", 31], ["LIMIT", 1]]
   (1.7ms)  COMMIT
Redirected to http://localhost:3000/listings/92
Completed 302 Found in 21ms (ActiveRecord: 9.0ms)

显然我的问题是:tag_ids

所以我尝试更改我的列表params.require(:listing).permit(),使其包含listing_attributes:[:id],标签:[:id]和:tag_ids

它杀死了我:)请帮助

列表模型

class Listing < ApplicationRecord

  has_and_belongs_to_many :tags
  belongs_to :suburb
  has_one :post_code, through: :suburb
  accepts_nested_attributes_for :tags

  def self.search(term)
    if term
      where('name LIKE ?', "%#{term}%")
    else
      order('id DESC')
    end
  end


end

标记模型

class Tag < ApplicationRecord
  has_and_belongs_to_many :listings
end

列表标签架构

create_table "listings_tags", id: false, force: :cascade do |t|
    t.bigint "listing_id", null: false
    t.bigint "tag_id", null: false
    t.index ["listing_id", "tag_id"], name: "index_listings_tags_on_listing_id_and_tag_id"
  end

列表控制器

class ListingsController < ApplicationController
  before_action :set_listing, only: [:show, :edit, :update, :destroy]

  # GET /listings
  # GET /listings.json
  def index
    @listings = Listing.search(params[:term])
  end

  # GET /listings/1
  # GET /listings/1.json
  def show
    @listing = Listing.find(params[:id])
    @tags = @listing.tags
    @suburb = @listing.suburb
    @postcode = @suburb.post_code
  end

  # GET /listings/new
  def new
    @listing = Listing.new
  end

  # GET /listings/1/edit
  def edit
  end

  # POST /listings
  # POST /listings.json
  def create
    @listing = Listing.new(listing_params)

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

  # PATCH/PUT /listings/1
  # PATCH/PUT /listings/1.json
  def update
    respond_to do |format|
      if @listing.update(listing_params)
        format.html { redirect_to @listing, notice: 'Listing was successfully updated.' }
        format.json { render :show, status: :ok, location: @listing }
      else
        format.html { render :edit }
        format.json { render json: @listing.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /listings/1
  # DELETE /listings/1.json
  def destroy
    @listing.destroy
    respond_to do |format|
      format.html { redirect_to listings_url, notice: 'Listing was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_listing
      @listing = Listing.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def listing_params
      params.require(:listing).permit(:name, :streetAddress, :telephone, :url, :term, :suburb_id, :post_code_id, :region_id, :country_id, :tag_ids)
    end
end

标签控制器

class TagsController < ApplicationController
  before_action :set_tag, only: [:show, :edit, :update, :destroy]

  # GET /tags
  # GET /tags.json
  def index
    @tags = Tag.all
  end

  # GET /tags/1
  # GET /tags/1.json
  def show
    @tag = Tag.find(params[:id])
    @listings = @tag.listings
  end

  # GET /tags/new
  def new
    @tag = Tag.new
  end

  # GET /tags/1/edit
  def edit
  end

  # POST /tags
  # POST /tags.json
  def create
    @tag = Tag.new(tag_params)

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

  # PATCH/PUT /tags/1
  # PATCH/PUT /tags/1.json
  def update
    respond_to do |format|
      if @tag.update(tag_params)
        format.html { redirect_to @tag, notice: 'Tag was successfully updated.' }
        format.json { render :show, status: :ok, location: @tag }
      else
        format.html { render :edit }
        format.json { render json: @tag.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /tags/1
  # DELETE /tags/1.json
  def destroy
    @tag.destroy
    respond_to do |format|
      format.html { redirect_to tags_url, notice: 'Tag was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_tag
      @tag = Tag.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def tag_params
      params.require(:tag).permit(:name)
    end
end

表格

<%= bootstrap_form_for(@listing, local: true) do |form| %>
  <%= form.text_field :name, id: :listing_name %>
  <%= form.text_field :streetAddress, id: :listing_streetAddress %>
  <%= form.collection_select(:suburb_id, Suburb.all, :id, :name) %>
  <%= form.collection_select(:post_code_id, PostCode.all, :id, :number) %>
  <%= form.collection_select(:region_id, Region.all, :id, :name) %>
  <%= form.collection_select(:country_id, Country.all, :id, :name) %>
  <%= form.text_field :telephone, id: :listing_telephone %>
  <%= form.text_field :url, id: :listing_url %>
  <%= form.select :tag_ids, Tag.all.pluck(:name, :id), {}, { multiple: true, class: "selectize" } %>
  <%= form.submit %>
<% end %>

非常感谢您的帮助。我确信我做错了很简单。

1 个答案:

答案 0 :(得分:1)

尝试

params.require(:listing).permit(:name, :streetAddress, :telephone, :url, :term, :suburb_id, :post_code_id, :region_id, :country_id, :tag_ids => [])

源:https://github.com/rails/strong_parameters