我在code.I問題需要時,用戶會在下載鏈接點擊提供的索引頁面的PDF文件將生成並在同一時間將下載使用Rails 3.I我使用蝦寶石來生成PDF。但我不知道什麼應該是rails中的link_to標記中的路徑路徑。請檢查下面的代碼。生成和使用下載PDF文件的Rails 3
產品/ index.html.erb:
<h1>Choose the option</h1>
<p>
<%= link_to "new input",products_new_path %>
</p>
<table>
<tr>
<th>Product name</th>
<th>Product Catagory</th>
</tr>
<% @product.each do |p| %>
<tr>
<td><%= p.p_name %></td>
<td><%= p.p_catagory %></td>
</tr>
<% end %>
</table>
<%= link_to "Download Pdf" %>
控制器/ products_controller.rb:
class ProductsController < ApplicationController
def index
@product=Product.all
require "prawn/table"
require "prawn"
Prawn::Document.generate("test.pdf") do |pdf|
table_data = Array.new
table_data << ["Product name", "Product category"]
@product.each do |p|
table_data << [p.p_name, p.p_catagory]
end
pdf.table(table_data, :width => 500, :cell_style => { :inline_format => true })
end
end
def new
@product=Product.new
end
def create
@product=Product.new(params[:product])
if @product.save
flash[:notice]="Data submitted"
flash[:color]="valid"
redirect_to :action => "index"
else
flash[:alert]="Data could not finish"
flash[:color]="invalid"
render 'new'
end
end
end
的Gemfile:
source 'https://rubygems.org'
gem 'rails', '3.2.19'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'sqlite3'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platforms => :ruby
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# To use Jbuilder templates for JSON
# gem 'jbuilder'
# Use unicorn as the app server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger
# gem 'debugger'
gem 'prawn', '~> 1.3.0'
gem 'prawn-table', '~> 0.2.1'
的routes.rb:
Generate::Application.routes.draw do
root :to => "products#index"
get "products/new" => "products#new"
post "products/create" => "products#create"
end
我的要求是,當用戶點擊鏈接"Download pdf"
HTML表值將轉換爲PDF格式,它會生成需要打印和下載請幫助我解決這個問題。
您必須使用'send_data'或'send_file'的PDF數據與響應控制器的動作。您的'link_to'將使用與該控制器操作相對應的路由。這個控制器動作也可能是你想要放置你的PDF生成邏輯的地方。 –
@約旦:你可以在這裏說一下我如何運用你的邏輯? – satya