2016-02-04 89 views
1

我正面临一个奇怪的错误,不幸的是我不知道如何调查它。Rails:当属性设置为true时显示帖子

integer =>pinoftheday设置为true时,我在主页上渲染某些针脚。我手动设置一些引脚为真。

对于某些引脚,它的工作正常,它们出现在主页上,其他一些则没有。顺便说一句,我正在检查我的控制台,他们被正确设置为true。

下面是一些代码:

<% @pins.each do |pin| %> 
    <% if pin.pinoftheday %> 
      (...) some informations about the pin 
    <% end %> 
    <% end %> 

任何想法如何,我可以检查为什么有些引脚没有渲染?我现在不写任何测试...我知道这很愚蠢,但我没有学会测试rails。

谢谢。

编辑:是的,在我的代码中它是一个pin模型。我想用post来使它更​​清晰。想象它不是:) - 编辑它到正确的模型:引脚。

+0

什么是帖子在这里?或者它应该被钉住? <%post.pinoftheday%> (...)一些关于PIN码的信息 <% end %> – Dheeresha

+0

在您的代码中,@ postss应该是@ posts',btw在'post'处有拼写错误,应该是'pin',对吗? –

回答

0

你的问题是,你定义在你的块local variable,并引用另:

<% @postss.each do |post| %> 
    <% if post.pinoftheday %> 
     ... 
    <% end %> 
<% end %> 

-

你会更好使用scope

#app/models/post.rb 
class Post < ActiveRecord::Base 
    scope :pin_of_the_day, -> { where pinoftheday: true } 
end 

你也会做好你的pinofthedayboolean。如果您参考了1 = true; 0 = false,则Rails会在您的db中使用tinyint来处理它,并将其作为布尔逻辑调用true/false。代替引用该整数为数字的,则可以调用true

上面将允许你拨打:

#app/controllers/your_controller.rb 
class YourController < ApplicationController 
    def index 
    @postss = Post.pin_of_the_day 
    end 
end 

这将删除低效条件逻辑(<% if ...):

<% @postss.each do |post| %> 
    ... 
<% end %> 
+0

是的,非常感谢您的宝贵意见。你是对的,使用示波器并从控制器拨打pinoftheday是一个更清洁的方法。关于使用布尔值,我不确定,我一直都在读它可以减慢应用程序。 – zacchj

0

如果我理解你的代码,然后在下面会:

<% @postss.each do |pin| %> 
    <% if pin.pinoftheday.nil? %> 
     (...) some informations about the pin 
    <% else %> 
     (...) some informations about the pin 
    <% end %> 
<% end %> 

希望能帮助你

1

尝试下面的代码。

<% @postss.each do |post| %> 
    <% if post.pinoftheday %> 
      (...) some informations about the pin 
    <% end %> 
    <% end %> 
相关问题