我有我的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 %>
其中哪一行是你面臨的問題? – uzaif
@uzaif,我在打印出版信息時遇到了問題。我在我的代碼中註明了問題出在哪裏。 –