无法在Rails 4 many_to_many关联中呈现复选框?

时间:2015-04-16 19:20:57

标签: ruby-on-rails

我在many_to_manyArticles之间建立Categories关联,在Rails 4应用中使用has_and_belongs_to_many

以下是相应的迁移和类:

class CategoriesArticles < ActiveRecord::Migration
  def change
    create_table :articles_categories, id: false do |t|
      t.belongs_to :category, index: true
      t.belongs_to :article, index: true
    end
    add_index :articles_categories, [:category_id, :article_id]
  end
end

class Category < ActiveRecord::Base
  has_and_belongs_to_many :articles
end

class Article < ActiveRecord::Base
  has_and_belongs_to_many :categories
end

当用户创建新文章时,我只想让他或她选择他/她想要与新文章关联的类别。我希望用户能够使用复选框选择这些类别。

这是ArticlesController

class ArticlesController < ApplicationController
  before_action :set_article, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, only: [:new, :create, :edit, :destroy, :update]
  before_action :verify_own_article, only: [:destroy]
  respond_to :html

  ...

  def new
    @categories = Category.all
    @article = Article.new
    respond_with(@article)
  end

  def create
    # Creates article object with current_user_id, initial_comment, and URL
    @article = current_user.articles.build(article_params)

    # Uses Pismo (gem) to grab title, content, photo of URL
    @article.populate_url_fields
    if @article.save
      flash[:success] = "Article created!"

      # Might need to change the location of this redirect
      redirect_to root_url
    else
      flash[:notice] = "Invalid article."
      redirect_to new_article_path
    end

  end

  def update
    @article.update(article_params)
    flash[:notice] = "Article successfully updated."
    respond_with(@article)
  end

  private
    def set_article
      @article = Article.find(params[:id])
    end

    def article_params
      params.require(:article).permit(:url, :title, :datetime, :content, :photo, :initial_comment)
    end

    # Ensure that a signed in user can only delete articles that they have posted
    def verify_own_article
      @article = current_user.articles.find_by_id(params[:id])
    end
end

以下是文章new.html.erb视图:

<h1>New article</h1>

<%= render 'form' %>

<%= link_to 'Back', articles_path %>

...和form部分:

<%= form_for(@article) do |f| %>
  <% if @article.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@article.errors.count, "error") %> prohibited this article from being saved:</h2>

      <ul>
      <% @article.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :url %><br>
    <%= f.text_field :url %>
  </div>
  <div class="field">
    <%= f.label :initial_comment %><br>
    <%= f.text_field :initial_comment %>
  </div>


  <% @categories.each do |t| %>
    <div class="field">
      <%= f.label t.name %>
      <%= f.check_box "categories[#{t.id}]" %>
      <br />
    </div>
  <% end %>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

然而,这对我来说是错误的,特别是行:

<% @categories.each do |t| %>
        <div class="field">
          <%= f.label t.name %>
          <%= f.check_box "categories[#{t.id}]" %>
          <br />
        </div>
      <% end %>

具体来说,它告诉我:

当我尝试呈现undefined method 'categories[1]' for #<Article:0x007f401193d520>页面时,

New Article。我该如何解决?感谢。

1 个答案:

答案 0 :(得分:2)

最好使用Rails collection_check_boxes帮助程序,而不是尝试手动创建这些复选框。这个助手已经创建了所需的所有参数/标记内容,以便添加或排除HABTM关系的项目,所有这些都在幕后。因此,您可以更改视图以包含以下内容:

<%= f.collection_check_boxes :categories_ids, @categories, :id, :name %>

请勿忘记在强参数声明中添加此内容(因为您必须接收所选类别ID并将其绑定到Article模型):

params.require(:article).permit(
  :url, :title, :datetime, :content, 
  :photo, :initial_comment, categories_ids: []
)

有关进一步的自定义(每个复选框的html样式或结构),请参阅完整的documentation

我希望它有所帮助:)