2013-10-09 50 views
0

感謝幫助stackoverflow我得到了我的創建嵌套模型表格工作的前一天,但我不能爲我的生活得到相應的更新表單工作。我已經閱讀了很多,並嘗試了儘可能多的解決方案。編輯嵌套在一個表格

表單看起來不錯,但製造商和比例的嵌套屬性,通過下拉選擇,沒有它們的當前值。表單中所有非嵌套元素都可以正常工作。

無論您對兩個嵌套下拉列表做出何種更改,按保存更改都會在相應的表格中創建NEW行,並且不會改變現有值。

最終我想要的是屬性是可編輯的,然後我將有一個「添加製造商」和「添加比例」按鈕或鏈接縮圖,需要多個列表。

這裏是我的表單字段,我嘗試過但未能通過隱藏字段。

<%= 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已經很好,但對UPDATES無效,因爲我沒有將'miniature_params'中的JOIN模型id列入白名單,所以它們無法檢索現有信息。

現在我有productions_attributes: [:id, :manufacturer_id],而不是僅僅productions_attributes: [:manufacturer_id]

如下

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

我也可以去除所有引用嵌套模式的出我的微型控制器更新方法,因爲它「只是工程」 。

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 

希望這是有人在將來有用。