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

外部形式应该是nested_form_for,而不是内部形式,应该是fields_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 回答