Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8259859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:57:34+00:00 2026-06-08T02:57:34+00:00

I want to implement blog\news application with ability to: show all posts at root:

  • 0

I want to implement blog\news application with ability to:

  1. show all posts at root: example.com/
  2. show all posts answering some year: example.com/2012/
  3. show all posts answering some year and month: example.com/2012/07/
  4. show some post by its date and slug: example.com/2012/07/slug-of-the-post

So I have created a mockup for routes.rb file:

# GET /?page=1
root :to => "posts#index"

match "/posts" => redirect("/")
match "/posts/" => redirect("/")

# Get /posts/2012/?page=1
match "/posts/:year", :to => "posts#index",
  :constraints => { :year => /\d{4}/ }

# Get /posts/2012/07/?page=1
match "/posts/:year/:month", :to => "posts#index",
  :constraints => { :year => /\d{4}/, :month => /\d{1,2}/ }

# Get /posts/2012/07/slug-of-the-post
match "/posts/:year/:month/:slug", :to => "posts#show", :as => :post,
  :constraints => { :year => /\d{4}/, :month => /\d{1,2}/, :slug => /[a-z0-9\-]+/ }

So I should work with params in index action and just get post by slug in show action (checking whether date is corect is an option):

# GET /posts?page=1
def index
  #render :text => "posts#index<br/><br/>#{params.to_s}"
  @posts = Post.order('created_at DESC').page(params[:page])
  # sould be more complicated in future
end

# GET /posts/2012/07/19/slug
def show
  #render :text => "posts#show<br/><br/>#{params.to_s}"
  @post = Post.find_by_slug(params[:slug])
end

Also I have to implement to_param for my model:

def to_param
  "#{created_at.year}/#{created_at.month}/#{slug}"
end

This is all I have learned from all night long searching in api/guides/SO.

But the problem is strange things keep happenning for me as new to rails:

  1. When I go to localhost/, the app breaks and says that it had invoked show action but first object in database had been recieved as :year (sic!):

    No route matches {:controller=>"posts", :action=>"show", :year=>#<Post id: 12, slug: "*", title: "*", content: "*", created_at: "2012-07-19 15:25:38", updated_at: "2012-07-19 15:25:38">}
    
  2. When I go to localhost/posts/2012/07/cut-test same thing happens:

    No route matches {:controller=>"posts", :action=>"show", :year=>#<Post id: 12, slug: "*", title: "*", content: "*", created_at: "2012-07-19 15:25:38", updated_at: "2012-07-19 15:25:38">}
    

I feel that there is something very easy that I haven’t made, but I can’t find what is it.

Anyway, this post will be helpful when it is solved, because there are solutions only for just slugs in url without date and similar but not useful questions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:57:35+00:00Added an answer on June 8, 2026 at 2:57 am

    The problem was in post’s path helper usage as post_path(post), because first parameter must be year since I use :as => :post in parametrized match in routes.rb.

    Nevertheless, to make the entire solution clear here are some actions needed to make that all work proper:

    1. You must add proper path helpers names for each match, e.g.

      # Get /posts/2012/07/slug-of-the-post
      match "/posts/:year/:month/:slug", <...>,
        :as => :post_date
      

      Now you can use post_date_path("2012","12","end-of-the-world-is-near") in views.

      Same for posts_path, posts_year_path("2012"), posts_month_path("2012","12") if properly named.

      I advice not to use neither :as => :post in that match nor creating to_param in model file as it can break something you don’t expect (as active_admin for me).

    2. Controller file posts-controller.rb should be filled with posts that needed extraction and checking of date’s correctness before slug. Nevertheless in this state it is OK and breaks nothing.

    3. Model file posts.rb should be filled with year and month extraciton in proper format, e.g.:

      def year
        created_at.year
      end
      
      def month
        created_at.strftime("%m")
      end
      

      There is no to_param method really needed as I’ve noticed already.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to implement a simple script to do some boring housekeeping on my
I want to implement an application which will work as a parser. User will
I want to implement a job scheduler in my windows azure application. My aim
I want to implement login using facebook in my windows phone 7.1 application When
It's time to implement sorting on my blog-like web application. In addition to browsing
I am creating an web application which allows user to post some article/blog on
I'm about to implement a blog, and I'm pretty sure I want to go
I'm needing to implement some form of captcha support for comments on my blog.
I wanna implement url rewriting so that, for example, all german pages have a
I want to implement a blog in my site with usernames as subdomains like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.