0

現時点では、ネストされたフォームを使用して、曲を作成するときにカテゴリ タグを曲に追加しようとしています。ネストされた属性をフォームに追加するまで、フォームは正常に機能していましたが、送信ボタンをクリックしても何も実行されません (ページのリロードなどは行われません)。

私のモデルでは、曲にはカテゴリ化によって多くのカテゴリがあり、その逆も同様です。

フォームは次のとおりです。

  <div class="span10 offset1">
    <%= form_for(@song) do |f| %>

      <%= f.label :title, "Title" %>
      <%= f.text_field :title %>

      <%= nested_form_for(@song.categorizations.build) do |f| %>
        <%= f.label :category_id, "TAG" %>
        <%= f.select :category_id, options_for_select(Category.all.collect {|c| [ c.tag, c.id ] }, { :include_blank => true }), prompt: "" %>
      <%end%>

      <%= f.submit "Save song", class: "btn btn-large btn-primary" %>
    <% end %>
  </div> 

そして私の曲のコントローラー:

  def new
    @song = Song.new
  end

 def create
    @song = Song.new(params[:song])
    if @song.save

        flash[:success] = "Song successfully added to library"
        redirect_to @song
    else
        #FAIL!
        render 'new'
    end
  end

分類コントローラー:

def new
    @categorization = Categorization.new
  end

  def create
    @song = Song.find(params[:id])
    @categorization = Category.new(params[:categorization])
    if @categorization.save
        flash[:success] = "Tag added!"
        redirect_to song_path(@song)
    else
        flash[:fail] = "TAG ERROR"
        redirect_to edit_song_path(@song)
    end
  end

助けてくれてありがとう!

4

1 に答える 1

1

外側のフォームは、fields_for である必要がある内側の部分ではなく、nested_form_for である必要があります。
また、混乱を避けるために、おそらく両方に f という名前を付けるべきではありません (動作を停止することはないと思いますが)。

<div class="span10 offset1">
<%= nested_form_for(@song) do |f| %>

  <%= f.label :title, "Title" %>
  <%= f.text_field :title %>

  <%= f.fields_for(@song.categorizations.build) do |catsf| %>
    <%= catsf.label :category_id, "TAG" %>
    <%= catsf.select :category_id, options_for_select(Category.all.collect {|c| [ c.tag, c.id ] }, { :include_blank => true }), prompt: "" %>
  <%end%>

  <%= f.submit "Save song", class: "btn btn-large btn-primary" %>
<% end %>

于 2012-07-17T16:07:07.907 に答える