【问题标题】:Assign parameters with strong parameters and nested models in rails 4在rails 4中分配具有强参数和嵌套模型的参数
【发布时间】:2015-01-02 20:33:18
【问题描述】:

这是我第一次在这里提出问题。我尝试了很多答案,但没有一个能解决我的问题。(对不起我的英语,我正在努力改进)。

Okai,问题如下:我正在做一个电子商店,它有产品。产品必须有一个类别,但不一定有一个子类别。这个想法是,只创建一个产品,包括类别和子类别名称作为参数,rails 必须自动创建类别和子类别表的条目。

问题是当我使用表单创建产品时。当我这样做时,在控制台中我看到:“Unpermitted parameters: category, subcategory”

这是三个模型:

class Product < ActiveRecord::Base

    has_one :subcategory
    has_one :category, through: :subcategory

    accepts_nested_attributes_for :category, :subcategory
end

class Category < ActiveRecord::Base
    belongs_to :product
    has_many :subcategories

    accepts_nested_attributes_for :subcategories
end

class Subcategory < ActiveRecord::Base
    belongs_to :product
    belongs_to :category
end

控制器相关部分:

def create
    @titulo = "Catálogo Online"
    @product = Product.create(product_params)

    respond_to do |format|
        if @product.save
            format.html { redirect_to @product, notice: 'Producto creado.' }
        else
            format.html { render :new }
        end
    end
end

...

def product_params
  params.require(:product).permit(:name, :description, :price, :stock, :code, category_attributes: [:id, :category, :product_id ], subcategory_attributes: [:id, :subcategory, :category_id, :product_id ])
end

还有形式

<%= form_for(@product) do |f| %>
  <% if @product.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2>

      <ul>
      <% @product.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :category %><br>
    <%= f.text_field :category %>
  </div>
  <div class="field">
    <%= f.label :subcategory %><br>
    <%= f.text_field :subcategory %>
  </div>
  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
    <div class="field">
    <%= f.label :code %><br>
    <%= f.text_field :code %>
  </div>
  <div class="field">
    <%= f.label :description %><br>
    <%= f.text_area :description %>
  </div>
  <div class="field">
    <%= f.label :price %><br>
    <%= f.number_field :price %>
  </div>
  <div class="field">
    <%= f.label :stock %><br>
    <%= f.check_box :stock %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

拜托,我遇到这个问题已经三天了。我不知道该怎么办。谢谢。

【问题讨论】:

    标签: ruby-on-rails forms rails-activerecord nested-attributes strong-parameters


    【解决方案1】:

    我会说,问题在于您正在使用 text_field 输入来输入类别和子类别。为此,您应该使用一个选择字段,然后在 UI 中提供另一种方式来创建类别和子类别。

    将 f.text_field :subcategory 替换为(并删除 :category 选项,因为您的数据模型不允许这样做)...

    <%= f.collection_select :subcategory_id, Subcategory.all, :id, :name, { selected: @product.subcategory_id } %>
    

    ...应该使您能够将产品分配给子类别。

    此外,您的数据模型在这里看起来不正确。产品需要属于子类别,而不是相反。

    【讨论】:

    • 谢谢。这很有效,并且是对非常有用的表单的改进,但这并不是我想要的。这个想法是相同的表单可以为所有表创建条目。这可能吗?尊重数据模型:最初我有你说的方式,但我放弃了它,因为我意识到做我想做的事情的逻辑更复杂。其他数据模型更好吗?为什么?。我是这方面的初学者,所以欢迎所有帮助。非常感谢。
    • 我认为您别无选择,只能再看看您的数据模型。是的。内联创建相关模型很复杂!也许从做一些简单的事情开始(分配已经创建的类别),然后一旦你开始工作,看看如何允许用户创建更多的类别。我认为您的子类别设置也会使事情复杂化!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多