HABTM relationships and accepts_nested_attributes_for

戏子无情 提交于 2019-12-05 01:10:12

问题


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:


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.




回答2:


In your form you probably should render the fields_for once per category (you can have multiple categories per post, hence the habtm relation). Try something like:

- for category in @post.categories
  = fields_for "post[categories_attributes][#{category.new_record? ? category.object_id : category.id}]", category do |builder|
    = builder.hidden_field :id unless category.new_record?
    = builder.label :content, "Name"
    = builder.text_field :content



回答3:


I have made my application and my nested form works with HABTM. My model is :

class UserProfile < ActiveRecord::Base

    attr_accessible :name, :profession

    has_and_belongs_to_many :cities

    belongs_to :user

    attr_accessible :city_ids, :cities
    def self.check_city(user,city)

     user.cities.find_by_id(city.id).present?

    end 

end

class City < ActiveRecord::Base

    attr_accessible :city_name

    has_and_belongs_to_many :user_profiles

end

In my form I have:

-# just showing the relevent parts


= f.fields_for :cities do|city| 

            = city.text_field :city_name 

And at my controller:

 def create

    params[:user_profile][:city_ids] ||= [] 
    if params[:user_profile][:cities][:city_name].present?
      @city= City.create(:city_name=>params[:user_profile][:cities][:city_name])
      @city.save
      params[:user_profile][:city_ids] << @city.id
    end

    @user=current_user
    params[:user_profile].delete(:cities)
    @user_profile = @user.build_user_profile(params[:user_profile])

    respond_to do |format|
      if @user_profile.save

        format.html { redirect_to @user_profile, notice: 'User profile was successfully created.' }
        format.json { render json: @user_profile, status: :created, location: @user_profile }
      else
        format.html { render action: "new" }
        format.json { render json: @user_profile.errors, status: :unprocessable_entity }
      end
    end
  end

def update

    params[:user_profile][:city_ids] ||= [] 
    if params[:user_profile][:cities][:city_name].present?
      @city= City.create(:city_name=>params[:user_profile][:cities][:city_name])
      @city.save
      params[:user_profile][:city_ids] << @city.id
    end
    @user=current_user
    params[:user_profile].delete(:cities)
    @user_profile = @user.user_profile

    respond_to do |format|
      if @user_profile.update_attributes(params[:user_profile])

        format.html { redirect_to @user_profile, notice: 'User profile was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @user_profile.errors, status: :unprocessable_entity }
      end
    end
  end

This code works.




回答4:


Maybe you should try it with (not testet):

attr_accessible :category_attributes

And HBTM relations arent really recommened... But I use them on my own :P



来源:https://stackoverflow.com/questions/3995576/habtm-relationships-and-accepts-nested-attributes-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!