Ruby on Rails 7 渲染 .rtf.erb 模板

huangapple go评论99阅读模式
英文:

Ruby on Rails 7 rendering .rtf.erb template

问题

我是新手学习Ruby on Rails,我正在尝试渲染.rtf.erb模板,我了解到这需要一些第三方插件或库,我找到了类似rtf gem库rrtf库,但我想用不同的方式实现。

我有一个位于./views/order.rtf.erb的模板,我正在将订单详情发送/渲染到这个模板,并希望以RTF格式下载。

内容如下,带有Ruby标签:

.
.
.
\trowd \trgaph108\trleft-108\trbrdrt\brdrs\brdrw10 \trbrdrl\brdrs\brdrw10 \trbrdrb\brdrs\brdrw10 \trbrdrr\brdrs\brdrw10 \trbrdrh\brdrs\brdrw10 \trbrdrv\brdrs\brdrw10 \clvertalt\clbrdrt\brdrs\brdrw10 \clbrdrl\brdrs\brdrw10 \clbrdrb\brdrs\brdrw10 \clbrdrr
\brdrs\brdrw10 \cltxlrtb \cellx3690\clvertalt\cltxlrtb \cellx5940\clvertalt\clbrdrt\brdrs\brdrw10 \clbrdrl\brdrs\brdrw10 \clbrdrb\brdrs\brdrw10 \clbrdrr\brdrs\brdrw10 \cltxlrtb \cellx8910\pard \nowidctlpar\widctlpar\intbl\adjustright {\fs16 
\par }\pard\plain \s15\nowidctlpar\widctlpar\intbl\adjustright \fs16\cgrid {\f1 SOLD TO:
\par }\pard\plain \nowidctlpar\widctlpar\intbl\adjustright \fs20\cgrid {\f1 
\par <%= @client_addr.addr1 rescue 'N/A'%>
\par <%= @client_addr.addr2 rescue 'N/A'%>
\par <%= @client_addr.addr3 rescue 'N/A'%>
\par <%= @client_addr.city rescue 'N/A'%>, <%= @client_addr.state rescue 'N/A'%> <%= @client_addr.zipcode rescue 'N/A'%>
\par }{\fs16 \cell }{\cell }{\fs16 
\par }\pard\plain \s15\nowidctlpar\widctlpar\intbl\adjustright \fs16\cgrid {\f1 SHIPPED TO:
\par }\pard\plain \nowidctlpar\widctlpar\intbl\adjustright \fs20\cgrid {\f1 
\par <%= @shipping_addr.addr1 rescue 'N/A'%>
\par <%= @shipping_addr.addr2 rescue 'N/A'%>
\par }\pard\plain \s16\nowidctlpar\widctlpar\intbl\adjustright \fs20\cgrid {\f1 <%= @shipping_addr.addr3 rescue 'N/A'%>
\par }\pard\plain \nowidctlpar\widctlpar\intbl\adjustright \fs20\cgrid {\f1 <%= @shipping_addr.city rescue 'N/A'%>, <%= @shipping_addr.state rescue 'N/A'%> <%= @shipping_addr.zipcode rescue 'N/A'%>
\par }{\fs16 \cell }\pard \nowidctlpar\widctlpar\intbl\adjustright {\row }

\pard \nowidctlpar\widctlpar\adjustright { \par }
.
.
.

我的控制器函数:

def show
    @order = Order.find(params[:id])
    raise NotFound unless @order
    if params[:format] == 'rtf'
      latest_order = Order.find_by(order_type: 'o', customer_id: @order.customer_id)
      if latest_order.nil?
        @client_addr = Customer.find(@order.customer_id).addresses[0]
        @shipping_addr = @client_addr
      else
        @client_addr = latest_order.client_address
        @shipping_addr = latest_order.shipping_address
      end

      # 构建一个表示RTF文档的对象
      rtf = RRTF::Document.new

      #==========================================
      #这里我不知道该怎么做...
      #==========================================

      #我做的
      rtf = File.read(Rails.root.to_s+ "/app/views/order.rtf.erb")
      # 将文档转换为RTF字符串
      send_data rtf, filename: 'order', type: 'application/rtf'

      #render template: "order.rtf.erb", formats: :rtf, layout: false
  else
    render
  end
end

输出:
Ruby on Rails 7 渲染 .rtf.erb 模板

英文:

I am new in Ruby on Rails and I am trying to render .rtf.erb template and I understand it requires some third party plugins or libraries and I found something like rtf gem library, rrtf library but I am looking to do something in different way.

I have template in ./views/order.rtf.erb and I am sending/rendering order details to this template and want to download in RTF format.

The content looks like below with ruby tags:

