simple_fields_for没有显示
我试图创建两个隐藏的字段,一个显示没有问题,但另一个来自嵌套表单不会
product.rb
class Product proc { |x| x[:option_name].blank? } belongs_to :user end
product_option.rb
class ProductOption < ActiveRecord::Base belongs_to :product end
products_controller.rb
class ProductsController @product_option.option_name) @current_user.update(:selected_product => @product.id) render :nothing => true end private def product_params params.require(:product).permit(:name, :id, :position, :product_description, :product_image_type, :product_image, :product_detail, :product_option_id, :product_options_attributes => [:id, :option_name, :ranking, :total_redeemed, :product_id]) end end
_form.html.erb
"post",:remote => true, :class => "item_prompt" do |f| %> 'product_id' %> 'product_option_id' %> "yep ready_button confirm_button", :name => "confirm_button" %>
HTML输出
我想出来了……问题是
fields_for将循环遍历集合关联,渲染出与其中的项目一样多的次数,如果关联为空则表示0次
所以要解决我必须添加的问题
@product = Product.new @product.product_options.build
到控制器中的索引操作。