Ruby on rails 3 来自其他控制器的部分状态

Ruby on rails 3 来自其他控制器的部分状态,ruby-on-rails-3,partial-views,conditional-statements,Ruby On Rails 3,Partial Views,Conditional Statements,我有一个模型组->有很多:问题和一个问题->有很多:投票 在我的视图显示中,对于控制器组,我有一个可以显示问题列表的分部: 'question',:collection=>@group.questions.byvote%> 然后我有一个链接来投票,风格如下: <%= link_to(question_votes_path(question), :controller => :vote, :action => :create, :method => :post, :id =

我有一个模型组->有很多:问题和一个问题->有很多:投票

在我的视图显示中,对于控制器组,我有一个可以显示问题列表的分部:
'question',:collection=>@group.questions.byvote%>
然后我有一个链接来投票,风格如下:

<%= link_to(question_votes_path(question), :controller => :vote, :action => :create, :method => :post, :id => 'vote') do %>
<%= image_tag("voteUp.png")%>
<%= question.votes_count%>
<%end%>

我怎样才能使它形成另一个控制器组或告诉问题控制器中的助手方法?

我认为您应该使用一个模型方法-然后您可以在视图中正确调用它,并且不引人注目地这样做。如果用户已对给定的
问题投票,则以下方法将返回
true

User.rb

def voted?(question)
  #returns true if vote exists
  Vote.exists?(:user_id => current_user.id, :question_id => question.id) 
end
<% if current_user.voted?(@question) %>
  #code to use if the user has voted on this question
<% else %>
  #code to use if the user has NOT voted on this question
<% end %>
您可以这样使用它:

\u question.html.erb

def voted?(question)
  #returns true if vote exists
  Vote.exists?(:user_id => current_user.id, :question_id => question.id) 
end
<% if current_user.voted?(@question) %>
  #code to use if the user has voted on this question
<% else %>
  #code to use if the user has NOT voted on this question
<% end %>

#如果用户对此问题进行了投票,则要使用的代码
#如果用户未对此问题投票,则使用的代码

什么是
可以投票?
最好告诉你的?用户可以投票的条件是什么?它告诉我是否存在当前用户对该问题的“投票”记录。谢谢!很好用。我只需要在model中添加
current\u user=UserSession.find.user
。很好,很高兴它能工作:)你也可以使用
:user\u id=>self.id