2013-10-09 2 views
0

stackoverflow에 대한 도움 덕분에 다른 스타일로 작성한 중첩 된 모델 양식을 만들었지 만, 필자의 삶에서 해당 업데이트 양식을 사용할 수 없습니다. 나는 많은 것을 읽었고 내가 찾을 수있는 많은 해결책을 시도했다.양식에 중첩 된 편집

양식이 잘 보이지만 드롭 다운을 통해 선택한 제조업체 및 배율의 중첩 된 특성에는 현재 값이 없습니다. 양식의 중첩되지 않은 모든 요소가 올바르게 작동합니다.

두 개의 중첩 드롭 다운 변경 사항을 저장하면 변경 사항 저장을 눌러 해당 테이블에 새로운 행을 만들고 기존 행을 변경하지 않습니다.

궁극적으로 내가 원하는 것은 속성을 편집 할 수 있도록 한 다음 여러 제조업체가 필요한 미니어처를 위해 "제조업체 추가"및 "축척 추가"단추 또는 링크가 있습니다.

다음은 숨겨진 필드를 전달하지 못했던 양식 필드입니다.

양식 여기

<%= render 'shared/error_messages', object: f.object %> 
     <%= f.label :name %> 
     <%= f.text_field :name %> 
     <%= f.label :material %> 
     <%= f.select 'material', options_from_collection_for_select(Miniature.select("DISTINCT material"), :material, 'material', @miniature.material) %> 
     <%= f.fields_for :sizes do |size_fields| %> 
     <%= size_fields.label :scale_id, "Scale".pluralize %> 
     <%= hidden_field "Miniature Scale", @miniature.sizes %> 
     <%= size_fields.select :scale_id, options_from_collection_for_select(Scale.all, :id, :name) %> 
     <% end %> 
     <%= f.fields_for :productions do |production_fields| %> 
     <%= production_fields.label :manufacturer_id, "Manufacturer".pluralize %> 
     <%= hidden_field "Miniature Manufacturer", @miniature.productions %> 
     <%= production_fields.select :manufacturer_id, options_from_collection_for_select(Manufacturer.all, :id, :name, @miniature.manufacturers) %> 
     <% end %> 
     <%= f.label :release_date %> 
     <%= f.date_select :release_date, :start_year => Date.current.year, :end_year => 1970, :include_blank => true %> 

내가 너무 많은/잘못된 물건으로 '데프 업데이트'작성했습니다 확신 미니어처 컨트롤러입니다. 나는 그들이 새로운 중첩 된 모델을 만들기위한 잘 작동하기 때문에 관계가 모두 올바른지 확신으로

미니어처 컨트롤러

class MiniaturesController < ApplicationController 
    before_action :signed_in_user, only: [:new, :create, :edit, :update] 
    before_action :admin_user,  only: :destroy 


    def show 
    @miniature = Miniature.find(params[:id]) 
    end 

    def new 
    @miniature = Miniature.new 
    @miniature.productions.build 
    @miniature.sizes.build 
    end 

    def create 
    @miniature = Miniature.new(miniature_params) 
    @production = @miniature.productions.build 
    @size = @miniature.sizes.build 
    if @miniature.save 
     redirect_to @miniature 
    else 
     render 'new' 
    end 
    end 

    def edit 
    @miniature = Miniature.find(params[:id]) 

    end 

    def update 
    @miniature = Miniature.find(params[:id]) 
    @production = @miniature.productions.find(params[:id]) 
    @size = @miniature.sizes.find(params[:id]) 
    if @miniature.update_attributes(miniature_params) 
     @production = @miniature.productions.update_attributes(:manufacturer_id) 
     @size = @miniature.sizes.update_attributes(:scale_id) 
     flash[:success] = "Miniature updated" 
     redirect_to @miniature 
    else 
     render 'edit' 
    end 
    end 
    def index 
    @miniatures = Miniature.paginate(page: params[:page]) 
    end 

    def destroy 
    Miniature.find(params[:id]).destroy 
    flash[:success] = "Miniature destroyed." 
    redirect_to miniatures_url 
    end 

private 
    def miniature_params 
     params.require(:miniature).permit(:name, :release_date, :material, productions_attributes: [:manufacturer_id], sizes_attributes: [:scale_id]) 
    end 

    def admin_user 
     redirect_to(root_url) unless current_user.admin? 
    end 

    def signed_in_user 
     unless signed_in? 
     store_location 
     redirect_to signin_url, notice: "Please sign in." 
     end 
    end 
end 

나는 모델을 첨부하지 않습니다. 미니어처 have_many 비늘과 크기와 제작을 통해 제조하고 있습니다.

도움이나 조언을 주시면 감사하겠습니다.

답변

1

답변 덕분에 this Q에 대한 답을 구했습니다. 이미 'CREATING'에 대해서는 괜찮 았지만 'miniature_params'에 JOIN 모델 ID를 허용하지 않았기 때문에 UPDATES에서는 작동하지 않으므로 기존 정보를 검색 할 수 없었습니다.

는 지금은 productions_attributes: [:id, :manufacturer_id] 대신

def miniature_params 
    params.require(:miniature).permit(:name, :release_date, :material, productions_attributes: [:id, :manufacturer_id], sizes_attributes: [:id, :scale_id]) 
end 

는 그냥 작동 '으로 나는 또한 내 미니어처 컨트롤러 업데이트 방법에서 중첩 된 모델에 대한 참조를 모두 제거 할 수 아래처럼 productions_attributes: [:manufacturer_id]

이 .

def update 
    @miniature = Miniature.find(params[:id]) 
    if @miniature.update_attributes(miniature_params) 
     flash[:success] = "Miniature updated" 
     redirect_to @miniature 
    else 
     render 'edit' 
    end 
    end 

희망이 미래에 누군가에게 유용합니다.

관련 문제