这行特定的代码似乎适用于 Rails 2.xx,但不适用于 3.xx?有人可以帮忙“翻译”一下吗?它?
<li<% if @flits.first == flit %> class="first" <% end %>>
我在 Rails 3 的 application.css
中为 #flits_list
和 #flits_list :hover
创建了 css,但我想要第一个 flit< /code> 列表 (
flits_list.first
) 中具有不同的 css,因此我创建了一个类,但此代码返回错误
home#index 中没有方法错误。当你没有预料到的时候,你得到了一个 nil 对象!您可能期望一个数组的实例。评估 nil.first 时发生错误
任何帮助将不胜感激。
<li<% if @flits.first == flit %> class="first" <% end %>>
I created css for #flits_list
and #flits_list :hover
in application.css
in Rails 3 but I would like the first flit
in the list (flits_list.first
) to have different css so I created a class, but this code returns the error
no method error in home#index. you have a nil object when you didn't expect it! You might have expected an instance of array. the error occurred while evaluating nil.first
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是 @flits 为零,大概是因为您的 all_flits 方法返回 nil。
但是,我建议不要将该逻辑放入视图中,分解这样的标签。您有多种选择可以使其更简洁:
选项 1:使用 CSS 伪类
first-child
,如下所示:这具有不需要任何后端逻辑或特殊标记的优点。唯一的缺点是它对旧浏览器的支持不稳定,例如 IE6。
选项 2:使用 Rails 标签助手。
选项 3:将其隐藏在辅助方法中
然后在辅助方法中:
The problem is that @flits is nil, presumably because your all_flits method is returning nil.
However, I'd recommend not putting that logic in the view, breaking up a tag like that. You have several options to make it cleaner:
Option 1: Use the CSS pseudo-class
first-child
like so:This has the advantage of not requiring any back-end logic or special markup. The only downside is that it has spotty older browser support, e.g. IE6.
Option 2: Use the Rails tag helpers.
Option 3: Tuck it away in a helper method
Then in the helper: