【问题标题】:Rails nested association & multiple collection_selectRails 嵌套关联和多个 collection_select
【发布时间】:2016-12-07 12:07:38
【问题描述】:

我对 Rails 还很陌生,在使用多选时更新关联表时遇到了一些问题。

我有三张桌子,portrait portrait_tagstags

(标签存储了我的标签名称(传统、励志、社区等))

我想要的结果是“多选字段”将根据 Tag.all tag_id 值将标签添加到 Portrait_tag 表中。目前这似乎只插入一个字段,并且 Portrait_tag 表中的 tag_id 为 NULL,然后当我返回编辑页面时,多选重复。

参数

  Parameters: {"utf8"=>"✓", "authenticity_token"=>"j+Obhq9u+mvOKYnj4+TAGy+be8s3AbZlMvuyKiot5iyKqjMyFAcs23PjbQjOTjwl6aRBx1M5lmYRZzTjOeDTJA==", "portrait"=>{"portrait_tags_attributes"=>{"0"=>{"tag_id"=>["", "1", "2"]}}}, "commit"=>"Save changes", "id"=>"72"}

标签.rb

class Tag < ActiveRecord::Base

  has_many :portraits, through: :portrait_tags
  accepts_nested_attributes_for :portraits

end

Portrait.rb

class Portrait < ActiveRecord::Base

    has_many :portrait_tags
    has_many :tags, through: :portrait_tags

    accepts_nested_attributes_for :portrait_tags

end

Portrait_tag.rb

class PortraitTag < ActiveRecord::Base

  belongs_to :portrait
  belongs_to :tag

end

Edit.html.haml

%h1 Edit Portrait
= form_for [:admin, @portraits], :html => { :method => :put } do |f|

  - if flash[:system].present?
    - flash[:system].each do |e|
      %div= e

  - if flash[:notice].present?
    %div= flash[:notice]

  = f.fields_for :portrait_tags do |a|
    = a.collection_select :tag_id, Tag.all, :id, :name, {}, {multiple: true}

  = f.submit "Save changes", class: "btn btn-primary"

人像控制器

class Admin::PortraitsController < ApplicationController

    def edit
       @portraits = Portrait.where(artist_id: 33, id: params[:id]).take
       @portraits.portrait_tags.build

    end

    def update

      @portrait = Portrait.where(artist_id: 33, id: params[:id]).take


      if @portrait.update(portrait_params)

        p portrait_params

      else
        flash[:system] = @portrait.errors.full_messages
        p @portrait.errors.full_messages

        render :edit
      end

    end

    private
      def portrait_params
        # Permit our attributes
        params.require(:portrait).permit(:id, portrait_tags_attributes: [:id, :tag_id => [] ])

      end



end

portrait_tags 表

+----+-------------+--------+
| id | portrait_id | tag_id |
+----+-------------+--------+

人像表

+----+-----------+--------------+
| id | artist_id | artist_image |
+----+-----------+--------------+

标签表

+----+-----------+--------------------+
| id |   name    |   portrait_tag_id  |
+----+-----------+--------------------+

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:
    <%= collection_select(:portrait_tag, :tag_ids, 
                  Tag.all(:order=>"name ASC"), 
                  :id, :name, {:selected => @portraits.tag_ids, :include_blank => true}, {:multiple => true}) %>
    

    希望这对你有用。

    【讨论】:

    • 谢谢,这成功了。你能解释一下为什么吗?我正在努力解决这个问题!
    • 感谢 Atul,如果我在 form_for 中有另一个字段,我该如何传递多个参数? params.require(:portrait).permit(:id, :artist_image) params.require(:portrait_tag).permit(:id, :tag_ids => [])
    猜你喜欢
    • 2014-04-07
    • 2014-10-22
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多