2014-02-03 5 views
1

Нужно посоветовать, как исправить создание нескольких вложенных записей в рельсах.Создание нескольких вложенных записей из поля выбора

Мой код:

Модели

class Category < ActiveRecord::Base 
    has_many :fcatalogs 
    has_many :features, :through => :fcatalogs 
    accepts_nested_attributes_for :fcatalogs 
end 

class Feature < ActiveRecord::Base 
    has_many :fcatalogs 
    has_many :categories, :through => :fcatalogs 
    accepts_nested_attributes_for :fcatalogs 
end 

class Fcatalog < ActiveRecord::Base 
    self.table_name = 'categories_features' 

    belongs_to :category 
    belongs_to :feature 
end 

В контроллере

def new 
    @category = Category.new 
    @category.fcatalogs.build 
end 

def create 
    @category = Category.new(category_params) 

    respond_to do |format| 
    if @category.save 
     format.html { redirect_to [:admin, @category], notice: category_params } #'Category was successfully created.' 
     format.json { render action: 'show', status: :created, location: @category } 
    else 
     format.html { render action: 'new' } 
     format.json { render json: @category.errors, status: :unprocessable_entity } 
    end 
    end 
end 


# Never trust parameters from the scary internet, only allow the white list through. 
    def category_params 
    params.require(:category).permit(:parent_id, :name, :description, :url, :image, :position, :visible, :meta_title, :meta_keywords, :meta_description, 
            :fcatalogs_attributes: [:feature_id]) 
    end 

Ввиду

<%= f.fields_for :fcatalogs do |builder| %> 
    <%= builder.label :feature_id, 'Feature' %> 
    <%= builder.collection_select(:feature_id, Feature.all, :id, :name, {}, {:multiple => true, :size => 5}) %> 
<% end %> 

Если я удалить : множественный => истинный,: размер = > 5 состояния, одиночная вложенная запись успешно создает, но с : кратному он терпит неудачу с ошибкой: Unpermited паров feature_id

ответ

0

Если кто-то еще натыкаются на это:

Я думаю, что вы должны изменить Params на: feature_ids, так как вы ищете несколько записей. Измените его в контроллере AND в представлении!

Смежные вопросы