2016-07-25 264 views
0

背景Rails的输出不正确

我有我的Rails应用程序的故事的典范。模型的其中一个部分是“已发布”的布尔值。

在创建和编辑表单上,我有一个切换按钮,如果故事发布则显示“开”,如果未发布则显示“关”。此切换功能目前正常工作,并且可以切换发布或未发布的故事;;并且该切换器相应地更新数据库。

问题

在节目页面我试图if语句做的,但它并没有解决正确,所以我只是做了一个打印出已发布的变量,它总是打印出“假“即使在检查数据库时它被设置为”true“。

守则

<% @title="Stories" %> 

<p><strong><%= @story.heading %></strong></p> 

<p><%= @story.body %></p> 

<p> 
    <%= #!!!!!!!!!!!!!!!!Not working currently!!!!!!!!!!!!! 
     # if @story.published 
     # @publish_Notice = "This story has been made public." 
     # else 
     # @publish_Notice = "This story is private." 
     # end 
     # @publish_Notice 

     @story.published # Always prints out 'false' even when database shows 'true' 
    %> 
</p> 

<p>~ <%= @story.authorName %></p> 

<p>Submitted: <%= @story.created_at.strftime("%B, %d %Y") %><br/> 

<%= 
    @location = " " 

    if @story.locationCity == "" || @story.locationCity == " " || @story.locationCity.nil? 
    @location = " " 
    else 
    @location = "Near: " @story.locationCity + ", " + @story.locationState 
    end 
%> 
<%= @location %></p> 

<% if (user_signed_in?) && (current_user == @story.user) %> 
    <%= link_to 'Edit', edit_story_path(@story) %> | 
    <%= link_to 'Delete', @story, method: :delete, data: { confirm: 'Are you sure?' } %> | 
<% end %> 
<%= link_to 'Back', stories_path %> 
+0

其中哪一行是你面临的问题? – uzaif

+0

@uzaif,我在打印出版信息时遇到了问题。我在我的代码中注明了问题出在哪里。 –

回答

1

答案 后的试图找出并解决这个问题我自己,而不是想出什么,终于在这里张贴问题2天后,我发现问题几分钟后,在我的故事控制器中的代码片段。看看你是否可以在我的代码下面找到问题。

旧代码

def show 
    if @story.published = false && @story.user != current_user 
    redirect_to stories_url, notice: 'That action is not permitted.' 
    end 
end 

说明

我建立这个代码片断,以阻止人们能够在浏览器中的一个故事号码进入,看到它,即使它WASN没有发表。问题代码位于第二行,我查看了故事是否已发布。我只放了一个等号,因此每次显示一个故事时,它都会在不更改数据库的情况下将发布的变量更改为“假”。我添加了额外的等号,现在它都可以正常工作。这是新的代码。

新代码

def show 
    if @story.published == false && @story.user != current_user 
    redirect_to stories_url, notice: 'That action is not permitted.' 
    end 
end 

谢谢那些谁的追问跟进信息,以帮助我与我的问题这么快。感谢您的协助。

+1

错过一个'='可能会造成很多麻烦。 – Pramod

+0

伟大的工作人! – uzaif

+1

'if @ story.published && ...'可能会更好地为您服务。或者甚至可以在'visible_to'方法中隐藏所有“被发布”和“是这个作者”逻辑的“@ story.visible_to(current_user)'。 –