2

Here are the classes as I have them set up:

class Stat < ActiveRecord::Base
    belongs_to :stats_parent
end

class TotalStat < Stat
    belongs_to :stats_parent
end

#The StatsParent class is just to show how I use the relation.
class StatsParent < ActiveRecord::Base
    has_one  :total_stat
    has_many :stats
end

For the Stats Controller index action:

def index
    @stats = Stat.all
    respond_to do |format|
        format.html # index.html.erb
        format.xml  { render :xml => @stat }
    end
end

In the index view for stats there is this bit of code:

<% @stats.each do |stat| %>
    ...
    <td><%= link_to 'Show', stat %></td>
<% end %>

And I get this error:

undefined method `total_stat_path' for #<ActionView::Base:0x0000010324c1f8>

Why cant the link_to work here? Do I need to create a separate controller to handle the TotalStat?

lillq
  • 14,758
  • 20
  • 53
  • 58

1 Answers1

3

There's clearly an STI(single table inheritance) issue there, though I'd need to see more code to see what's really up. A quick fix would be to be more specific about the link_to path:

<%= link_to "Show", stat_path(stat) %>
lillq
  • 14,758
  • 20
  • 53
  • 58
bensie
  • 5,373
  • 1
  • 31
  • 34
  • The method stat_path() is generated somewhere. Could you point me to any doc that describes what method like this are generated for me? – lillq Nov 20 '09 at 22:49
  • When you create a resource in your routes with map.resources :stats you get a bunch of routes for free. Check out section 3 in the routing guide for a better understanding of the routing methods you get from RESTful resources: http://guides.rubyonrails.org/routing.html#restful-routing-the-rails-default – bensie Nov 20 '09 at 23:02