除新的或已编辑的Adcategory外,所有正常工作都没有保存到数据库。 可能是我在adcategories_controller强参数中遗漏了一些东西。 有什么帮助吗?
#应用程序/助手/ application_helper.rb
def nested_dropdown(adcategories)
result = []
adcategories.map do |adcategory, sub_adcategories|
result << [('- ' * adcategory.depth) + adcategory.name, adcategory.id]
result += nested_dropdown(sub_adcategories) unless sub_adcategories.blank?
end
result
end
_form.html.erb
<%= f.select(:adcategory_ids, nested_dropdown(Adcategory.all.arrange), prompt: "Adcategory", selected: @adcategory ) %>
adcategories_controller.rb
def new
@adcategory = Adcategory.new
@adcategories = Adcategory.all
end
def edit
@adcategories = Adcategory.all
end
def create
@adcategory = Adcategory.new(adcategory_params)
respond_to do |format|
if @adcategory.save
format.html { redirect_to @adcategory, notice: 'Adcategory was successfully created.' }
format.json { render :show, status: :created, location: @adcategory }
else
format.html { render :new }
format.json { render json: @adcategory.errors, status: :unprocessable_entity }
end
end
end
private
def set_adcategory
@adcategory = Adcategory.find(params[:id])
end
def adcategory_params
params.require(:adcategory).permit(:name, :parent_id, :adcategory_id )
end