开发者

link_to issue with inherited Active Record class

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 <开发者_如何学JAVA; 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?


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) %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