【问题标题】:Issues with Simple_form and accepts_nested_attributes_forSimple_form 和 Accepts_nested_attributes_for 的问题
【发布时间】:2019-02-27 16:41:17
【问题描述】:

我对 RoR 很陌生,如果我说的不正确,很抱歉。

我有这些模型。

class Course < ApplicationRecord
  has_many :frequencies, inverse_of: :course
  belongs_to :subject, optional: true
  validates :start_date, presence: true
end

class Frequency < ApplicationRecord
  belongs_to :user
  belongs_to :course
  validates :course, presence: true
  validates_presence_of :course
  accepts_nested_attributes_for :user, :course
  has_many_attached :docs
end

课程和频率之间的关系是 1:N,但最后,我将其用作 1:1(定义模型后发生了变化)。

这是视图 app/views/frequencies/show.html.haml

    = simple_form_for @frequency, :url => frequencies_update_path(:id => @frequency.id) do |f|
            .panel.panel-primary
              .panel-heading
                %h4.panel-title= t 'frequencies.upd_frequency'
              .panel-body
                = f.simple_fields_for :user  do |u|
                  .row
                    .col-md-4
                      = u.label t 'frequencies.first_name'
                    .col-md-4
                      = u.input :first_name, :label => false, :disabled => true, :input_html => {:id => 'first_name'}
                  .row
                    .col-md-4
                      = u.label t 'frequencies.last_name'
                    .col-md-4
                      = u.input :last_name, :label => false, :disabled => true, :input_html => {:id => 'last_name'}
                      -#= u.hidden_field :id, value: @user_id
            .panel.panel-primary
              .panel-heading
                %h4.panel-title= t 'frequencies.course'
              .panel-body
                = f.simple_fields_for :course  do |u|
                  .row
                    .col-md-4
                      = u.label t 'frequencies.course_start_date'
                    .col-md-4
                      = u.input :start_date, :label => false, :disabled => (@frequency.validated? ? true : false), :input_html => {:id => 'course_start_date'}
    .
    .
    .
    = f.submit t('button.save'), :class => 'btn btn-primary ' + (current_user.role == $admin_role && @frequency.validated? ? 'disabled' : '')
= link_to t('button.cancel'), request.referer.present? ? request.referer : frequencies_index_path, :class => 'btn btn-default'

这是 frequencies_controller.rb

的一部分
def update
    @frequency = Frequency.find params[:id]
    @course = Course.find @frequency.course_id
    if over_max_hours_in_a_day(frequency_params[:user_attributes][:id], @course)
        flash[:danger] = t('flash.max_hours')
        render :action => :show and return
    end
    if @course.update(frequency_params[:course_attributes])
      @frequency.docs.attach(frequency_params[:attach][:docs]) if (frequency_params[:attach].present? && frequency_params[:attach][:docs].present?)
      flash[:notice] = t('flash.upd')
      redirect_to :action => 'index' and return
    else
      flash[:danger] = @course.errors.full_messages.to_sentence
      render :action => :show and return
    end
  end


  def show
    @frequency = Frequency.find params[:id]
    @subjects = Subject.all
  end

我可以从相关频率的角度编辑课程,但我有一些奇怪的行为:

  • 当我保存验证过程时,我的错误消息仅显示为 flash 消息,而不是在所涉及的字段下(在其他更简单的视图中,我在字段下也有消息)
  • 当我编辑某些课程的字段(从频率视图)并单击保存按钮后,它会调用更新操作,但是,如果它在 over_max_hours_in_a_day if 条件内运行,我将无法停留在同一个视图上修改后的字段已预编译(但我在开头显示操作时加载了类似的字段)
  • 当我在上一次编辑失败后按下取消按钮时,我会留在同一页面上,而不是返回上一个视图(索引视图)

我不确定这是否是由于belongs_to 模型上的accepts_nested_attributes_for,因为我通常在has_many 模型中看到它。

Rails 5 5.2.2

simple_form 4.1.0

拜托,你能帮帮我吗?

谢谢。

【问题讨论】:

  • 有什么帮助吗?我找不到出路...

标签: ruby-on-rails simple-form


【解决方案1】:
  1. 根据视图(从显示/索引页面)分离逻辑。创建 2 个更新方法
  2. 如果出现错误,只需再次渲染视图render :show OR render :index
  3. 我认为最好将 over_max_hours_in_a_day 的逻辑移到模型中
  4. 对于取消按钮,不要使用引荐来源网址,因为更新后它将不起作用。使用表单本地或其他方式传递确切的返回 url
  5. 如果你想高亮字段 - 你必须使用表单参数调用 .update, .save 方法

【讨论】:

  • 感谢您的回复,但我有点困惑。 1 - 我在正确的控制器中有 2 种更新方法,一种用于课程,一种用于频率。你是这个意思吗? 2 - 这是我在出现错误时所做的,我使用 render :action =&gt; :show and return 但我设置了 flash 消息,因为这是我看到的唯一错误消息 3 - 好的 4 - 抱歉,但我不明白我必须做什么 5 - 我认为这是我对 @course.update(frequency_params[:course_attributes]) 所做的,但它不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-31
  • 2016-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多