一个在铁轨上的红宝石中形成三个表

时间:2016-12-27 09:35:06

标签: ruby-on-rails ruby

我想创建一个表单来将数据保存到3个表中。 3个表以一对多关系连接。

我可以将数据保存到品牌名称,但不能保存到产品类型和子类别。

下面是代码:

<%= form_for(@brandname, :html => {class: "form-horizontal", role: "form"}) do |f| %>
      <div class="form-group">
        <div class="control-label col-sm-2">
          <%= f.label :brandname %>
        </div>
        <div class="col-sm-8">
          <%= f.text_field :brandname, class: "form-control", placeholder: "Enter Brand Name" %>
        </div>
      </div>
      <div class="form-group">
        <%= f.fields_for :producttype do |builder| %>
          <div class="control-label col-sm-2">
            <%= builder.label :content, "Type Of Product" %><br />
          </div>
          <div class="col-sm-8">
            <%= builder.text_field :content, class: "form-control", placeholder: "Enter Product Type" %>
          </div>
        <% end %>
      </div>
      <div class="form-group">
        <%= f.fields_for :subcategories do |builder| %>
          <div class="control-label col-sm-2">
            <%= builder.label :content, "Sub Category" %><br />
          </div>
          <div class="col-sm-8">
            <%= builder.text_field :content, class: "form-control", placeholder: "Enter Sub Category" %>
          </div>
        <% end %>
      </div>
      <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
          <%= f.submit(@brandname.new_record? ? "Add" : "Save", class: 'btn btn-primary btn-lg') %>
          <%= link_to "Cancel", new_brandname_path, class: "btn btn-default btn-lg active" %>
        </div>
      </div>      
    <% end %>

控制器:

  def new
    @brandname   = Brandname.new
    @brandname.producttypes.build
    producttype.subcategories.build
  end

  def create
    @brandname = Brandname.new(prodinfo_params)
    if @brandname.save
      flash[:success] = "ProductInfo Created Successfully"
      redirect_to root_path
    else
      render 'new'
    end
  end

  private
  def prodinfo_params
    params.require(:brandname).permit(:brandname, :producttype, :subcategory )
    # producttype_attributes: [:producttype]
  end

Model:

Brandname:

class Brandname < ApplicationRecord
  has_many :producttypes, :dependent => :destroy
  accepts_nested_attributes_for :producttypes
end

Producttype:

class Producttype < ApplicationRecord
  belongs_to :brandname
  has_many :subcategories, :dependent => :destroy
  accepts_nested_attributes_for :subcategories
end


Sub Category:

class Subcategory < ApplicationRecord
  belongs_to :producttype
end

有人可以帮我这个吗?

3 个答案:

答案 0 :(得分:0)

如果您描述了正在发生的事情,除了说产品类型和子类别不能保存,那将会很有帮助。保存时查看日志,我怀疑您会看到关于不允许关联属性的消息。

控制器中的productinfo_params需要允许关联的属性,例如

def prodinfo_params
  params.require(:brandname).permit(:brandname, producttypes_attributes: [:content, subcategory_attributes: [:content]]) )
end

答案 1 :(得分:0)

还需要在模板中更新他的第二个构建器对象

as subategory是嵌套形式productType而不是brandname

<div class="form-group">
    <%= f.fields_for :producttype do |builder| %>
      <div class="control-label col-sm-2">
        <%= builder.label :content, "Type Of Product" %><br />
      </div>
      <div class="col-sm-8">
        <%= builder.text_field :content, class: "form-control", placeholder: "Enter Product Type" %>
      </div>

      <div class="form-group">
        <%= builder.fields_for :subcategories do |cat_builder| %>
          <div class="control-label col-sm-2">
            <%= cat_builder.label :content, "Sub Category" %><br />
          </div>
          <div class="col-sm-8">
            <%= cat_builder.text_field :content, class: "form-control", placeholder: "Enter Sub Category" %>
          </div>
        <% end %>
       </div>
    <% end %>
 </div>

答案 2 :(得分:0)

<%= form_for(@brandname, :html => {class: "form-horizontal", role: "form", :multipart => true}) do |f| %>
      <div class="form-group">
        <div class="control-label col-sm-2">
          <%= f.label :brandname %>
        </div>
        <div class="col-sm-8">
          <%= f.text_field :brandname, class: "form-control", placeholder: "Enter Brand Name" %>
        </div>
      </div>
      <div class="form-group">
        <%= f.fields_for :producttypes do |builder| %>
          <div class="control-label col-sm-2">
            <%= builder.label :producttype, "Type Of Product" %><br />
          </div>
          <div class="col-sm-8">
            <%= builder.text_field :producttype, class: "form-control", placeholder: "Enter Product Type" %>
          </div>
      </div>
      <div class="form-group">
          <%= builder.fields_for :subcategories do |builder_sub| %>
            <div class="control-label col-sm-2">
              <%= builder_sub.label :subcategory, "Sub Category" %><br />
            </div>
            <div class="col-sm-8">
              <%= builder_sub.text_field :subcategory, class: "form-control", placeholder: "Enter Sub Category" %>
            </div>
          <% end %>
        <% end %>

PARAMS:

params.require(:brandname).permit(:brandname, producttypes_attributes: [:id, :brandname_id, :producttype, subcategories_attributes: [:id, :producttype_id, :subcategory]])