简体   繁体   English

如何使用户上传的可点击PDF?

[英]How to make clickable pdf from user upload?

I'm making a restaurant app and I'm having trouble having users able to upload PDFs for the menus while being able to click to download on my index and show pages. 我正在制作一个餐厅应用程序,但我无法让用户能够上传菜单的PDF,同时又能够单击以下载索引并显示页面。 Here is my table on my index.html.erb: 这是我index.html.erb上的表格:

<tbody >
    <% @restaurants.each do |restaurant| %>
    <tr>
        <td><%= restaurant.name %></td>
        <td><%= restaurant.description %></td>
        <td><%= restaurant.phone_number %></td>
        <td><%= restaurant.address %></td>
        <td><%= image_tag(restaurant.picture_url, :width => 300) if restaurant.picture.present? %> </td>
        <td><%= link_to(restaurant.menu) if restaurant.menu.present? %> </td>

        <td><%= link_to 'Show', restaurant %></td>
        <td><%= link_to 'Edit', edit_restaurant_path(restaurant) %></td>
        <td><%= link_to 'Destroy', restaurant, method: :delete, data: { confirm: 'Are you sure?' } %></td>
    </tr>
    <% end %>
</tbody>

Here is the show page relevant code: 这是显示页面的相关代码:

<p>
    <strong>Look at the Menu:<strong>
    <%= link_to(@restaurant.menu) if @restaurant.menu.present? %>

</p>

Also, does the controller need to be updated too? 另外,控制器也需要更新吗?

Updated with controller: 用控制器更新:

class RestaurantsController < ApplicationController 
     before_action :set_restaurant, only: [:show, :edit, :update, :destroy]



def index
    @restaurants=Restaurant.all
end

def show
end

def new
    @restaurant = Restaurant.new 
end

def edit
end

def create
    @restaurant = Restaurant.new(restaurant_params)

    respond_to do |format|
        if @restaurant.save
            format.html {redirect_to @restaurant, notice: 'Restaurant was successfully created.'}
            format.json { render :show, status: :created, location: @restaurant}
        else
            format.html {render :new }
            format.json {render json: @restaurant.errors, status: :unprocessable_entity }
        end
    end
end


def update
    respond_to do |format|
        if @restaurant.update(restaurant_params)
        format.html { redirect_to @restaurant, notice: 'Restaurant was successfuly updated.'}
        format.json { redner :show, status: :ok, location: @restaurant }
        else 
        format.html { render :edit }
        format.json { render json: @restaurant.errors, status: :unprocessable_entity}
        end
    end
end


def destroy 
    @restaurant.destroy
    respond_to do |format|
        format.html { redirect_to restaurants_url, notice: 'Restaurant was destorys.'}
        format.json { head :no_content }
    end
end




private

def set_restaurant
    @restaurant = Restaurant.find(params[:id])
end




def restaurant_params
    params.require(:restaurant).permit(:name, :description, :address, :phone_number, :picture, :menu)
end


end

Routes: 路线:

Rails.application.routes.draw do
  resources :restaurants

root :to => redirect('/restaurants')

You will have to do a little more to make your file upload work. 您将需要做更多的工作才能使文件上载工作。 Please read the corresponding Rails guide which will tell you, that the upload-field will provide you with an IO object. 请阅读相应的Rails指南 ,该指南将告诉您,上载字段将为您提供IO对象。 This is not your generic attribute, you will have to handle it in some other way (eg by using the paperclip gem). 这不是您的一般属性,您将不得不以其他方式处理它(例如,使用paperclip gem)。 Your uploaded file will not just be stored through your save or update calls in the controller, you will have to add some code for that, but paperclip will do most of the hard work. 你上传的文件将只是通过你的存储saveupdate控制器中的电话,则必须添加一些代码,这一点,但paperclip会做最困难的工作。

Additionally you will have to provide some more logic to display a link to your menu as this will have to be served through another action, you will have to provide a separate route for handling your files. 另外,您将必须提供更多逻辑来显示菜单的链接,因为必须通过其他操作来提供此链接,还必须提供处理文件的单独途径。 Usually you will not want to expose your internally stored filename to the user, it should just be stored into your model. 通常,您不希望将内部存储的文件名公开给用户,而应将其存储在模型中。 As I have not used paperclip to do this myself I can hardly advise you, but the docs on github will get you a long way. 由于我自己没有使用paperclip来完成此操作,因此我几乎不建议您使用github ,但是github上文档会为您提供很多帮助。 It provides additional information on how to specify the attachment for the database, how to configure your file store and so on. 它提供有关如何为数据库指定附件,如何配置文件存储等的其他信息。 You may also refer to this question for an example on how to upload a PDF and provide a link to it. 您也可以参考此问题以获取有关如何上载PDF并提供指向它的链接的示例。 Make sure to look at rake routes once you add the paperclip gem, I am pretty sure it will add some routing for retrieving your files. 添加paperclip gem后,请务必查看一下rake routes ,我很确定它将添加一些用于检索文件的路由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM