简体   繁体   English

Ruby语法错误,意外的tIVAR,应使用keyword_end

[英]Ruby Syntax error, unexpected tIVAR, expecting keyword_end

I have a model Entry, and it belongs to a Journal. 我有一个模型Entry,它属于Journal。 Journals have the ability to be public, so an entry belonging to a public journal would also be public, it would get that attribute from the journal. 期刊具有公开的能力,因此属于公开期刊的条目也将是公开的,它将从该期刊获得该属性。

I have an unless statement that just transfers it to the index unless @entry.is_public? || current_user.owns_entry? @entry 我有一个除非声明,除非将其转移到索引, unless @entry.is_public? || current_user.owns_entry? @entry unless @entry.is_public? || current_user.owns_entry? @entry

I have one method that is simply 我有一种方法很简单

def is_public?
  journal.public_access?
end

the other is pretty straightforward. 另一个非常简单。 The issue i am having is that when i try to use or instead of || 我遇到的问题是,当我尝试使用or而不是|| it works fine, but when i use || 它工作正常,但是当我使用|| it gives me a unexpected tIVAR, expecting keyword_end I am trying to figure out what the issue is, as rubocop tells me that i need to be using || 它给了我一个unexpected tIVAR, expecting keyword_end我正在尝试找出问题所在,因为rubocop告诉我我需要使用|| and i want to conform to ruby conventions. 我想遵守红宝石公约。 Anyone know what the issue is? 有人知道这个问题是什么吗?

In this case you need parentheses around your argument, ie current_user.owns_entry?(@entry) . 在这种情况下,您需要在参数周围current_user.owns_entry?(@entry)括号,即current_user.owns_entry?(@entry)

unless @entry.is_public? || current_user.owns_entry?(@entry)
  # ...
end

Generally speaking you should use parentheses except in the simplest cases, both for readability and to avoid syntax issues like this. 一般来说,除了在最简单的情况下,您都应该使用括号,以提高可读性并避免此类语法问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM