链接到rails中的特定页面

时间:2012-09-21 12:00:21

标签: ruby-on-rails ruby ruby-on-rails-3

我正在尝试使用此帖子在帖子标题中创建链接以打开新页面。 我尝试<%= link_to post.title, posts_path(path) %>,但收到错误

undefined local variable or method `posts_path' for #<#<Class:0x007fbf134f2b88>:0x007fbf13c17d78>

我做错了什么?

耙:路线:

 posts_index GET    /posts/index(.:format)         posts#index
               posts_new GET    /posts/new(.:format)           posts#new
            posts_create POST   /posts/create(.:format)        posts#create
                         GET    /posts/:id(.:format)           posts#show
                    root        /                              home#index
                    root        /                              home#index
        new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy
                   users GET    /users(.:format)               users#index
                    user GET    /users/:id(.:format)           users#show

3 个答案:

答案 0 :(得分:5)

如果您只是想参加演出动作,那就足够写

<%= link_to post.title, post %>

相同
<%= link_to post.title, post_path(post) %>

查看文档:{​​{3}}

答案 1 :(得分:0)

您正在尝试链接到单个帖子,但使用路径助手的复数形式,它不接受对象。使用post_path

<%= link_to post.title, post_path(path) %>

复数形式是列出该网址的所有资源。单数形式用于显示ONE资源。

有关详细信息,请参阅rails routing上的本指南

答案 2 :(得分:0)

我只需将此链接添加到我的节目页

<%= link_to 'Write a Review', new_comic_review_review_path(@comic_review)%>