0

I have this list that should show all activities inside a category, and it sort of works but the last element that is displayed is strange. There are 4 activities in the DB for this particular category and at the end of the list I'm getting an item /categories/1/activities

%table.catlist
  - @category.activities.each do |activity|
    %tr{:class => cycle('one','two')}
      %td= link_to activity.name, [@category, activity]
      %td  

I also tried:

link_to activity.name, category_activity_path(:category_id => @category.id, :id => activity.id)

But that's not working at all, and giving me this error:

No route matches {:category_id=>1, :action=>"show", :controller=>"activities", :id=>nil}

The routes are nested like this:

  resources :categories do
    resources :activities do
      resources :records
    end
  end

It's actually showing everything right, but it closes the list with this strange element which is basically the activity path, but without an id.

EDIT

I think this is happening because of the cycle method that is supposed to switch between CSS classes. Any ideas how the empty row can be avoided?

Community
  • 1
  • 1

1 Answers1

1

You are most probably building a new activity in your controller which is included in the list of activities for @category. If you have the following code in your controller,

@activity = @category.activities.build

try changing it to

@activity = Activity.new category_id: @category.id

or add a check in the view to only show persisted activities.

- @category.activities.each do |activity|
  - next if activity.new_record?
  %tr{:class => cycle('one','two')}
    %td= link_to activity.name, [@category, activity]
    %td  
jvnill
  • 29,479
  • 4
  • 83
  • 86
  • I don't think that's it, basically the only thing I have in my controller is `@category = Category.find(params[:id])` and a format_to block –  Apr 09 '13 at 01:46
  • can you try the last code? i'm pretty sure that you have an activity that is a new record which is why you get the error. – jvnill Apr 09 '13 at 01:48
  • That did fix it. Strange though, I really can't see where the new record is being created –  Apr 09 '13 at 10:11