这行特定的代码似乎适用于 Rails 2.xx,但不适用于 3.xx?有人可以帮忙“翻译”一下吗?它?

发布于 2024-11-18 13:07:41 字数 457 浏览 4 评论 0原文

<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 技术交流群。

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

发布评论

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

评论(1

流心雨 2024-11-25 13:07:41

问题是 @flits 为零,大概是因为您的 all_flits 方法返回 nil。

但是,我建议不要将该逻辑放入视图中,分解这样的标签。您有多种选择可以使其更简洁:


选项 1:使用 CSS 伪类 first-child ,如下所示:

 li:first-child {
    ...
 }

这具有不需要任何后端逻辑或特殊标记的优点。唯一的缺点是它对旧浏览器的支持不稳定,例如 IE6。


选项 2:使用 Rails 标签助手。

<%= content_tag :li, :class => @flits.first==flit?"first":"" %>

选项 3:将其隐藏在辅助方法中

<%= li_for_flit %>

然后在辅助方法中:

def li_for_flit
   #spit out your tag here
end

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:

 li:first-child {
    ...
 }

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.

<%= content_tag :li, :class => @flits.first==flit?"first":"" %>

Option 3: Tuck it away in a helper method

<%= li_for_flit %>

Then in the helper:

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