Topic: Trying to create a site with a homepage that changes every day

I am designing a site that has a new picture on the homepage every day.  The problem is that I can not see clearly how to do it.  How would you code the site to, basically, load a new homepage-pic everyday?  I could really use some guidance (links to tutorials, etc) on how to:

(1) set up the database, controllers, views, etc. to automatically pull the new picture everyday.

(2) schedule images ahead of time to be automatically updated on the homepage.

Specifically, what are the crucial code lines?  I am assuming something involving "date.today."
Using: mysql

I am assuming the solution will involve something with a filename matched to "date.today."

Thank you.  This is my first question on Rails forum.

Last edited by Starflyer59 (2011-01-09 07:53:28)

I'm so abstract that automatic doors at grocery stores do not even open for me. - @thatdankent

Re: Trying to create a site with a homepage that changes every day

In your page controller

def home 
   t = Time.new   
   @num = t.day
end

in your view:  <%= image_tag("/images/"+@num.to_s + ".jpg") %>

go to your folder /images and name the pics 1.jpg, 2.jpg, ....

Last edited by Henrik41 (2011-01-10 17:47:12)

Re: Trying to create a site with a homepage that changes every day

That is very helpful.  Follow-up questions:

(1) Does @num.to_s represent how many days of the year have passed?  Or how many days of the month? 

(2) similarily, how do I account for change of month/year? 

Thank you Henrik41!

I'm so abstract that automatic doors at grocery stores do not even open for me. - @thatdankent

Re: Trying to create a site with a homepage that changes every day

@num.to_s  ---   @num represent each day of the month. If the months has 30 days then it will be 1..30 or else it will be 1..31 if it's 31 day. with to_s you simply put the day and convert it to a string.

@num is independant of the month or year. If you want to store pictures of many months and have it change each day of the year then the technique is a bit different and you have to use Time.month

Re: Trying to create a site with a homepage that changes every day

I was able to get the image to display every day.  Now I am trying to figure out how to get the image to be from a database.  I would like users to be able to comment on the image and to have those comments stored with the image. 

Any guidance or advice would be appreciated.

Last edited by Starflyer59 (2011-01-21 05:42:21)

I'm so abstract that automatic doors at grocery stores do not even open for me. - @thatdankent

Re: Trying to create a site with a homepage that changes every day

You can use paperclip for your attachments. Paperclip has an option for data storage of files like pictures. Here is a simple tutorial: http://patshaughnessy.net/2009/5/29/pap … lob-column

For comments, just create a comment model and associate it (has_many :comments) to your pic model from your album. This is the way to do it:
http://sleekd.com/general/adding-multip … paperclip/

Re: Trying to create a site with a homepage that changes every day

Well, paperclip seems great.  But now I have lost the ability to have the images automatically load each day.  Can I still do it? 

The tag: <%= image_tag("/images/"+@num.to_s + ".jpg") %> is no longer working.


I am trying to figure it out, but feel like I am going in circles.  Any help would be appreciated

Last edited by Starflyer59 (2011-01-21 07:28:57)

I'm so abstract that automatic doors at grocery stores do not even open for me. - @thatdankent

Re: Trying to create a site with a homepage that changes every day

Why would you want to store your pictures in a database anyway (unless there are tons of datas) ?

There are plenty of ways to do that:

Just define a model which is going to be called day  (rails g model day if your on rails 3)
then define the day in your model ( t = Time.new   @num = t.day) and don't forget to convert it to string (.to_s)

Put the attached avatar paperclip statement in it.
and then follow the instruction in the tutorial
and change the view like this

<%= image_tag(@day.avatar.url) %>

If you want to strore more then a month, then add def month and def myday in your model and call @day.month.myday.avatar.url

I didn't test it but it should theoreticaly work.
The best way is to experiment

Last edited by Henrik41 (2011-01-21 17:02:04)

Re: Trying to create a site with a homepage that changes every day

