컨텍스트는 다음과 같습니다. 여러 역할을 할 수있는 엔티티가 있습니다. 이러한 역할은 사용자가 관리 할 수 있습니다.중첩 된 폼의 강력한 매개 변수를 처리하는 방법은 무엇입니까? 레일즈 4
예를 들어, "Lipsum"이라는 엔터티는 "계산원 및 영업 사원"일 수 있습니다. 그래서, 이것은 many_to_many 관계입니다.
그래서 난 내 3 개 모델을 가지고 : 엔티티, type_entity 및 entity_by_type
class Entity < ActiveRecord::Base
has_many :entity_by_types
has_many :type_entities, :through => :entity_by_types
accepts_nested_attributes_for :entity_by_types
end
class EntityByType < ActiveRecord::Base
belongs_to :entity
belongs_to :type_entity
end
class TypeEntity < ActiveRecord::Base
has_many :entity_by_types
has_many :entities, :through => :entity_by_types
end
나는 개체 유형에 대한 일반적인 CRUD 있습니다.
엔티티의 CRUD에서 필자는 Select-Option Multiple 필드가 있습니다. 사용자가 선택하는 하나 이상의 유형, 즉 생성중인 엔티티가 있습니다.
class Logistics::EntitiesController < ApplicationController
def index
@type_entities = TypeEntity.all
render layout: false
# I use this for show All entities by TypeEntity in my view index
end
def show
end
def new
@type_entities = TypeEntity.all
@entity = Entity.new
render layout: false
end
def create
entity = Entity.new(entity_parameters)
if entity.save
flash[:notice] = "Succesfull!."
redirect_to :action => :index
else
flash[:error] = "Error."
redirect_to :action => :index
end
end
def edit
@entity = Entity.find(params[:id])
@type_entities = TypeEntity.all
@action = 'edit'
render layout: false
end
def update
entity = Entity.find(params[:id])
entity.update_attributes(entity_parameters)
flash[:notice] = "Succesfull."
redirect_to :action => :index
end
def destroy
@entity = Entity.destroy(params[:id])
render :json => @entity
end
private
def entity_parameters
params.require(:entity).permit(:name, :surname, entity_by_types_attributes: [:id, :entity_id, :type_entity_id])
end
end
그리고 내 일부 형태 (방법에 대한 생성 및 업데이트)입니다 :
그럼 내 컨트롤러 엔티티는 다음과 같다 내가 "버튼을 클릭 제출 때,
= simple_form_for([:namespace, @entity], html: {class: 'form-horizontal' }) do |f|
= f.input :name, placeholder: "Nombre", input_html: { class: 'form-control' }, label: false
= f.input :surname, placeholder: "Apellidos", input_html: { class: 'form-control' }, label: false
%select.select2#type-entity-select{:name => "entity[entity_by_types_attributes][type_entity_id][]", :style => "width:100%;padding: 0;border: none;", :multiple => true}
- @type_entities.each do |tent|
%option{value: "#{tent.id}"}
= tent.name
그러나 type_entity_id "값이 1 개 이상 있습니다. 내 데이터베이스에서 엔티티 ID가 1 인 레코드 만 표시하지만 type_entity_id은 NULL입니다.
또한 형식에서 선택하는 유형의 수에 따라 하나 이상의 레코드가 표시되어야하는 경우에만 1 레코드 만 봅니다.
여기서 문제는 전달 형식이 배열 형식 인 type_entity_id입니다. 그래서, 어떻게 할 수 있습니까?
PD 다음은 PARAMS 내 컨트롤러로 이동하는 방법입니다
: 시도
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ASD"1231+Dssr6mRJcXKh9xHDvuVDmVl4jnwIilRBsuE=", "entity"=>{"name"=>"Lorem", "surname"=>"Ipsum", "entity_by_types_attributes"=>{"type_entity_id"=>["1", "4"]}}}
오류가 발생했습니다 : 'TypeError (Integer에 String의 암시 적 변환 없음)'. 나는 나의 질문을 더 많은 정보로 업데이트했다. – Dvex
컨트롤러에서 Create 또는 Update please에 대한 정의를 추가 할 수 있습니까? –
좋아, 이미 추가되었습니다 – Dvex