[英]Generate several model rows from a date range in rails 5.0.0.1
I want to generate several scheduled events from a date range on selected days input in a form for a specific trainer, The Evento model is: 我想以特定教练的形式从选定日期的日期范围内的日期范围生成多个计划的事件,Evento模型为:
class Evento < ApplicationRecord
belongs_to :equipo
has_many :asistencias, dependent: :destroy
accepts_nested_attributes_for :asistencias
scope :done, -> { where(registrado: true) }
validates :equipo_id, :fecha, :tipoEvento, presence: true
end
so I have a view at 'app/views/eventos/forma_prog.html.erb' which includes a form to get the parameters to define the date range like this: 因此,我在“ app / views / eventos / forma_prog.html.erb”上有一个视图,该视图包含用于获取参数以定义日期范围的表单,如下所示:
<% provide(:title, "Entrenamientos") %>
<div class="col-sm-6 col-sm-offset-3">
<p id="notice"><%= notice %></p>
<h1>Programación de Entrenamientos</h1>
<%= form_tag(programa_path) do %>
<% dias = [] %>
<%= label_tag(:entrenador, "Trainer:") %>
<%= select_tag :entrenador,
options_from_collection_for_select(@entrenadores, "id", "name"),
prompt: "Select the trainer", class: 'form-control' %>
<b>Days:</b>
<div class="well">
<% @dias.each_with_index do |day, index| %>
<%= label_tag day, day, class: "checkbox-inline nopadding"; %>
<%= check_box_tag 'dias[]', index, checked = false, class:
"nopadding" %> |
<% end %>
</div>
<%= label_tag(:inicio, "Starting Date:") %>
<%= date_field_tag :inicio, class: 'form-control' %>
<%= label_tag(:final, "Ending Date:") %>
<%= date_field_tag :final, class: 'form-control' %>
<%= submit_tag "Create events", class: "btn btn-default" %>
<% end %>
</div>
It works fine and returns params: 它工作正常并返回参数:
{"utf8"=>"✓",
"authenticity_token"=>"10BsOFEsCvO...==",
"entrenador"=>"2",
"dias"=>["2", "4"], # Being Tuesday and Thursday
"inicio"=>"2017-06-28",
"final"=>"2017-06-30",
"commit"=>"Create events"}
The controller action that generates the form is: eventos#forma_prog 生成表单的控制器操作为:eventos#forma_prog
# GET /eventos/forma_prog
def forma_prog
@entrenadores = User.all
@dias = %w[Dom Lun Mar Mie Jue Vie Sab]
@evento = Evento.new
end
And the controller action that is supposed to create the events is eventos#programa: 而应该创建事件的控制器动作是eventos#programa:
# POST /eventos/programa
def programa
entrenador = User.find(params[:entrenador])
inicio = Date.parse(params[:inicio])
final = Date.parse(params[:final])
dias = params[:dias].map! {|ele| ele.to_i }
@tipoEvento = "Entrenamiento"
entrenador.equipos.each do |equipo|
for @fecha in (inicio..final) do
if dias.include?(@fecha.wday)
@equipo_id = equipo.id
@evento = Evento.new(evento_params)
if !@evento.save
flash[:error] = "No ha sido posible crear los eventos."
redirect_to root_path
end
end
end
end
end
When I click the submit button I get an error saying param is missing or the value is empty: evento and the app console points to the eventos#evento_params: 当我单击提交按钮时,我收到一条错误消息,指出参数缺失或值为空:evento ,应用程序控制台指向eventos#evento_params:
# Never trust big bad internet, always use strong params
def evento_params
params.require(:evento).permit(:fecha, :tipoEvento, :equipo_id, :comment, :registrado, :asistencias_attributes => [:evento_id, :player_id, :tipo, :comment])
end
I can see that the params.require(:evento) part is the problem and I guess it has something to do with the 'form_tag' I chose instead a 'form_for @evento' But I did this way because I think the form is not fully related with the @evento object for the model, please help me here... 我可以看到params.require(:evento)部分是问题所在,我想这与我选择了“ form_for @evento”的“ form_tag”有关,但我这样做是因为我认为表单不是与模型的@evento对象完全相关,请在这里帮助我...
You are correct, the first problem is that form_tag
doesn't group all evento
values in one key, as you are expecting. form_tag
,第一个问题是form_tag
并没有像您期望的那样将所有evento
值分组在一个键中。
This can be solved using form_for
but, if you would like to keep form_tag
, just rename the inputs with evento[attribute]
. 这可以使用form_for
解决,但是,如果您想保留form_tag
,只需使用evento[attribute]
重命名输入即可。
Where attribute
is the name of the parameter/field (eg evento[fecha]
for your input fecha
). 其中attribute
是参数/字段的名称(例如,输入fecha
evento[fecha]
)。
After fixing the above, the error you mention will go away, but will another one will be raised because none of the attributes needed to create an Evento
is sent in your form
. 解决上述问题后,您提到的错误将消失,但将引发另一个错误,因为创建Evento
所需的所有属性均未以您的form
发送。
So, the second problem is that the form
is not sending all the parameters; 因此, 第二个问题是form
没有发送所有参数。 you can see those needed parameters in evento_params
method: 您可以在evento_params
方法中看到那些所需的参数:
def evento_params
params.require(:evento).permit(:fecha, :tipoEvento, :equipo_id, :comment, :registrado, :asistencias_attributes => [:evento_id, :player_id, :tipo, :comment])
end
This means that evento_params
is expecting a Parameters hash with this structure: 这意味着evento_params
期望具有以下结构的Parameters哈希:
{
"utf8"=>"✓",
"authenticity_token"=>"...",
"evento"=>{
"fecha"=>"...",
"tipoEvento"=>"...",
"equipo_id"=>"...",
"comment"=>"...",
"registrado"=>"...",
"asistencias_attributes"=>[
{
evento_id=>"...",
player_id=>"...",
tipo=>"...",
comment=> "..."
}
],
"commit"=>"Create events"
}
To fix this, you either need to send all those missing parameters, or adjust evento_params
method to use only the ones sent by the form
. 要解决此问题,您要么需要发送所有缺少的参数,要么将evento_params
方法调整为仅使用由form
发送的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.