代码之家  ›  专栏  ›  技术社区  ›  Will

在rails 5.2中用has-many-through记录保存记录

  •  -1
  • Will  · 技术社区  · 7 年前

    保存以下作业记录时出现“Spans Invalid”错误:

    #job.rb
    class Job < ApplicationRecord
        has_many :workspans
        has_many :spans, through: :workspans
    end
    

    我在rails 5.0中没有得到这个错误,但是在升级时,我不能关联跨度。

    数据来自一个相当标准的rails表单,每个跨度都有一个复选框。

    #new.html.erb
    <%= Span.each do |span| %>
      <%= check_box_tag "job[span_ids][]", span.id %>
    <% end %>
    

    什么已经改变了,我现在应该如何设置窗体来将跨距与@job关联起来?

    更新,详细信息

    #jobs_controller
      def create
        @job = Job.new(job_params)    
        if @job.save
          flash[:success] = "Job Saved"
          redirect_to  action: :index
        else
          flash[:alert] = "Job Not Saved"
          render 'new'
        end
      end
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Kartikey Tanna    7 年前

    从轨道5.2 belongs_to 默认情况下是必需的。你得提一下 optional: true 以消除错误。

    相关公关: https://github.com/rails/rails/pull/18937

    Rails回购的相关问题: https://github.com/rails/rails/issues/23960

    推荐文章