RubyonRails:有没有更好的方法来实现我开发的这个“预览”功能?

frebpwbc  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(356)

我的应用程序中有这个“预览”功能。该按钮出现在编辑页面中,单击该按钮时,用户将进入一个新页面,显示打印/下载表单时的外观。此“预览”功能将在表单中显示数据,而不保存到数据库。例如,如果我将“备注:字段值”从“备注”更改为“已编辑的备注”,则在预览页面中将显示“已编辑的备注”,但在数据库中,该值仍然是“备注”。
我已经实现了这一点,我将表单数据发送到preview方法,然后通过 assign_attributes() . 问题是,表单数据包含太多嵌套属性,因此链接太长,顺便说一句,它是get方法。我认为这不是一件好事,不是吗?那么,有没有其他方法可以在url中不显示参数的情况下实现这一点?还是在链路分拣机中生成参数的方法?
这是代码,很抱歉没有使用我的实际代码,但逻辑是相同的
编辑url: /modul/:id/edit 预览url: /modul/:id/preview?long_parameter 看法

<%= button_tag 'Preview', type: "button", class: "btn btn-outline-success mr-1", id: "preview" %>

$("#preview").click(function(){
  var params = $("#record_form").serialize()
  window.location = "preview?" + params
})

控制器

def preview
  # I'm using this logic 'cause @existing_record.assign_attributes(record_params) didn't update the children and grandchildren attributes
  @existing_record.children.each do |child|
    child_params = record_params[:children_attributes].values.select{|v| v[:id].to_i == child.id}.first
    child.assign_attributes(child_params.except(:grandchildren_attributes))
    child.grandchildren.each do |granchild|
      grandchild_params = child_params[:grandchildren_attributes].values.select{|v| v[:id].to_i == grandchild.id}.first
      grandchild.assign_attributes(grandchild_params)
    end
  end
end
jtw3ybtb

jtw3ybtb1#

为此,我将使用浏览器localstorage并序列化深度嵌套的数据结构,以便在用户准备保存到数据库之前,所有内容都保留在前端。

相关问题