Search code examples
ruby-on-railscontrollerurl-routing

Rails: Image Uploading Site. Home page


class PhootosController < ApplicationController

before_action :logged_in_user

def index
  @phootos = Phooto.all.sample(1)
end

def new
end

def show
  @phooto = Phooto.find(params[:id])
end

def create
  @phooto = current_user.phootos.build(phooto_params)
   if @phooto.save
     flash[:success] = "Photo created!"
     redirect_to uploads_url
   else
     redirect_to root_url
   end  
end

def favorite
  @phooto = Phooto.find params[:id]

  if request.put?
    current_user.favorites << @phooto
      redirect_to :back, notice: 'You successfully favorited #{@phooto.name}'
  elsif request.delete?
    current_user.favorites.delete(@phooto)
      redirect_to :back, notice: 'You successfully unfavorited #{@phooto.name}'
  else
      redirect_to :back, notice: 'Nothing happened.'
  end    
end

def feed
  @favorites = current_user.favorites.all 
end  

def uploaded
  @phootos = current_user.phootos.all
end  

  private

 def phooto_params
   params.require(:phooto).permit(:picture)
 end
 end

show.html.erb

<p><strong>Picture:</strong></p> 

<%= image_tag(@phooto.picture) %>

<%= link_to("< Previous", @phooto.previous) if @phooto.previous %>
<%= link_to("Next >", @phooto.next) if @phooto.next %>

<% if current_user %>
<%= link_to "favorite",   favorite_phooto_path(@phooto), method: :put %>
<%= link_to "unfavorite", favorite_phooto_path(@phooto), method: :delete %>

<% end %>

www.example.com/photos/1    

Professional websites have the homepage www.example.com load with a photo already displayed. Then when you click next/previous, you are routed to www.example.com/photos/#{photo_id}

How do I get my website to do this? I also want it setup so that each day a different random photo is displayed in the homepage.


Solution

  • If you want to show a random photo on the homepage, you need the following:

    #config/routes.rb
    resources :photos, only: [:index, :show]
    
    #app/controllers/photos_controller.rb
    class PhotosController < ApplicationController
       def index
          random  = ActiveRecord::Base.connection.adapter_name == 'MySQL' ? "RAND()" : "RANDOM()"
          @phooto = Photo.order(random).first
       end
    
       def show
          @phooto = Photo.find params[:id]
       end
    end
    

    Refs:

    --

    This will allow you to populate the @phooto variable in both the index and show actions;

    #app/views/photos/index.html.erb
    <%= render @phooto %>
    
    #app/views/photos/show.html.erb
    <%= render @phooto %>
    
    #app/views/photos/_photo.html.erb
    <p><strong>Picture:</strong></p> 
    
    <%= image_tag(photo.picture) %>
    
    <%= link_to("< Previous", photo.previous) if @phooto.previous %>
    <%= link_to("Next >", photo.next) if @phooto.next %>
    
    <% if current_user %>
       <%= link_to "favorite",   favorite_phooto_path(phooto), method: :put %>
       <%= link_to "unfavorite", favorite_phooto_path(phooto), method: :delete %>
    <% end %>
    

    You'll be able to work out the rest.


    In regards rendering a new random image each day, you'll have to store the "random" value each time the day changes.

    This would be best done with a cron job, invoking a rake task:

    #lib/tasks/new_random.rb
    namespace :random do
       task :generate => :environment do
          # save random for the day (maybe in a model or redis)
       end
    end