【问题标题】:Edit nested in a form编辑嵌套在表单中
【发布时间】:2013-10-09 11:15:04
【问题描述】:

感谢 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 %>

这是微型控制器,我很确定我在“def update”中填充了太多/错误的东西。

微型控制器

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

我不会附加模型,因为我很确定这些关系都是正确的,因为它们可以很好地创建新的嵌套模型。 微型模型通过尺寸和生产具有_许多规模和制造。

非常感谢任何帮助或指点。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 nested-forms fields-for


    【解决方案1】:

    感谢this Q 上的回答,我已经解决了。我已经可以创建但不适用于更新,因为我没有将 '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
    

    希望这对将来的某人有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-20
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2014-02-20
      相关资源
      最近更新 更多