I want to implement blog\news application with ability to:
- show all posts at root:
example.com/
- show all posts answering some year:
example.com/2012/
- show all posts answering some year and month:
example.com/2012/07/
- 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:
When I go to
localhost/
, the app breaks and says that it had invokedshow
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">}
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.