Thanks Henrik41.  I do not necessarily want the images "in" a database.  All I want is a way to call a new picture to the homepage each day and to be able to have people comment on that picture (and for those comments to remain associated with that day's picture). 

I am going to try your most recent suggestion.

I'm so abstract that automatic doors at grocery stores do not even open for me. - @thatdankent

Re: Trying to create a site with a homepage that changes every day

Ok .. that sounds easy in Rails 3:

Rails new Dailypic
cd dailypic
Rails g scaffold Comment comment:text pic_id:integer
Rails g model Pic
rails plugin install git://github.com/thoughtbot/paperclip.git
include in your gem file : gem "paperclip"
Bundle install
rails g migration add_avatarcolums_to_pic
rake db:migrate

in your addavatartcolumntopic:
------------------------------------
class AddAvatarcolumsToPic < ActiveRecord::Migration
  def self.up
    add_column :pics, :avatar_file_name,    :string
    add_column :pics, :avatar_content_type, :string
    add_column :pics, :avatar_file_size,    :integer
    add_column :pics, :avatar_updated_at,   :datetime
  end

  def self.down
    remove_column :pics, :avatar_file_name
    remove_column :pics, :avatar_content_type
    remove_column :pics, :avatar_file_size
    remove_column :pics, :avatar_updated_at
  end
end

Model
--------

class Pic < ActiveRecord::Base
  has_many :comments
  has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end


class Comment < ActiveRecord::Base
    belongs_to :pic 
end

Controller
-------------

In the comments controller add a private method
private
  def picoftheday
    t = Date.today
   
    @pic = Pic.first(:conditions => {:picdate => t})
  end

in the beginning of the same controller add a before_filter like this:
  before_filter :picoftheday

so the comment controller looks like this:
class CommentsController < ApplicationController
  # GET /comments
  # GET /comments.xml
  before_filter :picoftheday
 
  def index
   
    @comments = @pic.comments.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @comments }
    end
  end

  # GET /comments/1
  # GET /comments/1.xml
  def show
    @comment = @pic.comments.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @comment }
    end
  end

  # GET /comments/new
  # GET /comments/new.xml
  def new

   
    @comment = @pic.comments.build
    @comments = @pic.comments.all
   
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @comment }
    end
  end

  # GET /comments/1/edit
  def edit
    @comment = @pic.comments.find(params[:id])
  end

  # POST /comments
  # POST /comments.xml
  def create
    @comment = @pic.comments.create(params[:comment])

    respond_to do |format|
      if @comment.save
        format.html { redirect_to(@comment, :notice => 'Comment was successfully created.') }
        format.xml  { render :xml => @comment, :status => :created, :location => @comment }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @comment.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /comments/1
  # PUT /comments/1.xml
  def update
    @comment = @pic.comments.find(params[:id])

    respond_to do |format|
      if @comment.update_attributes(params[:comment])
        format.html { redirect_to(@comment, :notice => 'Comment was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @comment.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /comments/1
  # DELETE /comments/1.xml
  def destroy
    @comment = @pic.comments.find(params[:id])
    @comment.destroy

    respond_to do |format|
      format.html { redirect_to(comments_url) }
      format.xml  { head :ok }
    end
  end
private
  def picoftheday
    t = Date.today
   
    @pic = Pic.first(:conditions => {:picdate => t})
  end
 
end

As you notice we implemented the connection between Pic and Comments at all levels.

In your views:

For example your new comment view could look like this:

<%= image_tag @pic.avatar.url %>

<% @comments.each do |comment| %>
 
<br> <%= comment.comments %></br>

<% end %>


<%= render 'form' %>

And don't forget to replace Pic_id field by a hidden field in your form comment partial ( <%= f.hidden_field :pic_id %>)

Anyway this should do the basic job.

Last edited by Henrik41 (2011-01-24 17:04:46)

Re: Trying to create a site with a homepage that changes every day

Wow.  I'm speechless.  Thanks Henrick41.  I'll let you know how it works.

I'm so abstract that automatic doors at grocery stores do not even open for me. - @thatdankent

Re: Trying to create a site with a homepage that changes every day

hi,
i have done my home page with image changing every day by calling the id to home page.
but now i want to make a 3 image box with changing according to day. previously i have done by calling the id,so that all the 4 images are showing the sane id image.how i can made it possible by calling image id from database for seperate image in same home page.

thanks

Re: Trying to create a site with a homepage that changes every day

Well, Johpaul, I do not understand what you are looking for.  Does the 3 image box change every day?  Or just a single image in the box?  Where does the 4th image come from?   Not sure what id image means, either.  Need more specifics.

I'm so abstract that automatic doors at grocery stores do not even open for me. - @thatdankent

Re: Trying to create a site with a homepage that changes every day

hi,
i have already made a picture to change it every day.by find by sql and return it id to home page controller.so that it gets the id and show the image.but now i want to show another 3 image in same home page.but i am getting the same image in 3 box as same as the previous defined image.so how i can get the required image in my field.

thank u

Re: Trying to create a site with a homepage that changes every day

This is a different issue than the one I am dealing with.  I would need to see your code (controllers and views).  I recommend you post your own question.

I'm so abstract that automatic doors at grocery stores do not even open for me. - @thatdankent

Re: Trying to create a site with a homepage that changes every day

Interesting thread. I like the suggestions and views of all the members. It contains useful information. Thanks for sharing this thread with me.

vastu consultant | vastu | SEO services in delhi | Aishwaraya Rai Bachchan's delivery predictions

Last edited by aquariusmediaaseo (2011-11-10 08:56:15)

Re: Trying to create a site with a homepage that changes every day

Why are you trying reinvented the wheel?
Use any CMS ie Joomla! and you will get a lot of modules that do that automatically, all you will have to do is pick the pictures




---
dgt examenes

Last edited by skunworks (2012-02-02 06:52:16)