更新属性不适用于我的ID列表吗?

时间:2016-06-18 21:05:34

标签: ruby ruby-on-rails-3 form-for

在我的网站上,您可以打开会话,并为会话添加工具。管理员(创建会话的人)可以选择会话中的工具。

在此示例中,我们会说"管理员需要以下乐器:1,1再次使用乐器2"。到目前为止,一切都按预期进行,但随后......#{start.exciting.music.now}

管理员想要添加其他乐器但是如果乐器已经存在则无法再添加它?

修改表单

  .col-xs-12.item
    %hr
    %ul#instrument-selection.instrument-list
      = link_to instruments_path, class: 'fancybox' do
        %li
          = image_tag('plus.png', class: 'fit')
          Add instrument
      - @meeting.instruments.each do |instrument|
        %li
          = image_tag(instrument.icon_path)
          = instrument.name
          %input{type: 'hidden', value: instrument.id, name: 'meeting[instrument_ids][]'}
          %i.fa.fa-times.delete-me

点击精美框链接时打开的内容

%section#musician-listing
  .col-xs-12.item
    .musician-badge
      %ul.musician-info
        - @instruments.each do |instrument|
          %li.col-xs-12.item
            .col-xs-6
              = image_tag(instrument.icon_path, class: 'fit instrument-icons')
            .col-xs-6
              = link_to '#', class: 'btn btn-success fit square add-instrument', data: {instrument_id: instrument.id, name: instrument.name, icon: "/#{instrument.icon_path}"} do
                Add
                = instrument.name
        .clearboth

会议控制器

  def create
    meeting = current_user.meetings.create( meeting_params )
    if meeting.valid?
      redirect_to meeting
      flash[:notice] = "Succesfully created session"
    else
      redirect_to new_meeting_path
      flash[:alert] = "Oops.. You haven't filled in the required information"
    end
  end
  def update
    meeting = current_user.meetings.find(params[:id])
    meeting.update_attributes(meeting_params)

    redirect_to meeting
    flash[:notice] = "Succesfully edited session"
  end

更新会议时的输出(我猜似乎很好)

Started PATCH "/meetings/163" for 127.0.0.1 at 2016-06-18 22:29:41 +0200
Processing by MeetingsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"ZJvwuX3x4f7cACp5N75IOEn7zczO4qyevmxydiN+LF+AZnys11kioeAbUU/jwlc76+mAdN2c2vvZKFpgg+6vPA==", "meeting"=>{"title"=>"Test2", "closed_session"=>"false", "genre_id"=>"2", "instrument_ids"=>["1", "1", "1", "1", "2"], "user_ids"=>["2"]}, "commit"=>"Update session", "id"=>"163"}

现在奇怪的部分!

即使" instrument_ids"返回[" 1"," 1"," 1"," 1"," 2"]它只是删除第3和第4" 1"它保留了" 2"。

如果我们再次更新它(在更新数组之前是[" 1"," 1"," 2"]),例如我们添加另一个" 2"和" 5"和另一个" 1",PATCH命令将返回[" 1"," 1"," 2"," 2&# 34;," 5"," 1"]但它最终会以[" 1"," 1"," 2"," 5"]。

同样删除已存在的所有内容

如果您创建新会议,可以根据需要添加相同的ID。

任何帮助都会非常感激,这让我很生气......

干杯, 克里斯

P.S。盯着这个问题差不多12个小时......哈哈该死的......

Meeting.rb

class Meeting < ActiveRecord::Base
  belongs_to :genre
  acts_as_votable
  has_many :user_meetings
  has_many :users, through: :user_meetings

  has_many :meeting_instruments
  has_many :instruments, through: :meeting_instruments

  has_many :likes, through: :user_meetings, source: :user

  validates :title, :genre_id, :meeting_instruments, presence: true

  scope :ordered, -> { order(updated_at: :desc) }

  mount_uploader :preview, PreviewUploader

  def clean_users
    meeting_instruments.unavailable.where('user_id not in (?)', user_ids).update_all(user_id: nil)
  end

  def artists
    users + User.find(meeting_instruments.pluck(:user_id) - [nil])
  end

  def musicians
    "#{users.first.musician_name} (+#{artists.count - 1})"
  end

  def timelines
    meeting_instruments.unavailable.map(&:timeline)
  end

  def openings
    return 'None' unless meeting_instruments.available.any?
    Instrument.find(meeting_instruments.available.pluck(:instrument_id)).map(&:name).join(', ')
  end

  def join(user_id, instrument_id)
    meeting_instrument = meeting_instruments.available.find_by(instrument_id: instrument_id)
    meeting_instrument.update_attributes(user_id: user_id)
    meeting_instrument.create_timeline(user_id: user_id)
  end

  def wav_files
    files = []

    timelines.map(&:source_url).each do |uri|
      destination = Tempfile.new(["output_#{id}", '.wav'])
      destination_path = destination.path
      destination.unlink

      source = Tempfile.new("source_#{id}")

      open(source.path, 'wb') do |file|
        file << open(uri).read
      end

      `ffmpeg -i #{ source.path.shellescape } #{ destination_path.shellescape }`
      files << destination_path
    end
    files
  end

  def publish
    file = Tempfile.new(["output_#{id}", '.mp3'])
    files = wav_files
    if files.count > 1
      combiner = Sox::Combiner.new(files, combine: :mix, norm: true)
      combiner.write file.path
      File.open file.path
    else
      File.open files.first
    end
  end

  def load_preview
    Resque.enqueue(PreviewLoader, self.id)
  end
end

和meeting_instrument.rb

class MeetingInstrument < ActiveRecord::Base
  belongs_to :meeting
  belongs_to :instrument
  belongs_to :user
  has_one :timeline

  scope :available, -> { where(user_id: nil) }
  scope :unavailable, -> { where('user_id is not ?', nil) }

  def open?
    user_id.nil?
  end
end

0 个答案:

没有答案