Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 测试助手中的所有权_Ruby On Rails - Fatal编程技术网

Ruby on rails 测试助手中的所有权

Ruby on rails 测试助手中的所有权,ruby-on-rails,Ruby On Rails,我的观点是这样的: <% video.members.each do |p| %> <% if p.id == current_user.id %> <%= "paid" %> <% end %> <% end %> 基本上,我是想根据id是否匹配来确定是否有会员为视频付费 也许这是一种非常糟糕的方法,在这种情况下,我很乐意尝试不同的方法 假设这是一种确定的检查方法,我如何编写类似的语句,但作为助手方法?我已经尝试

我的观点是这样的:

<% video.members.each do |p| %>
  <% if p.id == current_user.id %>
    <%= "paid" %>
  <% end %>
<% end %>

基本上,我是想根据id是否匹配来确定是否有会员为视频付费

也许这是一种非常糟糕的方法,在这种情况下,我很乐意尝试不同的方法


假设这是一种确定的检查方法,我如何编写类似的语句,但作为助手方法?我已经尝试过了,但似乎无法在助手中编写相同的逻辑,因为块只是吐出完整的数组而不是id,这意味着它不起作用。

您应该改为:

<% if video.members.exists?(id: current_user.id) %>
  <%= 'Paid' %>
<% end %>

这将生成一个查询,以测试视频是否由当前用户付费;-)


在助手中:

# application_helper.rb
def display_paid_or_not(video)
  return '' if video.blank? # similar to .nil?
  video.members.exists?(id: current_user.id) ? 'Paid' : ''
end

# in view
<%= display_paid_or_not(video) %> 
#应用程序_helper.rb
def显示是否付费(视频)
如果video.blank返回“”类似于。零?
video.members.exists?(id:当前用户.id)?'“已付款”:”
结束
#鉴于

希望这有帮助

太棒了,谢谢Yoshiji先生,正是我所需要的,非常好用