【发布时间】:2016-11-28 07:42:14
【问题描述】:
背景
我的 rails 应用中有一个故事模型。模型的一部分是“已发布”的布尔值。
在创建和编辑表单上,我有一个切换按钮,如果故事已发布,则显示“打开”,如果故事未发布,则显示“关闭”。此切换当前工作正常,我可以切换故事的发布或不发布;;这个切换会相应地更新数据库。
问题
在显示页面上,我试图做一个 if 语句,但它没有正确解析,所以我只是打印了已发布的变量,即使检查设置的数据库,它也总是打印出“false”为“真”。
守则
<% @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 %>
【问题讨论】:
-
您在哪一行遇到问题?
-
@uzaif,我在打印已发布信息的行中遇到了问题。我的代码中有 cmets 显示问题所在。
标签: ruby-on-rails ruby database sqlite boolean