开发者

Link to resource

I have created model, view and controller:

$ rails generate scaffold Post name:string title:string content:text

I run server and I see list of posts if I open in the browser http:\localhost:3000\posts. Now I need to create link to this page. Something like:

<%= link_to("settings", { :controller => 'groups', :action => 'index'}) %>

But I get error on opening this page:

Couldn't find Group with ID=index

How ca I 开发者_如何学运维create link to http:\localhost:3000\posts and which action do I use in this case?


I think the path helpers are excellent in these cases. You could do it like this:

<%= link_to("Posts", posts_path) %>

posts_path in this case will link to http://localhost:3000/posts

When you use use resources :posts in your routes.rb you automatically get a few path helpers. For example:

posts_path            # /posts
post_path(@post)      # /posts/1
edit_post_path(@post) # /posts/1/edit
new_post_path         # /posts/new


If you have a route such as:

resources :groups

In config/routes.rb then you will have the helper groups_path. You can use rake routes to see all of your routes and helpers, but in this case you will have:

groups_path
group_path(@group)
edit_group_path(@group)

Polymorphic Routes Documentation

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