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')