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

  • SEARCH
  • Home
  • 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 8254451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:11:36+00:00 2026-06-08T01:11:36+00:00

So I have a simple post model that has_many tags :through post_tags. This allows

  • 0

So I have a simple post model that has_many tags :through post_tags. This allows for a many to many relationship. However I haven’t been able to get the form_for and fields_for to work. I am really stuck because I can’t find any documentation on the form_for helper with a has_many :through relationship. I watched the rails cast, read previous stack questions, and even researched in the Rails 3 Way. Anywho, here’s what I got.

 class Post < ActiveRecord::Base
  belongs_to :blog

  has_many :post_tags, :dependent => :destroy
  has_many :tag, :through => :post_tags, :dependent => :destroy 

  has_many :post_categories, :dependent => :destroy
  has_many :category, :through => :post_categories, :dependent => :destroy 

  attr_accessible(:title, ...)

  accepts_nested_attributes_for :tag, :allow_destroy => true   

end

and

class PostTag < ActiveRecord::Base
  belongs_to :tag
  belongs_to :post
end

and

class Tag < ActiveRecord::Base
    has_many :post_tags
    has_many :post, :through => :post_tags
end

and my controller code is

def new
    @title = "Create a New Post"
    @user = User.find(params[:user_id])
    @blog = @user.blog
    @post = @blog.post.new
    @post.ptype = params[:type]
    3.times { @post.tag.build}
  end

  def create
    @user = User.find(params[:user_id])
    @blog = @user.blog
    @post = @blog.post.new(params[:post])

    if @post.save
        ...
    end
  end

and the form is

<%= form_for([@user,@blog,@post],:url => user_blogs_posts_path, :html => {:multipart=>true}) do |p| %>
    <%= render 'shared/error_messages', :object => p.object %>
    ... 
        <%= p.fields_for :tag do |t|%>
            <%=t.label :tag %>
            <%=t.text_field :tag %>
        <% end %>

        <%=p.submit%>
<% end %>

and the error is

Can't mass-assign protected attributes: tag_attributes
  • 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-08T01:11:38+00:00Added an answer on June 8, 2026 at 1:11 am

    Add :tag_attributes to attr_accessible in Post

    attr_accessible :title, ..., :tag_attributes, ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a couple of simple objects that have a many-to-many relationship. Django has
I have a simple REST service endpoint that is failing when I POST using
I have a simple form that looks like so <% remote_form_for post, :url =>
I have a relatively simple HTML / JavaScript form. Such as this: <form method='POST'
I have a Rails 3 simple scaffolded application having one model like that: class
I have a simple db.Model , that has one of the fields db.ListProperty(users.User) For
I have a model, Report, that is polymorphic. So many itens in my site
I'm trying to model this inheritance for a simple blog system Blog has many
I have a simple form in my MVC3 site that allows users to create
Have a painfully simple blog Post creator, and I'm trying to check if the

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.