Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 从rails 5.0.0.1中的日期范围生成多个模型行_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 从rails 5.0.0.1中的日期范围生成多个模型行

Ruby on rails 从rails 5.0.0.1中的日期范围生成多个模型行,ruby-on-rails,ruby,Ruby On Rails,Ruby,我想从选定日期的日期范围生成多个计划事件,输入特定培训师的表格,Evento模型为: class Evento < ApplicationRecord belongs_to :equipo has_many :asistencias, dependent: :destroy accepts_nested_attributes_for :asistencias scope :done, -> { where(registrado: true) } validates

我想从选定日期的日期范围生成多个计划事件,输入特定培训师的表格,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
生成表单的控制器操作是:eventosforma\u prog

# GET /eventos/forma_prog
def forma_prog
  @entrenadores = User.all
  @dias = %w[Dom Lun Mar Mie Jue Vie Sab]
  @evento = Evento.new
end
应该创建事件的控制器操作是EventsPrograma:

# 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
当我单击submit按钮时,会出现一个错误,提示param丢失或值为空:evento,应用程序控制台指向eventosevento_参数:

# 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
我可以看出params.require:evento部分是问题所在,我猜这与“form_标签”有关,我选择了“form_for@evento”,但我这样做是因为我认为表单与模型的@evento对象没有完全关联,请在这里帮我…

你是对的,第一个问题是form_标记没有像您所期望的那样将所有evento值分组到一个键中

这可以使用form_for解决,但是,如果您想保留form_标记,只需使用evento[attribute]重命名输入即可

其中attribute是参数/字段的名称,例如输入fecha的evento[fecha]

修复上述错误后,您提到的错误将消失,但会引发另一个错误,因为创建Evento所需的任何属性都不会在表单中发送

因此,第二个问题是表单没有发送所有参数;您可以在evento_params方法中看到所需的参数:

这意味着evento_params需要具有以下结构的参数哈希:

{
  "utf8"=>"✓",
  "authenticity_token"=>"...",
  "evento"=>{
    "fecha"=>"...",
    "tipoEvento"=>"...",
    "equipo_id"=>"...",
    "comment"=>"...",
    "registrado"=>"...",
    "asistencias_attributes"=>[
      {
        evento_id=>"...",
        player_id=>"...",
        tipo=>"...",
        comment=> "..."
      }
    ],
  "commit"=>"Create events"
}

要解决此问题,您需要发送所有缺少的参数,或者调整evento____________________________________________________,刚才对问题进行了编辑,以便更明确地说明路由到EventoPrograma方法/操作的视图,该方法/操作连接到evento_参数。我希望我没有弄错你的问题。据我所知,forma_prog.html.erb会将数据发送到EventsPrograma,对吗?如果是这样,则表示表单中缺少数据。查看更新的答案以获得更详细的解释。这是正确的,但在我看来,至少这是我假装但可能失败的,forma_prog.html.erb为EventsPrograma设置常规参数,以根据这些参数生成模型记录,您可能会看到,我正在表格中定义的范围内为表格中选定的属于培训师的团队生成日期。我看到了您更新的答案,但是forma_prog.html.erb不应该设置Evento模型参数,而是应该为控制器操作设置边界参数以生成记录参数,这是可能的吗?我现在明白了,是的,您可以创建Evento,而无需在表单中明确发送其属性,但它不会以您的方式工作,也就是说,使用强参数,因为参数只包含表单发送的信息。因此,您需要手动设置所有Evento属性。我现在可以看到您是如何设置fecha和id的,但是其他属性(例如tipoEvento、comment、registrado等)呢。,信息在哪里?我在eventosprograma操作中这样做:“@evento=evento.newParameteros_evento”,并在private部分中定义了Parameteros_evento方法,以返回带有所有必需属性的evento哈希。谢谢
# 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
def evento_params
  params.require(:evento).permit(:fecha, :tipoEvento, :equipo_id, :comment, :registrado, :asistencias_attributes => [:evento_id, :player_id, :tipo, :comment])
end
{
  "utf8"=>"✓",
  "authenticity_token"=>"...",
  "evento"=>{
    "fecha"=>"...",
    "tipoEvento"=>"...",
    "equipo_id"=>"...",
    "comment"=>"...",
    "registrado"=>"...",
    "asistencias_attributes"=>[
      {
        evento_id=>"...",
        player_id=>"...",
        tipo=>"...",
        comment=> "..."
      }
    ],
  "commit"=>"Create events"
}