Ruby on rails 编辑嵌套在表单中的内容
多亏了stackoverflow上的帮助,前几天我的创建嵌套模型表单开始工作了,但我一辈子都不能让相应的更新表单工作。我读了很多书,尝试了尽可能多的解决方案 表单看起来不错,但是通过下拉选择的Manufacturer和Scale的嵌套属性没有其当前值。表单的所有非嵌套元素都可以正常工作 无论对这两个嵌套下拉列表进行什么更改,按save changes都会在相应的表中创建新行,并且不会更改现有的行 最终我想要的是属性是可编辑的,然后我将有一个“添加制造商”和“添加比例”按钮或需要多个列表的微缩模型链接 这里是我的表单字段,我尝试传递一个隐藏字段但失败 表格Ruby on rails 编辑嵌套在表单中的内容,ruby-on-rails,ruby-on-rails-4,nested-forms,fields-for,Ruby On Rails,Ruby On Rails 4,Nested Forms,Fields For,多亏了stackoverflow上的帮助,前几天我的创建嵌套模型表单开始工作了,但我一辈子都不能让相应的更新表单工作。我读了很多书,尝试了尽可能多的解决方案 表单看起来不错,但是通过下拉选择的Manufacturer和Scale的嵌套属性没有其当前值。表单的所有非嵌套元素都可以正常工作 无论对这两个嵌套下拉列表进行什么更改,按save changes都会在相应的表中创建新行,并且不会更改现有的行 最终我想要的是属性是可编辑的,然后我将有一个“添加制造商”和“添加比例”按钮或需要多个列表的微缩模型
<%= 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 %>
Date.current.year,:end\u year=>1970,:include\u blank=>true%>
这是微型控制器,我很确定我在“def更新”中填充了太多/错误的内容
微型控制器
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
类微型控制器
我不会附加模型,因为我非常确定这些关系都是正确的,因为它们可以很好地用于创建新的嵌套模型。
微型画有许多规模和生产规模
非常感谢您的帮助和指点。感谢您的回答,我已经解决了这个问题。我已经做的可以创建,但不能用于更新,因为我没有在“minimation_参数”中列出连接模型ID,所以它们无法检索现有信息
现在我有了productions\u属性:[:id,:manufacturer\u id]
,而不仅仅是productions\u属性:[:manufacturer\u 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
希望这对将来的人有用。多亏了我的答案,我已经解决了这个问题。我已经做的可以创建,但不能用于更新,因为我没有在“minimation_参数”中列出连接模型ID,所以它们无法检索现有信息
现在我有了productions\u属性:[:id,:manufacturer\u id]
,而不仅仅是productions\u属性:[:manufacturer\u 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
希望这对将来的人有用。多亏了我的答案,我已经解决了这个问题。我已经做的可以创建,但不能用于更新,因为我没有在“minimation_参数”中列出连接模型ID,所以它们无法检索现有信息
现在我有了productions\u属性:[:id,:manufacturer\u id]
,而不仅仅是productions\u属性:[:manufacturer\u 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
希望这对将来的人有用。多亏了我的答案,我已经解决了这个问题。我已经做的可以创建,但不能用于更新,因为我没有在“minimation_参数”中列出连接模型ID,所以它们无法检索现有信息
现在我有了productions\u属性:[:id,:manufacturer\u id]
,而不仅仅是productions\u属性:[:manufacturer\u 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
希望这对以后的人有用