.
.
.
\trowd \trgaph108\trleft-108\trbrdrt\brdrs\brdrw10 \trbrdrl\brdrs\brdrw10 \trbrdrb\brdrs\brdrw10 \trbrdrr\brdrs\brdrw10 \trbrdrh\brdrs\brdrw10 \trbrdrv\brdrs\brdrw10 \clvertalt\clbrdrt\brdrs\brdrw10 \clbrdrl\brdrs\brdrw10 \clbrdrb\brdrs\brdrw10 \clbrdrr
\brdrs\brdrw10 \cltxlrtb \cellx3690\clvertalt\cltxlrtb \cellx5940\clvertalt\clbrdrt\brdrs\brdrw10 \clbrdrl\brdrs\brdrw10 \clbrdrb\brdrs\brdrw10 \clbrdrr\brdrs\brdrw10 \cltxlrtb \cellx8910\pard \nowidctlpar\widctlpar\intbl\adjustright {\fs16 
\par }\pard\plain \s15\nowidctlpar\widctlpar\intbl\adjustright \fs16\cgrid {\f1 SOLD TO:
\par }\pard\plain \nowidctlpar\widctlpar\intbl\adjustright \fs20\cgrid {\f1 
\par <%= @client_addr.addr1 rescue 'N/A'%>
\par <%= @client_addr.addr2 rescue 'N/A'%>
\par <%= @client_addr.addr3 rescue 'N/A'%>
\par <%= @client_addr.city rescue 'N/A'%>, <%= @client_addr.state rescue 'N/A'%> <%= @client_addr.zipcode rescue 'N/A'%>
\par }{\fs16 \cell }{\cell }{\fs16 
\par }\pard\plain \s15\nowidctlpar\widctlpar\intbl\adjustright \fs16\cgrid {\f1 SHIPPED TO:
\par }\pard\plain \nowidctlpar\widctlpar\intbl\adjustright \fs20\cgrid {\f1 
\par <%= @shipping_addr.addr1 rescue 'N/A'%>
\par <%= @shipping_addr.addr2 rescue 'N/A'%>
\par }\pard\plain \s16\nowidctlpar\widctlpar\intbl\adjustright \fs20\cgrid {\f1 <%= @shipping_addr.addr3 rescue 'N/A'%>
\par }\pard\plain \nowidctlpar\widctlpar\intbl\adjustright \fs20\cgrid {\f1 <%= @shipping_addr.city rescue 'N/A'%>, <%= @shipping_addr.state rescue 'N/A'%> <%= @shipping_addr.zipcode rescue 'N/A'%>
\par }{\fs16 \cell }\pard \nowidctlpar\widctlpar\intbl\adjustright {\row }

\pard \nowidctlpar\widctlpar\adjustright { \par }
.
.
.

My controller's function:

def show
    @order = Order.find(params[:id])
    raise NotFound unless @order
    if params[:format] == 'rtf'
      latest_order = Order.find_by(order_type: 'o', customer_id: @order.customer_id)
      if latest_order.nil?
        @client_addr = Customer.find(@order.customer_id).addresses[0]
        @shipping_addr = @client_addr
      else
        @client_addr = latest_order.client_address
        @shipping_addr = latest_order.shipping_address
      end

      # Construct an object representing the RTF document
      rtf = RRTF::Document.new

      #==========================================
      #here I do not have any idea what to do...
      #==========================================
      
      #what i did
      rtf = File.read(Rails.root.to_s+ "/app/views/order.rtf.erb")
      # Convert document into RTF string
      send_data rtf, filename: 'order', type: 'application/rtf'

      #render template: "order.rtf.erb", formats: :rtf, layout: false
  else
    render
  end
end

Output:
Ruby on Rails 7 渲染 .rtf.erb 模板

答案1

得分: 1

如果不需要动态生成RTF文件,就不需要额外的库。在你的示例中,你有一个包含嵌入ERB的RTF文件,所以只需要将该文件放入ERB中,并让它发挥作用。

类似这样的代码应该可以工作:

def show
  # ...

  rtf = File.read(File.join(Rails.root, "/app/views/order.rtf.erb"))
  # 将文件加载为ERB模板
  erb_template = ERB.new(rtf)
  # 执行生成的ERB代码以生成完成的模板
  output = erb_template.result(binding)
  # 将文档转换为RTF字符串
  send_data output, filename: 'order', type: 'application/rtf'

  # ...
end
英文:

There is no need for extra libraries if the RTF file will not be generated dynamically. In your example you have an RTF file with embedded ERB, so it's just a question of putting that file into ERB and and letting it do its magic.

Something like this should work:

def show
  # ...

  rtf = File.read(File.join(Rails.root, "/app/views/order.rtf.erb"))
  # Load the file as an ERB template
  erb_template = ERB.new(rtf)
  # Executes the generated ERB code to produce a completed template
  output = erb_template.result(binding)
  # Convert document into RTF string
  send_data output, filename: 'order', type: 'application/rtf'

  # ...
end

huangapple
  • 本文由 发表于 2023年8月9日 01:21:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861870.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定