여기에서 질문하는 것은 처음입니다. 나는 아직 많은 답변을 시도했지만 아무도 내 문제를 해결하지 못했습니다. (영어로 유감스럽게 생각합니다.)레일에 강한 매개 변수와 중첩 된 모델로 매개 변수를 지정하십시오. 4
오카이 (Okai) 문제는 다음과 같습니다. 저는 전자 상점을 운영하고 제품을 가지고 있습니다. 제품에는 카테고리가 있어야하지만 반드시 하위 카테고리는 아니어야합니다. 카테고리와 하위 카테고리 이름을 매개 변수로 포함하는 제품 만 생성하면 레일스는 카테고리 및 하위 카테고리 테이블에 대한 항목을 자동으로 만들어야합니다.
문제는 양식으로 제품을 만들 때입니다. 나는이 작업을 수행 할 때, 콘솔에서 난을 참조 : "허가되지 않은 매개 변수를 : 범주, 하위 범주"
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 %>
제발,이 문제로 3 일이 지났습니다. 나는 무엇을 해야할지 모르겠다. 감사.
감사합니다. 그게 효과적이고 형식에 대한 개선은 매우 유용하지만, 정확히 내가 원하는 것은 아닙니다. 아이디어는 동일한 양식이 모든 테이블에 대한 항목을 만들 수 있다는 것입니다. 그게 가능하니? 데이터 모델에 대한 존중 : 원래 당신이 말한 방식대로했지만, 내가 원하는 것을하기위한 논리가 더 복잡하다는 것을 깨달았 기 때문에 그것을 버렸다. 다른 데이터 모델이 더 좋습니다. 왜?. 나는 초보자이므로 모든 도움을 환영합니다. 고마워요. –
나는 당신이 어떤 옵션을 가지고 있다고 생각하지 않지만 데이터 모델을 다시 한번 살펴보아야한다. 예. 그것은 관련 모델의 인라인 생성을 갖는 복잡합니다! 아마도 단순한 작업 (이미 생성 된 카테고리 지정)을 시작한 다음 사용자가 더 많은 카테고리를 만들 수 있도록하는 방법을 살펴보십시오. 나는 당신의 하위 카테고리 설치가 너무 복잡하게 될 것이라고 생각합니다! – stef