开发者

Simple routing error

Total noob question.

I am building a simple photoblog in Rails which consists of Posts. Each post has its own id "/posts/1". I built Posts using rails scaffolding.

The problem is that I am unable to go to a url such as "/posts/index" or "/posts/anything" because it's trying to match anything after "/po开发者_运维问答sts/" it to an id... So I get back an error like:

Couldn't find Post with ID=index

I'm sure this could be fixed with routes, but I'm not really sure how and I feel that there is some big-picture problem I am missing here.

You can clone my app from here: https://github.com/tbhockey/PhotoBlog

Thank you.


Running rake routes is usually pretty helpful. In this case, the route to your index should just be /posts (not /posts/index). If you run rake routes you'll probably see something like

posts GET    /posts(.:format) {:action=>"index", :controller=>"posts"}

That's trying to tell you that the URL /posts will be directed to the index action of your posts_controller. Also, this may be referred to in your code as posts_path or posts_url.


Try to do this for your routes.rb :

  scope :path => '/posts', :controller => :posts do
    get 'show/(:id)' => :show, :as => 'show_post'
  end 

For a /posts/show/1, it will call the show action of the posts controller.

It's always a nice idea to learn about how routing works in Rails. It's very important and will help you understand many things about Rails. Especially looking at named routes is vital for good Rails programming.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