【问题标题】:How to validation depend on different submit buttons如何验证取决于不同的提交按钮
【发布时间】:2020-11-23 15:35:26
【问题描述】:

我的应用程序有“save_button”、“check_button”和“submit_button”,如果我点击“submit_button”以避免有人没有附加任何文件(tape_files)并提交,我只想进行验证。

Model:
validates :tape_files, attached: true, on: :update
View:
<%= f.submit "Save", name: "save_button" %>
<%= f.submit "Check", name: "check_button" %>
<%= f.submit "Submit", name: "submit_button" %>
def update
    @tape.update(tapes_params)
    if params[:save_button]
      redirect_to tapes_path, notice: "Saved!"
    elsif params[:check_button]
      redirect_to tapes_path, notice: "Checked!"
    elsif params[:submit_button]
        redirect_to tape_file_tape_path(@tape.id), notice: "Attachment Saved!"   
    end

我发现“save(false)”可以跳过验证,但我真的不知道如何将它集成到验证中。 有人知道如何仅针对“submit_button”验证修改验证吗?

【问题讨论】:

    标签: ruby-on-rails ruby validation rails-activestorage


    【解决方案1】:

    我很难在这里提供更多的代码修复,因为我不了解您的代码和域的整个用例,但您是否探索过 custom context with on 的使用?

    如果我了解您希望仅在单击 submit_button 时运行验证,您可以使用您的代码进行更改。否则,我希望您了解如何根据您的确切需求对其进行自定义。

    Model:
    # You can change the context to be a custom one, 
    # or use multiple e.g. on: [:update, :submitted]
    validates :tape_files, attached: true, on: :submitted
    
    
    def update
      # [1] Change to assign_attributes
      @tape.assign_attributes(tapes_params)
    
      if params[:submit_button] 
        # [2] This will run all validations without an explicit
        # context PLUS the `:submitted` ones.
        if @tape.save(context: :submitted)
          redirect_to tape_file_tape_path(@tape.id), notice: "Attachment Saved!"
        else
          render 'edit', notice: "Update failed"
        end
      else
        # [3] This will run all validations without an explicit context.
        if @tape.save
          if params[:save_button]
            redirect_to tapes_path, notice: "Saved!"
          elsif params[:check_button]
            redirect_to tapes_path, notice: "Checked!"
          else
            # [Optional] You may want to consider handling a default redirect here
          end
        else
          render 'edit', notice: "Update failed"
        end
      end
    end
    

    【讨论】:

    • save! 如果失败,将引发 ActiveRecord::RecordInvalid,这意味着您应该有一个处理程序来避免出现“我们很抱歉出了点问题”页面
    • 是的@engineersmnky 这是故意的,因为在没有其他代码更改的情况下,他的代码目前正在默默地失败并给出误报。
    • 我并不是说你的代码是一个坏主意,它只是一个关于失败时会发生什么的评论,并提供解决该问题的行动方案。该评论更多是针对 OP 而不是您自己,因为它可能是实施您的提案后的下一个问题。
    • 如果我单击“submit_button”而不是其他按钮,则“tape_files”已得到验证。精彩的!非常感谢,马修!但是我无法显示@engineersmnky 所写的错误消息。所以,我稍微修改了控制器。我将其添加为答案。
    • @frankyorg 我更新了我的答案,以便您了解如何显示错误。在您的回答中,您实际上并没有保存记录。
    【解决方案2】:

    我解决了这个问题如下:

    Model:
    validates :tape_files, attached: true, on: :submit_button
    
    Controller:
    def update
     @tape.assign_attributes(tapes_params)   
      if params[:submit_button]     
         @tape.valid?(:submit_button)
         if !@tape.errors.any?
            redirect_to tape_file_tape_path(@tape.id), notice: "Attachment Saved!"
         else
            render 'edit', notice: "Update failed"
         end
        
      elsif params[:save_button]
          redirect_to tapes_path, notice: "Saved!"
      elsif params[:check_button]
          redirect_to tapes_path, notice: "Checked!"
      end
      @tape.save
    end
    

    【讨论】:

      猜你喜欢
      • 2016-10-20
      • 1970-01-01
      • 1970-01-01
      • 2016-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多