Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8740229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:05:40+00:00 2026-06-13T11:05:40+00:00

I am working on building a blog with categorization. I am a bit stuck

  • 0

I am working on building a blog with categorization. I am a bit stuck on how to implement categorization in the form. I have setup a has many through relationship and want to add check boxes to associate a blog with multiple categories. What I have so far is passing the categories through to the view, and I can list them out, however I cannot get the form_for method working for some reason.

Here is my code.

blog model

class Blog < ActiveRecord::Base
  attr_accessible :body, :title, :image
  has_many :categorizations
  has_many :categories, through: :categorizations
  has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }

  validates :title, :body, :presence => true

end

Category Model

class Category < ActiveRecord::Base
  has_many :categorizations
  has_many :blogs, through: :categorizations
  attr_accessible :name
end

Categorization Model

class Categorization < ActiveRecord::Base
  attr_accessible :blog_id, :category_id
  belongs_to :blog
  belongs_to :category
end

Blog new controller

 def new
    @blog = Blog.new
    @categories = Category.all

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @blog }
    end
  end

Blog new form view

<%= form_for(@blog, :url => blogs_path, :html => { :multipart => true }) do |f| %>
  <% if @blog.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@blog.errors.count, "error") %> prohibited this blog from being saved:</h2>

      <ul>
      <% @blog.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
   <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
   <div class="field">
    <%= f.file_field :image %>
  </div>
  <div class="field">
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </div>
  <div class="field">
    Categories:
    <% @categories.each do |category| %>
      <% fields_for "blog[cat_attributes][]", category do |cat_form| %>
        <p>
          <%= cat_form.check_box :name %>
        </p>
      <% end %>
    <% end %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

This code is my failing point

<% @categories.each do |category| %>
          <% fields_for "blog[cat_attributes][]", category do |cat_form| %>
            <p>
              <%= cat_form.check_box :name %>
            </p>
          <% end %>
        <% end %>

Although I am not positive I am approaching any of it right since I am currently learning. Any advice on how to accomplish this.

Thanks,
CG

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:05:42+00:00Added an answer on June 13, 2026 at 11:05 am

    First of all, you probably don’t need a separate Categorization model unless there’s a use case you haven’t described here. You can set up a many-to-many relationship like this:

    class Blog < ActiveRecord::Base
      has_and_belongs_to_many :categories
    end
    
    class Category < ActiveRecord::Base
      has_and_belongs_to_many :blogs
    end
    

    You should have a database table like this:

    class CreateBlogsCategories < ActiveRecord::Migration
      def change
        create_table :blogs_categories, id: false do |t|
          t.references :blog
          t.references :category
        end
      end
    end
    

    Then you can construct the view like this:

    <div class="field">
      Categories:
      <% @categories.each do |category| %>
        <%= label_tag do %>
          <%= check_box_tag 'blog[category_ids][]', category.id, @blog.category_ids.include?(category.id) %>
          <%= category.name %>
        <% end %>
      <% end %>
    </div>
    

    Lastly, in your form_for, you specify url: blogs_path – you should remove this if you plan to use this form for the edit action as well, because that should generate a PUT request to /blogs/:id. Assuming you used resources :blogs in routes.rb, Rails will determine the correct path for you based on the action used to render the form.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on building a blog with MVC 3 and EF. I have
I'm working on building a tree structure in MySQL and have been experimenting with
I'm working on building a new windows 8 app using javascript. I want to
I'm working on building an iOS 6 app. I have a class TDBeam which
I'm working on building up an interface that I want to function as a
I am building a blog site and have models in respect of Category and
What I want To get this guide to work http://blog.bigpixel.ro/2012/07/building-cc-applications-with-maven/ The Error I'm running
I am working on building a web browser in Java. I have used a
Working on building a form with several Databound controls and noticed that can't leave
I'm working on building a Silverlight application whereas we want to be able to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.