This is going to be a really dumb question, and I almost hate myself for asking it, but here goes.
When I run my Cucumber test, I'm getting a "syntax error, unexpected ')'" with the following code:
inside my user model:
def member?(gallery)
array = []
self.groups.each do |group|
array << group.id
end
开发者_高级运维 if array.include?(gallery.group.id)
true
end
end
And in my view:
<ul>
<% @galleries.each do |gallery| %>
<% if current_user.member?(gallery) %>
<li>
<%= link_to gallery.title, gallery %>
</li>
<% end %>
<% end %>
</ul>
EDIT: Here is the important part of the error in full:
~/Coding/Rails/galleryTest/app/views/galleries/index.html.erb:8: syntax error, unexpected ')', expecting keyword_then or ';' or '\n'
... current_user.member? gallery );@output_buffer.safe_concat('
... ^
~/Coding/Rails/galleryTest/app/views/galleries/index.html.erb:13: syntax error, unexpected keyword_end, expecting ')'
'); end
^
EDIT 2: Here is the error when removing the '=':
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id (ActionView::Template::Error)
./app/models/user.rb:18:in `member?'
I have tried a few different things, and I've got to be missing something really trivial. Another pair of eyes would be greatly appreciated.
Thank you very much.
<%= if current_user.member?(gallery) %>
should be:
<% if current_user.member?(gallery) %>
Not that there is no =
, it means output and your code is trying to output the response of the if block.
Alright, here is what I have, and how it seems to be working:
In my user model:
def member?(gallery)
array = self.groups.collect { |g| g.id }
if array.include?(gallery.group_id)
true
end
end
In my view:
<% if user_signed_in? %>
<ul id="private_galleries">
<% @galleries.each do |gallery| %>
<% if current_user.member?(gallery) %>
<li>
<%= link_to gallery.title, gallery %>
</li>
<% end %>
<% end %>
</ul>
<% end %>
<ul>
<% @galleries.each do |gallery| %>
<% if gallery.group_id == nil %>
<li>
<%= link_to gallery.title, gallery %>
</li>
<% end %>
<% end %>
</ul>
My tests are running alright now, but they aren't passing, which is strange, as when I set up users with groups and galleries with groups and view them with the site running, they seem to be showing appropriately, which just means my tests are probably effed. That'll be a task for the morning, and probably another question on StackOverflow!
精彩评论