0

我正在尝试在帖子标题中创建链接,该链接使用此帖子打开新页面。我试过<%= 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
4

3 回答 3

5

如果你只是想去show action,写就够了

<%= link_to post.title, post %>

这与

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

看看文档:http ://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to

于 2012-09-21T12:07:16.020 回答
0

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

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

复数形式是列出该 url 处的所有资源。单数形式用于显示一个资源。

有关更多详细信息,请参阅此导轨路由指南

于 2012-09-21T12:55:43.433 回答
0

我只需将此链接添加到我的显示页面

<%= link_to 'Write a Review', new_comic_review_review_path(@comic_review)%> 
于 2012-12-21T21:01:43.543 回答