1

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. Here is my table on my 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')
jpn
  • 285
  • 2
  • 15
  • Thats petty little information to be helpful, we won't be able to tell you much by just seeing your view. Please add the HTML your `link_to` is generating and the corresponding part of your routes file. If your `restaurant.menu` field contains a name of an uploaded file you would have to make sure that this routes to one of your controller actions that can actually retrieve and serve the file. I somewhat doubt you can do that with a single call (and without a custom helper) and still get an HTML page that looks readable. Please post the controller action too. – Patru Jul 14 '14 at 13:32
  • How do you expect your `rails` server to find the menu? There does not seem to be an entry in the `routes.rb`-file, though it might be associated through another model. Then there might be a possibility that a sensible link is rendered, though probably not for a `pdf` format. Could you show us the HTML generated from the `link_to`? Is there a specific `Menu` model? – Patru Jul 14 '14 at 15:49
  • Sorry for the novice nature of this post...I'm a beginner. But this intended to show a file uploaded by the user - I'm not sure if this information helps or not. I'm trying to figure out how to display the file that the user uploaded and would be stored in the database? – jpn Jul 14 '14 at 16:18

1 Answers1

0

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. This is not your generic attribute, you will have to handle it in some other way (e.g. by using the 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.

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. 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. 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.

Community
  • 1
  • 1
Patru
  • 4,481
  • 2
  • 32
  • 42