Rails 语法错误,意外')'

发布于 2024-12-04 17:50:45 字数 1292 浏览 0 评论 0原文

这将是一个非常愚蠢的问题,我几乎讨厌自己问这个问题,但这里是。

当我运行黄瓜测试时,我收到“语法错误,意外的')'”,代码如下:

在我的用户模型中:

def member?(gallery)
    array = []
    self.groups.each do |group|
        array << group.id
    end
    if array.include?(gallery.group.id)
        true
    end
end

在我看来:

<ul>
<% @galleries.each do |gallery| %>
    <% if current_user.member?(gallery) %>
    <li>
        <%= link_to gallery.title, gallery %>
    </li>
    <% end %>
<% end %>
</ul>

编辑:这是完整错误的重要部分:

 ~/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 
            ^

编辑2:这是删除“=”时的错误:

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?'

我尝试了一些不同的方法,但我必须遗漏一些非常微不足道的东西。另一双眼睛将不胜感激。

非常感谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南街九尾狐 2024-12-11 17:50:45
<%= if current_user.member?(gallery) %>

应该是:

<% if current_user.member?(gallery) %>

并不是说​​没有 =,它意味着输出,并且您的代码正在尝试输出 if 块的响应。

<%= 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.

久随 2024-12-11 17:50:45

好吧,这就是我所拥有的,以及它似乎如何工作:

在我的用户模型中:

def member?(gallery)
    array = self.groups.collect { |g| g.id }
    if array.include?(gallery.group_id)
        true
    end
end

在我看来:

<% 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>

我的测试现在运行正常,但它们没有通过,这很奇怪,就像我使用组和画廊与组并在网站运行时查看它们,它们似乎显示得很好,这仅仅意味着我的测试可能是有效的。这将是早上的任务,并且可能是 StackOverflow 上的另一个问题!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文