0

Background

I have a model for stories in my rails app. One of the parts of the model is a boolean for "published".

On the creation and edit forms, I have a toggle button that shows "On" if the story is published and "Off" if it is not published. This toggle currently works correctly and I am able to toggle a story from being published or not published;; and this toggle updates the database accordingly.

The problem

On the show page I was trying to do an if statement but it wasn't resolving correctly, so I just did a print out of the published variable and it always prints out "false" even when checking the database it is set to "true".

The Code

<% @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 %>
2
  • in which line are you face issue? Commented Jul 25, 2016 at 4:29
  • @uzaif, I was having the issue in the line where I was printing out the published information. I have comments in my code showing where the problems were. Commented Jul 25, 2016 at 4:52

1 Answer 1

1

The Answer After 2 days of trying to figure out and fix this problem myself, and not coming up with anything and finally posting the question here, I found the problem a few minutes later in a code snippet sitting in my stories controller. See if you can find the problem in my code below.

Old Code

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

Explanation

I setup this code snippet to stop people from being able to enter in a story number in the browser and seeing it, even if it wasn't published. The problem code is in the second line where I checked to see if the story was published. I only put one equal sign, and thus every time a story was shown it would change the published variable to 'false' without changing the database. I added the extra equal sign and now it all works as it should. Here is the new code.

New Code

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

Thank you to those who were so quick on asking for follow up information to help me with my problem. I appreciate your assistance.

Sign up to request clarification or add additional context in comments.

3 Comments

A single = missed can create lot of troubles.
if @story.published && ... might serve you better. Or maybe even if @story.visible_to(current_user) with all the "is it published" and "is this person the author" logic hidden inside the visible_to method.
It's always unadvisable to use x == false! You can run into problems like: the method returns nil, which would also be interpreted as false. so for the future, use only if x.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.