2015-05-25 42 views
1

我在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格式,它会生成需要打印和下载请帮助我解决这个问题。

+0

您必须使用'send_data'或'send_file'的PDF数据与响应控制器的动作。您的'link_to'将使用与该控制器操作相对应的路由。这个控制器动作也可能是你想要放置你的PDF生成逻辑的地方。 –

+0

@约旦:你可以在这里说一下我如何运用你的逻辑? – satya

回答

3

产品/ index.html.erb:

<%= link_to "Download_pdf", download_pdf_path(:format => 'pdf') %> 

的Gemfile:

gem 'prawn' 
gem 'prawn-table' 

routes.rb中,添加以下路由:

get "products/download_pdf" => "products#download_pdf", :as => 'download_pdf' 

不要捆绑安装。

products_controller.rb

require "prawn" 
    require "prawn/table" 

     def download_pdf 
     @product = Product.all 
     respond_to do |format| 
      format.pdf do 
      pdf = Prawn::Document.new 
      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 }) 
      send_data pdf.render, filename: 'test.pdf', type: 'application/pdf', :disposition => 'inline' 
      end 
     end 
     end 

这将生成您的PDF。

+0

在哪个文件中,我将把你最后的代码。 – satya

+0

@ user123:我没有按照你。我把代码最后一堆download_pdf方法里面控制器page.But点击该链接,没有PDF文件生成后。 – satya

+0

请检查更新的代码。 –