SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    RoR - URL params hash help

    Having a little both getting this to work. Wondering if someone can help. Basically I am trying to come up with the following url:

    http://localhost:3000/article/showall?date[month]=04&date[year]=2009

    This is going to form the basis for a news archive. However I want to do a few checks first to make sure what I expect is there in the URL. I have the following piece of code in my controller.

    The view prints the month and year variables fine when everything is in the url as above. However if one of the the year or month params part of the url is missing then no default month or year variables I set before the check aren't showen in the view.

    Can anyone help or recomend of a better way of doing this?

    Code:
    @month = 04 (Would be the current month)
    @year = 2009 (Would be the current year)
        
    if defined? params[:date][:month] then
       @month = params[:date][:month]
    end
        
    if defined? params[:date][:year] then
       @year = params[:date][:year]
    end

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After a bit of messing around I tried using an if blank? statement instead of an if defined one and it seems to have worked.

    Works exactly the way I want it to work now. If either the month or year variables are not set then it will default to the current month and year.

    Code:
    @month = params[:date][:month]
    @year = params[:date][:year]  
      
    if @month.blank? || @year.blank?
      @month = Time.now.month
      @year = Time.now.year
    end
    Now just need to check they are numeric and I can start with the query.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •