2017-11-10 28 views
0

RoR을 처음 사용하고 (레일 5 사용) has_many through : 연관성에 문제가 있습니다.레일 5 - has_many through : 및 폼의 중첩 fields_for

다른 언어에 대해 다른 라벨로 카테고리를 만들고 싶습니다.

class Language < ApplicationRecord 
    has_many :category_infos 
    has_many :categories, through: :category_infos 
end 

class Category < ApplicationRecord 
    has_many :category_infos 
    has_many :languages, through: :category_infos 
    accepts_nested_attributes_for :category_infos 
end 

class CategoryInfo < ApplicationRecord 
    belongs_to :language 
    belongs_to :category 
    accepts_nested_attributes_for :language 
end 

컨트롤러 :

class CategoriesController < ApplicationController 

    def new 
    @category = Category.new 
    @languages = Language.all 
    @languages.each do |language| 
     @category.category_infos.new(language:language) 
    end 
    end 

    def create 
    @category = Category.new(category_params) 
    if @category.save 
     redirect_to @category 
    else 
     render 'new' 
    end 
    end 

    private 
    def category_params 
     params.require(:category).permit(:name, category_infos_attributes:[:label, language_attributes: [:id, :language]]) 
    end 

end 

양식 : 나는 새로운 카테고리를 만들 때

<%= form_with model: @category, local: true do |form| %> 
    <% if @category.errors.any? %> 
    <div id="error_explanation"> 
     <h2> 
     <%= pluralize(@category.errors.count, "error") %>: 
     </h2> 
     <ul> 
     <% @category.errors.full_messages.each do |msg| %> 
      <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <br/> 
    <% end %> 
    <p> 
    <%= form.label :name %> 
    <%= form.text_field :name %> 
    </p> 
    <p> 
    Labels: 
    </p> 
    <table> 
    <% @category.category_infos.each do |category_info| %> 
     <tr> 
     <td> 
      <%= category_info.language.name %> 
     </td> 
     <td> 
      <%= form.fields_for :category_infos, category_info do |category_info_form| %> 
      <%= category_info_form.fields_for :language, category_info.language do |language_form| %> 
       <%= language_form.hidden_field :id, value: category_info.language.id %> 
       <%= language_form.hidden_field :name, value: category_info.language.name %> 
      <% end %> 
      <%= category_info_form.text_field :label %> 
      <% end %> 
     </td> 
     </tr> 
    <% end %> 
    </table> 
    <p> 
    <%= form.submit %> 
    </p> 
<% end %> 

, 나는이 오류가 :

여기

내 모델입니다
Couldn't find Language with ID=1 for CategoryInfo with ID= 

:

이미 데이터베이스에 여러 언어를 등록한 그러나
@category = Category.new(category_params) 

(1 = 영어, 2 = ... 등 프랑스어) 나는 그래서 양식을 작성해야합니까

I 동시에 Category와 CategoryInfos를 영어, 프랑스어 등으로 만들 수 있습니까? 답

답변

0

당신은 고전 초보자 misstake을하고 그냥 ID를 전달하여 연결을 생성 할 때 fields_for을 사용하고 사전에

감사합니다.

<%= form_with model: @category, local: true do |f| %> 

    # ... 

    <%= f.fields_for :category_infos do |cif| %> 
    <%= cif.collection_select(:language_id, Language.all, :name, :id) %> 
    <%= cif.text_field :label %> 
    <% end %> 
<% end %> 

당신은 또한 하나의 컨트롤러에 책임을 미친 금액을 추가로 사용자가 동시에 매우 많은 안티 패턴을 언어를 만들 수 있도록하는 속성을 전달할 수 있지만

. 또한 사용자가 범주는 만들 수 있지만 언어는 만들 수 없으면 권한 문제가 발생합니다.

기능이 필요한 경우 아약스를 사용하여 별도의 언어 컨트롤러에 요청을 보냅니다.

+0

빠른 답변 주셔서 감사합니다. Ofc 또한 params.require (: category) .permit (: name, category_infos_attributes : [: language_id, : label])와 같이 params 컨트롤을 업데이트해야했습니다. – Romain