gpt4 book ai didi

javascript - 处理嵌套表单中的许多隐藏字段

转载 作者:行者123 更新时间:2023-11-29 14:44:42 25 4
gpt4 key购买 nike

我有一个 has_many through association 但我收到一个错误,因为我没有传递 :ingredient_id作为一个数组(或类似的东西,这只是一个猜测:))

错误: Couldn't find Ingredient with ID=1 for MealIngredient with ID=

Parameters:

{"utf8"=>"✓",
"authenticity_token"=>"oJQaOCcVavXpWTs4Zz88TWrBchkf7315AnxCJgoYGnpzOxqWd3tz1tiSFwZGDPmzVPpzkSDhFGdDY9j5bpoI1Q==",
"meal"=>{"name"=>"",
"meal_ingredients_attributes"=>{"1450070337804"=>{"ingredient_attributes"=>{"id"=>"1",
"name"=>"Banana"},
"ingredient_id"=>"2", # HERE THE ID IS BEING SENT
"quantity"=>"0.0",
"_destroy"=>"false"},
"1450070339919"=>{"ingredient_attributes"=>{"id"=>"2",
"name"=>"Grape"},
"ingredient_id"=>"", # HERE THERE'S NO ID BEING SENT
"quantity"=>"0.0",
"_destroy"=>"false"}}},
"commit"=>"Create Meal"}

这就是我处理这种嵌套形式的方式:

成分.rb

class Ingredient < ActiveRecord::Base
has_many :meal_ingredients
has_many :meals, through: :meal_ingredients


accepts_nested_attributes_for :meal_ingredients, :reject_if => :all_blank

end

Meal.rb

class Meal < ActiveRecord::Base

has_many :meal_ingredients
has_many :ingredients, through: :meal_ingredients
accepts_nested_attributes_for :ingredients, :reject_if => :all_blank, :allow_destroy => true
accepts_nested_attributes_for :meal_ingredients

end

MealIngredient.rb

class MealIngredient < ActiveRecord::Base
belongs_to :meal
belongs_to :ingredient
accepts_nested_attributes_for :ingredient, :reject_if => :all_blank

end

_form.html.erb

<div class = "row">
<div class= "col-md-10 col-md-offset-1">
<%= simple_form_for(@meal) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :name %>
</div>
<h3>ingredientes</h3>
<fieldset id="ingredients">
<ol>
<%= f.fields_for :meal_ingredients do |meal_ingredient| %>
<%= render "meal_ingredient_fields", :f => meal_ingredient %>
<% end %>
</ol>

<div class="form-actions">
<div><%= link_to_add_association 'adicionar ingrediente', f, :meal_ingredients, 'data-association-insertion-node' => "#ingredients ol", 'data-association-insertion-method' => "append", :wrap_object => Proc.new {|quantity| quantity.build_ingredient; quantity }, :class => "btn btn-default" %></div>
</div>
</fieldset>
<div class="form-actions">
<div><%= f.button :submit, class: "btn btn-success" %></div>
</div>
<% end %>
</div>
</div>

_meal_ingredient_fields.html.erb

<div class = "nested-fields">
<table class= "table">
<thead>
<tr>
<td>
<%= f.label "Nome" %>
</td>
<td>
<%= f.label "Unidade" %>
</td>
<td>
<%= f.label "Carbo" %>
</td>
<td>
<%= f.label "Prot" %>
</td>
<td>
<%= f.label "Gordura" %>
</td>
<td>
<%= f.label "Quantidade" %>
</td>
<td>
<%= f.label "kcal" %>
</td>
</tr>
</thead>
<tbody>
<tr>
<%= f.fields_for :ingredient do |fi| %>
<%= fi.hidden_field :id %> ## EDIT 1: THIS LINE WAS REMOVED
<td scope="row" class="col-md-4">
<%= fi.autocomplete_field :name, autocomplete_ingredient_name_ingredients_path, :id_element => '#ingredient_id', class: "form-control" %>
</td>
<td class="col-md-1">
<%= fi.text_field :unit, required: true, class: "form-control" %>
</td>
<td class="col-md-1">
<%= fi.text_field :carb, required: true, :pattern => '^\d+(\.\d+)*$', title: "Apenas números separados por pontos", class: "form-control " %>
</td>
<td class="col-md-1">
<%= fi.text_field :prot, required: true, :pattern => '^\d+(\.\d+)*$', title: "Apenas números separados por pontos", class: "form-control " %>
</td>
<td class="col-md-1">
<%= fi.text_field :fat, required: true, :pattern => '^\d+(\.\d+)*$', title: "Apenas números separados por pontos", class: "form-control " %>
</td>
<% end %>
<td class="col-md-1">
<%= f.hidden_field :ingredient_id %> ## EDIT 1: FOLLOWING CODE WAS REMOVED: :id => 'ingredient_id'
<%= f.number_field :quantity, required: true, :pattern => '^\d+(\.\d+)*$', title: "Apenas números separados por pontos", class: "form-control" %>

</td>
<td class="col-md-1">
</td>
<td class="col-md-1">
<%= link_to_remove_association "remove item", f, :class => "btn btn-danger" %>
</td>
</tr>
</tbody>
</table>
</div>

