无法保存,具有嵌套属性且没有错误

时间:2016-01-21 21:35:07

标签: ruby-on-rails

我正在尝试保存表单中的输入,并且由于某种原因它无法保存 - 我的控制台中没有出现错误,也没有在表单上调用错误通知。

我的表单如下:

    <%= simple_form_for @line_item do |f| %> 
      <%= f.input :product_id, as: :hidden, input_html: { value: @product.id } %>       
      <% @attribute_categories.each do |category| %>
        <%= f.simple_fields_for :line_item_attributes do |attributes_form| %>
          <%= attributes_form.association :product_attribute, collection: category.product_attributes, label: category.name %>
        <% end %>
      <% end %>

      <%= f.input :instruction %>

      <%= f.button :submit %>
    <% end %>

我的创建操作如下所示:

  def create
    #product = Product.find(params[:product_id])
    #@line_item = @cart.add_product(product.id)
    @line_item = @cart.line_items.new(line_item_params)
    respond_to do |format|
      if @line_item.save
        format.html { redirect_to store_url(product.store), notice: 'Line item was successfully created.' }
        # format.js { @current_item = @line_item }
        format.json { render :show, status: :created, location: @line_item }
      else
        format.html { render :new }
        format.json { render json: @line_item.errors, status: :unprocessable_entity }
      end
    end
  end

我的新动作

  def new
    @product = Product.find_by(params[:product_id])
    @attribute_categories = @product.attribute_categories
    @line_item = LineItem.new
    @line_item.line_item_attributes.build
    respond_to do |format|
      format.js
    end
  end

和我的new.js.erb呈现包含表单的引导模式。 new.js.erb的代码是:

$('body').append("<%= escape_javascript render('line_items/new_modal') %>");
$('#product-modal').modal('show');

1 个答案:

答案 0 :(得分:0)

确保line_item_params允许所有适当的参数保存您的对象。

  def line_item_params
    params.require(:line_item).permit(:all, :the, :params, :you, :are, :passing)
  end