I want to implement a blog \ news application with the ability:
- show all messages from root:
example.com/ - show all messages responding for one year:
example.com/2012/ - show all posts for the year and month:
example.com/2012/07/ - show message by its date and bullet:
example.com/2012/07/slug-of-the-post
So, I created a layout for the file routes.rb:
root :to => "posts#index"
match "/posts" => redirect("/")
match "/posts/" => redirect("/")
match "/posts/:year", :to => "posts#index",
:constraints => { :year => /\d{4}/ }
match "/posts/:year/:month", :to => "posts#index",
:constraints => { :year => /\d{4}/, :month => /\d{1,2}/ }
match "/posts/:year/:month/:slug", :to => "posts#show", :as => :post,
:constraints => { :year => /\d{4}/, :month => /\d{1,2}/, :slug => /[a-z0-9\-]+/ }
So, I have to work with parameters in action indexand just get post by slug in action show(checking if date corct is an option):
def index
@posts = Post.order('created_at DESC').page(params[:page])
end
def show
@post = Post.find_by_slug(params[:slug])
end
Also I need to implement to_paramfor my model:
def to_param
"#{created_at.year}/#{created_at.month}/#{slug}"
end
This is all that I learned from all the night searches in api / guide / SO.
But the problem is that for me more and more things arise that are worn for rails:
localhost/, , show, : year (sic!):
No route matches {:controller=>"posts", :action=>"show", :year=>
localhost/posts/2012/07/cut-test, :
No route matches {:controller=>"posts", :action=>"show", :year=>
, - , , , .
, , , URL , .