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 3284274
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:08:10+00:00 2026-05-17T20:08:10+00:00

I have a form that lets me create new blog posts and I’d like

  • 0

I have a form that lets me create new blog posts and I’d like to be able to create new categories from the same form.

I have a habtm relationship between posts and categories, which is why I’m having trouble with this.

I have the following 2 models:

class Post < ActiveRecord::Base
  has_and_belongs_to_many :categories
  attr_accessible :title, :body, :category_ids

  accepts_nested_attributes_for :categories # should this be singular? 
end

class Category < ActiveRecord::Base
  has_and_belongs_to_many :posts
  attr_accessible :name
end

My form lets me pick from a bunch of existing categories or create a brand new one. My form is as follows.

# using simple_form gem
.inputs
  = f.input :title
  = f.input :body

  # the line below lets me choose from existing categories
  = f.association :categories, :label => 'Filed Under'

  # I was hoping that the code below would let me create new categories
  = f.fields_for :category do |builder|
    = builder.label :content, "Name"
    = builder.text_field :content

When I submit my form, it gets processed but the new category is not created. My command prompt output tells me:

WARNING: Can't mass-assign protected attributes: category

But, if I add attr_accessible :category, I get a big fat crash with error message “unknown attribute: category”.

If I change the fields_for target to :categories (instead of category) then my form doesn’t even display.

I’ve spent a while trying to figure this out, and watched the recent railscasts on nested_models and simple_form but couldn’t get my problem fixed.

Would this be easier if I was using a has_many :through relationship (with a join model) instead of a habtm?

  • 1 1 Answer
  • 2 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-05-17T20:08:11+00:00Added an answer on May 17, 2026 at 8:08 pm

    Thanks to everyone who answered. After much trial and error, I managed to come up with a fix.

    First of all, I switched from a HABTM to a has_many :through relationship, calling my join model categorization.rb (instead of categorizations_posts.rb) – NB: the fix detailed below will likely work with a HABTM too:

    Step 1: I changed my models to look like this:

    # post.rb
    class Post < ActiveRecord::Base
      has_many :categorizations
      has_many :categories, :through => :categorizations
      attr_accessible :title, :body, :category_ids
      accepts_nested_attributes_for :categories
    end
    
    #category.rb
    class Category < ActiveRecord::Base
      has_many :categorizations
      has_many :posts, :through => :categorizations
      attr_accessible :name, :post_ids
    end
    
    #categorization.rb
    class Categorization < ActiveRecord::Base
      belongs_to :post
      belongs_to :category
    end
    

    From the post model above: obviously, the accessor named :category_ids must be present if you want to enable selecting multiple existing categories, but you do not need an accessor method for creating new categories… I didn’t know that.

    Step 2: I changed my view to look like this:

    -# just showing the relevent parts
    = fields_for :category do |builder|
      = builder.label :name, "Name"
      = builder.text_field :name
    

    From the view code above, it’s important to note the use of fields_for :category as opposed to the somewhat unintuitive fields_for :categories_attributes

    Step 3
    Finally, I added some code to my controller:

    # POST /posts
    # POST /posts.xml
    def create
      @post = Post.new(params[:post])
      @category = @post.categories.build(params[:category]) unless params[:category][:name].blank?
      # stuff removed
    end
    
    
    def update
      @post = Post.find(params[:id])
      @category = @post.categories.build(params[:category]) unless params[:category][:name].blank?
      # stuff removed
    end
    

    Now, when I create a new post, I can simultaneously choose multiple existing categories from the select menu and create a brand new category at the same time – it’s not a case of one-or-the-other

    There is one tiny bug which only occurs when editing and updating existing posts; in this case it won’t let me simultaneously create a new category and select multiple existing categories – if I try to do both at the same time, then only the existing categories are associated with the post, and the brand-new one is rejected (with no error message). But I can get round this by editing the post twice, once to create the new category (which automagically associates it with the post) and then a second time to select some additional existing categories from the menu – like I said this is not a big deal because it all works really well otherwise and my users can adapt to these limits

    Anyway, I hope this helps someone.

    Amen.

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

Sidebar

Related Questions

So I have a form that lets a user create a new team. Our
I have a form that lets users create new records, In the field that
I have an admin form that lets users create entities that require an image.
I have a sub form in Access: The CopyNo is a combobox that lets
Lets say that you have a following simple application: <form action=helloServlet method=post> Give name:<input
I have a form that has label values that I would like to pass
On a form (F1) i have a button, from which if i create another
I have a form that lets the user enter in some text. It will
I have a modelform that will either create a new model or edit an
I have a general create function that submits a new user to the database

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.