Szymon Radosz Szymon Radosz - 1 month ago 4x
Ruby Question

Devise: I can't display only current user workouts

I created little sport app and now I have problem with show only current user workouts on index page.

The problem : NoMethodError in WorkoutsController#index. undefined method `workouts' for nil:NilClass.

def index
@workouts = current_user.workouts.all

I leave all details below:


  1. In my User model I have

    has_many :workouts

  2. In my Workout model I wrote

    belongs_to :user

  3. In my views/workouts/index.html.erb

    <% @workouts.each do |workout| %>
    <h4><%= link_to, workout %></h4>
    <h3><%= workout.workout %></h3>
    <% end %>


try this:

def index
    @workouts = current_user.present? ? current_user.workouts : []

i think there is error when you are not loged, and current_user return nil