Reputation: 2705
I am trying to use CanCanCan to define abilities in my Rails 4 app.
I have this ability in my ability.rb file:
can :read, Proposal,
{ :expiry_date > Time.now,
:sweep => { :disclosure => { :allusers => true } }
}
syntax error, unexpected ',', expecting =>
{ :expiry_date > Time.now,
When I try removing the comma after Time.now, I get this error:
syntax error, unexpected '\n', expecting =>
/Users/ml/f4/c2/app/models/ability.rb:107: syntax error, unexpected '}', expecting keyword_end
I don't know how to decipher what these error messages mean and what it wants me to do. For the first format, I don't understand why replacing a comma with => would be correct because there are two separate attributes that need to be evaluated for that error.
For the second error, I don't understand why I would but an 'end' in at that point, as i have a long list of other abilities that need to go into the class.
I wonder whether this error is something to do with trying to use the Time.now reference. Is that not a valid function in the ability file? If so, is there another way to see whether the date in the attribute is later than the date on which the ability is being tested?
When I try making this a block, as:
can :read, Proposal do | prop |
{:expiry_date > Time.now,
:sweep => { :disclosure => { :allusers => true } }
}
end
I get this error:
syntax error, unexpected ',', expecting =>
{:expiry_date > Time.now,
And then I'm stuck again because I don't know where to put the =>
When I try removing the outer curly braces, so that the block reads:
can :read, Proposal do | prop |
:expiry_date > Time.now,
:sweep => { :disclosure => { :allusers => true } }
end
I get this error:
syntax error, unexpected ',', expecting keyword_end
:expiry_date > Time.now,
So, just trying random things, I tried to vary the brackets as:
can :read, Proposal do | prop |
[:expiry_date > Time.now & :sweep => { :disclosure => { :allusers => true } } ]
end
(so square brackets instead of curly braces on the outer level) - and I don't get stopped at that line any more. I really don't understand why that works (and haven't yet tested it on seed data), but it has stopped errors from being thrown.
Thank you
Upvotes: 0
Views: 154
Reputation: 9523
Did you notice that you have
{:expiry_date > Time.now,
instead of
{:expiry_date => Time.now,
Notice the syntax error.
Upvotes: 1