2013-08-25 33 views
0

我想澄清一下使用與rails 4豎起大拇指的gem。我目前有一個用戶資源和一個後期資源,並按照如下設置了大拇指。豎起大拇指寶石路由選擇錯誤

將gem添加到gemfile並使用bundler進行安裝。 生成需要遷移

用戶模型

class User < ActiveRecord::Base 
acts_as_voter 
end 

Post模型

class Post < ActiveRecord::Base 
acts_as_voteable 
end 

郵政控制器

def vote_up 
@post = Post.find(params[:id]) 
current_user.vote_for(@post) 
respond_to do |format| 
    format.js 
end 
end 

查看

<%= link_to('vote for this post!', vote_up_post_path(@post) , :method => :post) %> 

路由文件

resources :posts do 
    member do 
    post :vote_up 
    end 
end 

但是我不斷收到此錯誤

No route matches [POST] "/posts/vote_up" 

運行耙路線後,我可以看到下面的路線是提供給我:

vote_up_post POST /posts/:id/vote_up(.:format) posts#vote_up 

任何想法可能是這個錯誤的原因?

+0

你能添加你的視圖嗎? – Willian

回答

0

請您向我們展示您的觀點。

顯然,你在呼喚

/posts/vote_up 

代替

/posts/:id/vote_up 
0

「vote_up」 的行爲像一個RESTful的作用,類似於 「後/ 1 /刪除」,「後/ 1 /編輯」。所以你需要在路由中添加這個自定義的RESTful動作。

首先改變你的路線。

resources :posts do 
    member do 
    post 'vote_up' 
    end 
end 

然後,在你看來,使用這條道路,新增資源以arg

vote_up_post_path @post 

參考:http://guides.rubyonrails.org/routing.html#adding-more-restful-actions

+0

對不起,我沒有在我原來的帖子中包含我的路線設置。它已經成立。 – Joshua

+0

@Joshua,一切似乎都合法。路線變更後您是否嘗試重新啓動Rails服務器? –

0

其他人讀這筆記應該認識到,後是不是一個singluar他的控制器帖子...但帖子http動詞

resources :contenders do 
    member do 
    post 'vote_up' 
    end 
end 
0

不知道,但在你r視圖,而不是@post嘗試給@ post.id投票_up_post_path。

<%= link_to('vote for this post!', vote_up_post_path(@post.id) , :method => :post) %>