如何验证取决于不同的提交按钮
How to validation depend on different submit buttons
我的应用程序有“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”验证修改验证吗?
我很难在这里提供更多的代码修复,因为我不了解您的代码和域的整个用例,但您是否探索过 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
我解决了这个问题如下:
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
我的应用程序有“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”验证修改验证吗?
我很难在这里提供更多的代码修复,因为我不了解您的代码和域的整个用例,但您是否探索过 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
我解决了这个问题如下:
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