2016-03-15 44 views
0

我安裝了https://github.com/ryanto/acts_as_votable寶石。我嘗試了各種作者通過Rails控制檯在文檔中顯示的東西。但是我不明白如何在視圖中調用這些方法。如何正確調用視圖中的方法?

def vote 
    @post.liked_by current_user 
end 

這例如是我的看法。刪除並編輯工作,但投票不。什麼是正確的方法來做到這一點?

<% if current_user?(post.user) %> 
    <%= link_to "delete", post, method: :delete %>, 
    <%= link_to "edit", edit_post_path(post)%> 
    <% end %> 
    <%= link_to "vote", post, method: :vote if logged_in? %> 

回答

1

您是否添加了路由?喜歡的東西:

resources :answers do 
    member do 
    put "like", to: "answers#upvote" 
    put "dislike", to: "answers#downvote" 
    end 
end 

然後在您的視圖:

<%= link_to "", like_answer_path(answer), method: :put, class: "glyphicon glyphicon-arrow-up" %> 

<%= link_to "", dislike_answer_path(answer), method: :put, class: "glyphicon glyphicon-arrow-down" %> 
+0

我用這個結構,你的建議,但我得到這個錯誤: NoMethodError在PostsController#指數,可能是什麼問題? – Kunok

+0

沒有方法錯誤的方法是什麼? – toddmetheny

+0

我在這裏創建了線程:http://stackoverflow.com/questions/36020051/instance-variable-cant-be-assigned-via-private-method – Kunok

1

您需要參加表決的路線在routes.rb,那麼你建立你的鏈接以同樣的方式你刪除和編輯環節都建:方法名,對象名稱,路徑:

vote_post_path(post)

method參數不是控制器方法,它是html verb