如何在视图中正确调用方法?

时间:2016-03-15 12:12:19

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

我安装了https://github.com/ryanto/acts_as_votable gem。我试过通过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? %>

2 个答案:

答案 0 :(得分:1)

您需要在routes.rb中进行投票,然后按照构建删除和编辑链接的方式构建链接:方法名称,对象名称,路径:

vote_post_path(post)

method参数不是控制器方法,而是html verb

答案 1 :(得分: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" %>