link_to when using STI --> undefined method `animal_path'

46 views
Skip to first unread message

tom

unread,
Oct 6, 2015, 11:58:36 AM10/6/15
to Ruby on Rails: Talk
hi,

im getting started with STI but having trouble with link_to

1)
Model Animal
including an type column

2) routes:
#resource :animal
 resources :tiger , :controller => 'animals', type: 'tigler'
 resources :lion , :controller => 'animals', type: 'lion'

3) index.html
link_to 'show', animal >>undefined method `animal_path'


>> what is the correct way without hacking through classes and rewriting url helpers etc?

thx


Dave Aronson

unread,
Oct 6, 2015, 12:04:17 PM10/6/15
to rubyonrails-talk
On Tue, Oct 6, 2015 at 11:57 AM, tom <toma...@gmail.com> wrote:

> 2) routes:
> #resource :animal
> resources :tiger , :controller => 'animals', type: 'tigler'
> resources :lion , :controller => 'animals', type: 'lion'
>
> 3) index.html
> link_to 'show', animal >>undefined method `animal_path'

animal_path won't work because you've commented out resource :animal
(which should be plural anyway, as should the others).

--
Dave Aronson, consulting software developer of Codosaur.us,
PullRequestRoulette.com, Blog.Codosaur.us, and Dare2XL.com.

tom

unread,
Oct 6, 2015, 12:08:11 PM10/6/15
to Ruby on Rails: Talk
its just pseudo-code. 

but if i leave the base resource in there, then it give me an /animal/id route which i dont want. i want either /tiger/id or lion/id

thx




--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAHxKQiioyE-S4WZzjG8E-ktu0CG3YvJ-_TugLrTeu_%2B3AzGsYg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Dave Aronson

unread,
Oct 6, 2015, 12:14:54 PM10/6/15
to rubyonrails-talk
On Tue, Oct 6, 2015 at 12:07 PM, tom <toma...@gmail.com> wrote:

> its just pseudo-code.
>
> but if i leave the base resource in there, then it give me an /animal/id
> route which i dont want. i want either /tiger/id or lion/id

Then you need to know whether your animal is a tiger or lion (or bear
of course), and call the appropriate path helper. That kind of
messiness is exactly what inheritance (and other forms of
polymorphism) are great at solving.

tom

unread,
Oct 6, 2015, 12:19:22 PM10/6/15
to Ruby on Rails: Talk
correct. i am experimenting with polymorphic_path(resource) and eg polymorphic_url([blog, post]),  but so far im not getting the result i want. i am having separate files  and inherited models and also inherited controllers (those dont have eny defined actions yet)...but no go....

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages