title: Having trouble rendering checkboxes in a Rails 4 many_to_many association
body: <p>I have a <code>many_to_many</code> association between <code>Categories</code> and <code>Articles</code>, using <code>has_and_belongs_to_many</code> in a Rails 4 app:</p>
  
  <p>Here are the corresponding migration and classes:</p>
  
  <pre><code>class CategoriesArticles &lt; ActiveRecord::Migration
    def change
      create_table :categories_articles, id: false do |t|
        t.belongs_to :category, index: true
        t.belongs_to :article, index: true
      end
      add_index :categories_articles, [:category_id, :article_id]
    end
  end
  
  class Category &lt; ActiveRecord::Base
    has_and_belongs_to_many :articles
  end
  
  class Article &lt; ActiveRecord::Base
    has_and_belongs_to_many :categories
  end
  </code></pre>
  
  <p>When a user creates a new article, I simply want to give him or her the option to select categories that he/she wants to associate with the new article. I want the user to be able to select these categories with checkboxes.</p>
  
  <p>Here's the <code>ArticlesController</code>:</p>
  
  <pre><code>class ArticlesController &lt; 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 index
      if user_signed_in?
        @articles = current_user.article_feed
        # TODO: if there are too few articles on a user's feed, we want to display more articles
      else
        @articles = Article.all
      end
      @articles = @articles.page(params[:page] || 1).per(12)
      respond_with(@articles)
    end
  
    def show
      respond_with(@article)
    end
  
    def new
      @categories = Category.all
      @article = Article.new
      respond_with(@article)
    end
  
    def edit
      if current_user.articles.find_by_id(params[:id]).nil?
        flash[:notice] = "You do not have permission to edit this article."
        redirect_to @article
      end
    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
  
    def destroy
      if @article
        @article.destroy
        flash[:notice] = "Article successfully destroyed."
      else
        flash[:notice] = "You do not have permission to delete this article."
      end
      # TODO: change this to another redirect location
      redirect_to root_path
    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
  </code></pre>
  
  <p>Here's the article <code>new.html.erb</code> view:</p>
  
  <pre><code>&lt;h1&gt;New article&lt;/h1&gt;
  
  &lt;%= render 'form' %&gt;
  
  &lt;%= link_to 'Back', articles_path %&gt;
  </code></pre>
  
  <p>... and the <code>form</code> partial:</p>
  
  <pre><code>&lt;%= form_for(@article) do |f| %&gt;
    &lt;% if @article.errors.any? %&gt;
      &lt;div id="error_explanation"&gt;
        &lt;h2&gt;&lt;%= pluralize(@article.errors.count, "error") %&gt; prohibited this article from being saved:&lt;/h2&gt;
  
        &lt;ul&gt;
        &lt;% @article.errors.full_messages.each do |message| %&gt;
          &lt;li&gt;&lt;%= message %&gt;&lt;/li&gt;
        &lt;% end %&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
  
    &lt;div class="field"&gt;
      &lt;%= f.label :url %&gt;&lt;br&gt;
      &lt;%= f.text_field :url %&gt;
    &lt;/div&gt;
    &lt;div class="field"&gt;
      &lt;%= f.label :initial_comment %&gt;&lt;br&gt;
      &lt;%= f.text_field :initial_comment %&gt;
    &lt;/div&gt;
  
  
    &lt;% @categories.each do |t| %&gt;
      &lt;div class="field"&gt;
        &lt;%= f.label t.name %&gt;
        &lt;%= f.check_box "categories[#{t.id}]" %&gt;
        &lt;br /&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
  
    &lt;div class="actions"&gt;
      &lt;%= f.submit %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>However, this is erroring for me, specifically the lines:</p>
  
  <pre><code>&lt;% @categories.each do |t| %&gt;
          &lt;div class="field"&gt;
            &lt;%= f.label t.name %&gt;
            &lt;%= f.check_box "categories[#{t.id}]" %&gt;
            &lt;br /&gt;
          &lt;/div&gt;
        &lt;% end %&gt;
  </code></pre>
  
  <p>Specifically, it's telling me:</p>
  
  <p><code>undefined method 'categories[1]' for #&lt;Article:0x007f401193d520&gt;</code>. How do I fix this? Thanks.</p>