0

私は現在3つのモデルを持っています:割り当てモデルを通して、曲には多くのセットリストがあり、その逆もあります。

ネストされたフォームを使用して、既存の曲をセットリストに追加しようとしています。

ネストされたフォームの現在のビュー:

<div>
  <%=form_for @allocation do|builder|%>
    <%=builder.label :song_id, "Pick a song" %>

     <%= builder.hidden_field :setlist_id, value: @setlist.id %>

     <%= builder.select(:song_id, options_for_select(@selections), {}, {multiple: true, size: 7}) %>

    <%=builder.submit "Add Song", class: "btn btn-large btn-primary" %>
  <% end %>
</div>

セットリストを編集するための私のコントローラー:

  def edit
    @songs = Song.all(order: 'title')
    @setlist = Setlist.find(params[:id])
    @allocations = @setlist.allocations
    @allocation = Allocation.new
    @selections = Song.all.collect {|s| [ [s.title, s.artist].join(" by "), s.id ]   }
  end

  def update

    @setlist = Setlist.find(params[:id])
    @selections = Song.all.collect {|s| [ [s.title, s.artist].join(" by "), s.id] }
    @allocations = @setlist.allocations
    @allocation = Allocation.new

    params[:allocation][:song_id].reject! { |c| c.empty? }

    if @setlist.update_attributes(params[:setlist])
      if @allocation.save
        flash[:success] = "SETLIST SAVED!"
        redirect_to setlist_path(@setlist)
      else
        flash[:fail] = "Setlist not saved"
        render 'edit'
      end
    else
      flash[:fail] = "FAIL!"
      render 'edit'
    end
  end

セットリストに曲を追加するためにフォームを送信するたびに、次のようなエラーが返されます。

Validation failed: Setlist can't be blank, Song can't be blank

すべてのパラメーターが正しく渡されているように見えるので、困惑しています。返されるパラメータは次のとおりです。

   {"utf8"=>"✓",
 "_method"=>"put",
 "authenticity_token"=>"ThIXkLeizRYtZW77ifHgmQ8+UmsGnDhdZ93RMIpppNg=",
 "setlist"=>{"date(1i)"=>"2012",
 "date(2i)"=>"7",
 "date(3i)"=>"11",
 "morning"=>"false"},
 "allocation"=>{"setlist_id"=>"1",
 "song_id"=>["5"]},
 "commit"=>"Add Song",
 "id"=>"1"}

よろしくお願いします

4

1 に答える 1

1

フィールドで複数の選択を許可していますが:song_id、そのうちの 1 つのオプションには空白の値があると思います。そのオプションと他の 1 つのオプションが選択されている必要があり、["", 13]応答が発生します。

params[:allocation][:song_id].reject! { |c| c.empty? }

これにより、そのパラメーターから空白のエントリが消去されます。updateこれは、メソッド内のこの行の前の任意の場所に配置する必要があります

if @setlist.update_attributes(params[:setlist])

検証エラーについては、それAllocationがフォームの目的であるため、から発生していると思います。

@allocation = Allocation.new

if @allocation.save!

:set_list_idすべての属性に、や などの値を必要とする属性がここにあるとは限りません:song_idAllocation最初に属性を設定せずにデータベースに永続化しようとしています。これが、発生している検証の問題の原因である可能性があります。

編集:

Rails のネストされたフォームは、親フォームのオブジェクトに関連付けられたオブジェクトの一連のフォーム フィールドです。このフォームがオブジェクトをどのようにfields_for呼び出したかに注意してください。person_formこれにより、 のようなネストされたパラメーターが生成されますparam[:person][:children][:name]

<% form_for @person do |person_form| %>

  <%= person_form.label :name %>
  <%= person_form.text_field :name %>

  <% person_form.fields_for :children do |child_form| %>
    <%= child_form.label :name %>
    <%= child_form.text_field :name %>
  <% end %>

  <%= submit_tag %>

このupdate方法では、次のような単純なものを使用できます

person = Person.find(params[:id]).update_attributes(params[:person])

on Person は、関連付けupdate_attributesの作成、更新、および保存を管理できます。childrenaccepts_nested_attributes_for

これがあなたの求めているものだと思います。そのため、updateそれに応じてビューと方法を再考する必要があるかもしれません。これは、レールのまったくの初心者にとっては比較的トリッキーな作業です。ドキュメント(非常によく書かれています) に戻って、ここで助けを求めてください。

于 2012-07-12T13:59:19.373 に答える