簡體   English   中英

Simple_form和accepts_nested_attributes_for的問題

[英]Issues with Simple_form and accepts_nested_attributes_for

我對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'

這是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

我可以從相關頻率的角度編輯課程,但是我有一些奇怪的行為:

  • 當我保存驗證過程時,但僅將錯誤消息顯示為Flash消息,而不包含在涉及的字段下(在其他更簡單的視圖中,該消息也包含在該字段下)
  • 當我編輯某些課程的字段時(從頻率視圖中),當我單擊“保存”按鈕后,它會調用更新操作,但是,如果運行在over_max_hours_in_a_day內(如果有條件的話),則無法在修改后的視圖中停留在同一視圖上預編譯的字段(但是我在開始show action時就加載了類似的字段)
  • 在上次失敗的編輯后按“取消”按鈕時,我停留在同一頁面上,而不是返回上一個視圖(索引視圖)

我不確定這是否歸因於belongs_to模型上的accepts_nested_attributes_for,因為我通常在has_many模型中看到它。

滑軌5 5.2.2

simple_form 4.1.0

拜托,你能幫我嗎?

謝謝。

  1. 根據視圖(來自顯示/索引頁面)的不同邏輯。 創建2種更新方法
  2. 如果發生錯誤,則再次渲染視圖render :show OR render :index
  3. 我認為最好將over_max_hours_in_a_day的邏輯移至模型中
  4. 對於取消按鈕,請不要使用引薦來源網址,因為更新后它將無法正常工作。 使用表單本地人或其他方式傳遞確切的返回URL
  5. 如果您想高字段-您必須使用表單參數在其上調用.update.save方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM