调用浅嵌套路径:post_up_vote_path(post),失败

时间:2014-07-18 01:46:16

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

之前,为了简化路径调用,我更改了routes.rb文件,以下列方式实现浅嵌套:

SampleApp::Application.routes.draw do

  get "comments/create"
  devise_for :users
  resources :users, only: [:update]

  resources :topics do
    resources :posts, except: [:index]
  end

  resources :posts, only: [] do
    resources :comments, only: [:create, :destroy]
  end

  post '/up-vote' => 'votes#up_vote', as: :up_vote
  post '/down-vote' => 'votes#down_vote', as: :down_vote
  get 'about' => 'welcome#about'

  root to: 'welcome#index'
end

然而,当我在局部内部调用post_up_vote_path(post)时,它无法识别路径方法:

<div class='vote-arrows'>
  <div class='pull-left'>
    <div><%= link_to " ", post_up_vote_path(post), class: 'glyphicon glyphicon-chevron-up', method: :post %></div>
    <div><strong><%= post.points %></strong></div>
    <div><%= link_to " ", post_down_vote_path(post), class: 'glyphicon glyphicon-chevron-down', method: :post %></div>
  </div>
</div>

我在节目视图中调用上面的部分(_vote.html.erb):

<div class="row">
  <div class="col-md-8">
    <%= render partial: 'votes/voter', locals: {post: @post} %>
    ...

此代码中是否有显示错误的内容?

1 个答案:

答案 0 :(得分:0)

您的路线不正确 - 您将其命名为down_voteup_vote,而不是post_down_votepost_up_vote

如果您检查rake routes的输出,它会告诉您应该使用的名称。