最后, Controller :meals_controller.rb

class MealsController < ApplicationController
before_action :set_meal, only: [:edit, :update, :show]


def index
@meals = Meal.all
end

def show
end

def new
@meal = Meal.new
@ingredient = @meal.ingredients.build
end

def create
@meal = Meal.new(meal_params)
if @meal.save
flash[:success] = "Refeição criada com sucesso!"
redirect_to meals_path
else
render :new
end
end

def edit
end

def update
if @meal.update(meal_params)
flash[:success] = "Your meal was updated succesfully!"
redirect_to meal_path(@meal)
else
render :edit
end
end

private

def meal_params
params.require(:meal).permit(:name, :picture, :tcarb, :tprot, :tfat, :tkcal, meal_ingredients_attributes: [:ingredient_id, :quantity, ingredient_attributes: [:id, :name, :unit, :carb, :prot, :fat, :_destroy]])
end


def set_meal
@meal = Meal.find(params[:id])
end

end

请帮忙,这让我很头疼:D

编辑


我正在使用 ingredients.js 收集 cocoon 设置的当前 ID,并在自动完成中选择成分时将其应用到字段中。

 $(function(){
var num;
var id;
$('#ingredients').on('cocoon:after-insert', function(e, insertedItem) {
$('input[id^="meal_meal_ingredients_attributes_"][id$="_ingredient_attributes_name"]').each(function(){
var current = $(this);
var rx = /meal_meal_ingredients_attributes_(.*)_ingredient_attributes_name/;
num = rx.exec(current.attr('id'))[1];
id = '#meal_meal_ingredients_attributes_'+ num +'_ingredient_';
});
$(id + 'attributes_name').bind('railsAutocomplete.select', function(event, data){
$( id + 'id' ).val( data.item.id);
$( id + 'attributes_unit' ).val( data.item.unit).prop('disabled', true);
$( id + 'attributes_carb' ).val( data.item.carb).prop('disabled', true);
$( id + 'attributes_prot' ).val( data.item.prot).prop('disabled', true);
$( id + 'attributes_fat' ).val( data.item.fat).prop('disabled', true);
});

});
});

最佳答案

错误看起来很不明显;我不认为这会直接回答你的问题,但无论如何我都会试一试看看是否有帮助:

{"utf8"=>"✓",
"authenticity_token"=>"oJQaOCcVavXpWTs4Zz88TWrBchkf7315AnxCJgoYGnpzOxqWd3tz1tiSFwZGDPmzVPpzkSDhFGdDY9j5bpoI1Q==",
"meal"=>
{"name"=>"",
"meal_ingredients_attributes"=>
{
"1450070337804"=>
{ "ingredient_attributes"=> {"id"=>"1","name"=>"Banana"},
"ingredient_id"=>"2", # HERE THE ID IS BEING SENT
"quantity"=>"0.0",
"_destroy"=>"false"
},
"1450070339919"=>
{ "ingredient_attributes"=> {"id"=>"2", "name"=>"Grape"},
"ingredient_id"=>"", # HERE THERE'S NO ID BEING SENT
"quantity"=>"0.0",
"_destroy"=>"false"
}
}
},
"commit"=>"Create Meal"
}

我试图确定您实际上是在制作一种新成分,还是调用现有成分。

您正在使用以下字段:

<%= f.fields_for :ingredient do |fi| %>
<%= fi.hidden_field :id %>
<% end %>
<%= f.hidden_field :ingredient_id, :id => 'ingredient_id' %>

ingredient_id 会建议您调用现有成分; id 字段表明您正在尝试创建一个新的。这可能是错误发生的地方。

  1. Why are you setting id for an ingredient?
  2. Why are you then referring to ingredient_id for meal_ingredient?

我个人会去掉新成分的 id,并使 ingredient_id 取决于您是否有新成分(如果您创建新的嵌套,它会自动设置记录)。

我还会确保 ingredient_attributes 仅在全部填写(我认为您已经填写)时才被接受:

#app/models/meal_ingredient.rb
class MealIngredient < ActiveRecord::Base
accepts_nested_attributes_for :ingredient, reject_if: :all_blank
end

不需要为嵌套记录明确定义id:

<%= f.fields_for :ingredient do |fi| %>
# remove ID, you don't need it
<%= fi.text_field :name %>

要管理 ingredient_idingredient_attributes 的事情,您可能必须在前端使用 JS(如果需要,我会用这个更新答案) ;可以使用以下方法处理后端:

#app/models/meal_ingredient.rb
class MealIngredient < ActiveRecord::Base
validate :ingredient_set

private

def ingredient_set
errors.add(:ingredient_id, "Need either a new ingredient, or to select one") unless ingredient && ingredient_id.blank?
end
end

此外,看起来您的强参数需要调整:

def meal_params
params.require(:meal).permit(:name, :picture, :tcarb, :tprot, :tfat, :tkcal, meal_ingredients_attributes: [:ingredient_id, :quantity, ingredient_attributes: [:name, :unit, :carb, :prot, :fat, :_destroy]])
end

关于javascript - 处理嵌套表单中的许多隐藏字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34260394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com