dimanche 10 juillet 2016

Rails Conditional Statements on Object Index Page

In my rails app, I have an index of an object I'm calling Lessons. If a user marks the lesson as being completed, it gets recorded in a join table called Completions and the completed_step boolean gets set to true.

In the show action for the lesson, I'm able to insert a conditional statement to modify the button for "Complete Lesson."

That button logic works fine like this:

<% if current_user.completed_steps.include? @lesson %>                
   <%= button_to "This Lesson is Completed", @lesson.next, class: "btn btn-success btn-lg", :method => :get  %>               
<% else %>
   <%= button_to "Mark this Lesson as Complete", complete_lesson_path, class: "btn btn-warning btn-lg" %>
<% end %>

My question is how do I incorporate this type of logic check on the index view?

I'd like the links to each Lesson to appear in a Bootstrap Panel and I want to change the color of the panel depending upon whether or not the user completed_step for each Lesson.

I tried wrapping the panel in the same statement and it doesn't work, it stays red even though the user has completed these lessons.

Here's what I attempted:

<% @lessons.each do |lesson| %>

          <% if current_user.completed_steps.include? @lesson %>  
            <div class="panel panel-success">
          <% else %>
            <div class="panel panel-danger">
          <% end %>
              <div class="panel-heading">
                <h3 class="panel-title">
                  <%= link_to(lesson) do %>
                        <strong><%= lesson.title %></strong>
                  <% end %>
                </h3>
              </div>
              <div class="panel-body">
                <td><%= lesson.summary %></td>
              </div>
            </div>

What am I doing wrong or why isn't this working?

Aucun commentaire:

Enregistrer un commentaire