jacobrubyonrails jacobrubyonrails - 3 months ago 6
Ruby Question

rails if current id do

I'm developing an online course site using Ruby on Rails and bootstrap. I have a model for Courses and a model for Lessons. In the lesson controller show view I am not only showing the lesson page with video, discussion and notes but also a list of all the lessons belonging to the course. This is done by:

<%= @lessons.each do |lesson| %>
<%= link_to [lesson.course, lesson] do %>
<%= lesson.title %>
<% end %>
<% end %>


The user can, from the individual lesson page, pick the next lesson to watch.

Question:

The URL generated is: localhost:3000/courses/1/lessons/2 (I'll change this with Friendly Id gem later). I would like to show in the list of all lessons on the individual lesson page which lesson the user is watching right now. So basically maybe have something say "You are currently watching : Lesson with id 2" and have it in a different background color with some custom html. How can I have different HTML and CSS for the currently watching lesson in the all lessons list?

Thanks a lot in advance!

/Jacob

Answer

A simple way would be to add a conditional, in the case you have the current lesson available in a local variable or method current_lesson. Then it can look like this:

<%= @lessons.each do |lesson| %>
  <% if lesson.id == current_lesson.id %>
    ... other html ...
  <% else %>
    <%= link_to [lesson.course, lesson] do %>
      <%= lesson.title %>
    <% end %>
   <% end %>
 <% end %>
Comments