简体   繁体   English

Simple_form和accepts_nested_attributes_for的问题

[英]Issues with Simple_form and accepts_nested_attributes_for

I'm quite new with RoR so sorry if I say something not correct. 我对RoR还是很陌生,所以如果我说不正确的话对不起。

I have these models. 我有这些模型。

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

The relationship between Course and Frequency is 1:N but, at the end, I use it as a 1:1 (things changed after defined the models). 课程与频率之间的关系是1:N,但是最后,我将其用作1:1(定义模型后情况发生了变化)。

This is the view app/views/frequencies/show.html.haml 这是视图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'

This is part of the frequencies_controller.rb 这是frequencys_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

I'm able to edit a course from the related frequency's view but I have some strange behaviours: 我可以从相关频率的角度编辑课程,但是我有一些奇怪的行为:

  • when I save the validation process occurs but I have the error message only as flash message and not under the involved field (in others simpler views I have the message also under the field) 当我保存验证过程时,但仅将错误消息显示为Flash消息,而不包含在涉及的字段下(在其他更简单的视图中,该消息也包含在该字段下)
  • when I edit some course's fields (from the frequency view) and after I click on the save button it calls the update action but, if it runs inside the over_max_hours_in_a_day if condition, I'm not able to stay on the same view with the modified fields precompiled (but I have the fields like it loads at the beginning show action) 当我编辑某些课程的字段时(从频率视图中),当我单击“保存”按钮后,它会调用更新操作,但是,如果运行在over_max_hours_in_a_day内(如果有条件的话),则无法在修改后的视图中停留在同一视图上预编译的字段(但是我在开始show action时就加载了类似的字段)
  • when I press the cancel button after a previous failed edit I remain on the same page instead of come back to the previous view (index view) 在上次失败的编辑后按“取消”按钮时,我停留在同一页面上,而不是返回上一个视图(索引视图)

I'm not sure if this is due to accepts_nested_attributes_for on a belongs_to model, because I usually see it in a has_many model. 我不确定这是否归因于belongs_to模型上的accepts_nested_attributes_for,因为我通常在has_many模型中看到它。

Rails 5 5.2.2 滑轨5 5.2.2

simple_form 4.1.0 simple_form 4.1.0

Please, can you help me? 拜托,你能帮我吗?

Thanks. 谢谢。

  1. separate logic depending on the view (from show / index pages). 根据视图(来自显示/索引页面)的不同逻辑。 Create 2 update methods 创建2种更新方法
  2. in case of error just render view again render :show OR render :index 如果发生错误,则再次渲染视图render :show OR render :index
  3. I think it's better move logic of over_max_hours_in_a_day to model 我认为最好将over_max_hours_in_a_day的逻辑移至模型中
  4. for cancel button don't use referrer because after update it won't work. 对于取消按钮,请不要使用引荐来源网址,因为更新后它将无法正常工作。 Pass exact back url using form locals or other way 使用表单本地人或其他方式传递确切的返回URL
  5. if you want to hightligh fields - you must call .update , .save method on it with form parameters 如果您想高字段-您必须使用表单参数在其上调用.update.save方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM