0

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

Mel
  • 2,481
  • 26
  • 113
  • 273
  • I wonder if CanCanCan is somehow confused between curly braces that denote a block vs those that denote a hash. You can pass the hash without curly braces, so why not try that (it's the form shown in CanCanCan docs). And put all your code into a single line and see what happens. I've only ever used block style conditions myself. The alternative is to have a separate can :read for each condition, which will OR the conditions together. – MarsAtomic Sep 05 '15 at 05:02
  • Hi, i tried removing the outer curly braces. I can't find an example in the wiki that has multiple conditions (so not sure if that was what you had in mind??). I want both expiry date and sweep to be true for the ability to work. – Mel Sep 05 '15 at 05:17

1 Answers1

1

Did you notice that you have

{:expiry_date > Time.now,

instead of

{:expiry_date => Time.now,

Notice the syntax error.

Tamer Shlash
  • 9,314
  • 5
  • 44
  • 82
  • Hi Tamer, I want the Time.now to be less than the expiry date. Are you sure the => means less than? – Mel Sep 05 '15 at 05:13