Polymorphic association with rails view links

so i have a polymorphic association. I want to display all posts, but can't get the view links to work
class Artist < ActiveRecord::Base
  has_many :posts, :as => :postable
end

class Label < ActiveRecord::Base
 has_many :posts, :as => :postable
end

class Post < ActiveRecord::Base
   belongs_to :postable, :polymorphic => true
end

class ApplicationController < ActionController::Base    
    def find_postable
      params.each do |name, value|
        if name =~ /(.+)_id$/
          @postable = $1.classify.constantize.find(value)
        end
      end
      nil
    end
end    

class InfoController < ApplicationController
  before_filter :find_postable
  def index
    @allposts = Post.find(:all, :order => 'created_at DESC', :limit => 10 )
  end

<% for post in @allposts %>
<div class="all_posts_post">
<h2 class="content_title">
<%= link_to post.title, ???????? %>

<%= link_to 'Edit', ??????? %>
<%= link_to 'Destroy', ?????? :method => :delete,  :confirm => 'Are you sure?' %>

Open in new window

depassionAsked:
Who is Participating?
 
JESiiConnect With a Mentor Commented:
OK... you have only the routes [note the plural on 'posts']:
   artist_posts_path
   label_posts_path
so you'll have to use one of those, depending on the context.

Alternatively, check out this discussion:
   http://stackoverflow.com/questions/2071979/rails-polymorphic-associations-plus-routes
which discusses a similar situation and might provide a solution if you don't want to distinguish between artists and labels when referencing "postable".

HTH...jon
0
 
JESiiCommented:
If you remove the before_filter, do you get anything displayed?
0
 
depassionAuthor Commented:
actually it doesn't seem to make any difference, this is exactly what i have;
class InfoController < ApplicationController

  def index
    @allgigs = Gig.find(:all, :order => 'starts ASC', :limit => 20, :conditions => [ "starts between ? and ?", Time.now, 7.days.since ] )
    @allposts = Post.find(:all, :order => 'created_at DESC', :limit => 10 )
    @alltags = Tag.find(:all)
  end
end

#app/views/info/index
<div id="all_posts">
 <h2 class="all_posts_title">Latest News</h2>
 <% for post in @allposts %>
 <div class="all_posts_post">
 <h2 class="content_title"><%= link_to post.title, [@postable, post] %></h2>
<% if post.post_image.exists? %>
<div class="all_posts_post_img">
<%= image_tag post.post_image.url(:thumb) %>
<!-- END .all_posts_post_img --></div>	
<% end -%>
<p class="date">Posted <%= distance_of_time_in_words_to_now(post.created_at) %> ago</p>
<div class="all_posts_body">
<%= auto_link truncate(post.body, :length => 270) %>
<!-- END .all_posts_body --></div>
<div class="tag_list">
<% for tag in post.tags %>
<span class="tags"><%= link_to tag.name, tag_path(tag) %></span>
<% end -%>
<!-- END .tag_list --></div>
<% if admin? %>
<%= link_to 'Edit', polymorphic_url([post.postable, post], :action => :edit) %>
<%= link_to 'Destroy', [post.postable, post], :method => :delete,  :confirm => 'Are you sure?' %>
<% end -%>
<div class="content_clear_with_hr"></div>	
<!-- END .all_posts_post --></div>
<% end -%>	
<!-- END #all_posts --></div>

#error
undefined method `post_path' for #<ActionView::Base:0x1031147d0>

Open in new window

0
 
JESiiCommented:
Ah! lines 40: Undefined method 'post_path"...  believe that means you are having routes problems.

You may need to say "posts_path" [note the plural]

If that doesn't work, type the following in your shell:
  rake routes
to see what is actually defined in your system.

...jon

0
 
depassionAuthor Commented:

artist_gigs GET    /artists/:artist_id/gigs(.:format)              {:controller=>"gigs", :action=>"index"}
                    POST   /artists/:artist_id/gigs(.:format)              {:controller=>"gigs", :action=>"create"}
     new_artist_gig GET    /artists/:artist_id/gigs/new(.:format)          {:controller=>"gigs", :action=>"new"}
    edit_artist_gig GET    /artists/:artist_id/gigs/:id/edit(.:format)     {:controller=>"gigs", :action=>"edit"}
         artist_gig GET    /artists/:artist_id/gigs/:id(.:format)          {:controller=>"gigs", :action=>"show"}
                    PUT    /artists/:artist_id/gigs/:id(.:format)          {:controller=>"gigs", :action=>"update"}
                    DELETE /artists/:artist_id/gigs/:id(.:format)          {:controller=>"gigs", :action=>"destroy"}
       artist_posts GET    /artists/:artist_id/posts(.:format)             {:controller=>"posts", :action=>"index"}
                    POST   /artists/:artist_id/posts(.:format)             {:controller=>"posts", :action=>"create"}
    new_artist_post GET    /artists/:artist_id/posts/new(.:format)         {:controller=>"posts", :action=>"new"}
   edit_artist_post GET    /artists/:artist_id/posts/:id/edit(.:format)    {:controller=>"posts", :action=>"edit"}
        artist_post GET    /artists/:artist_id/posts/:id(.:format)         {:controller=>"posts", :action=>"show"}
                    PUT    /artists/:artist_id/posts/:id(.:format)         {:controller=>"posts", :action=>"update"}
                    DELETE /artists/:artist_id/posts/:id(.:format)         {:controller=>"posts", :action=>"destroy"}
       artist_links GET    /artists/:artist_id/links(.:format)             {:controller=>"links", :action=>"index"}
                    POST   /artists/:artist_id/links(.:format)             {:controller=>"links", :action=>"create"}
    new_artist_link GET    /artists/:artist_id/links/new(.:format)         {:controller=>"links", :action=>"new"}
   edit_artist_link GET    /artists/:artist_id/links/:id/edit(.:format)    {:controller=>"links", :action=>"edit"}
        artist_link GET    /artists/:artist_id/links/:id(.:format)         {:controller=>"links", :action=>"show"}
                    PUT    /artists/:artist_id/links/:id(.:format)         {:controller=>"links", :action=>"update"}
                    DELETE /artists/:artist_id/links/:id(.:format)         {:controller=>"links", :action=>"destroy"}
      artist_videos GET    /artists/:artist_id/videos(.:format)            {:controller=>"videos", :action=>"index"}
                    POST   /artists/:artist_id/videos(.:format)            {:controller=>"videos", :action=>"create"}
   new_artist_video GET    /artists/:artist_id/videos/new(.:format)        {:controller=>"videos", :action=>"new"}
  edit_artist_video GET    /artists/:artist_id/videos/:id/edit(.:format)   {:controller=>"videos", :action=>"edit"}
       artist_video GET    /artists/:artist_id/videos/:id(.:format)        {:controller=>"videos", :action=>"show"}
                    PUT    /artists/:artist_id/videos/:id(.:format)        {:controller=>"videos", :action=>"update"}
                    DELETE /artists/:artist_id/videos/:id(.:format)        {:controller=>"videos", :action=>"destroy"}
      artist_charts GET    /artists/:artist_id/charts(.:format)            {:controller=>"charts", :action=>"index"}
                    POST   /artists/:artist_id/charts(.:format)            {:controller=>"charts", :action=>"create"}
   new_artist_chart GET    /artists/:artist_id/charts/new(.:format)        {:controller=>"charts", :action=>"new"}
  edit_artist_chart GET    /artists/:artist_id/charts/:id/edit(.:format)   {:controller=>"charts", :action=>"edit"}
       artist_chart GET    /artists/:artist_id/charts/:id(.:format)        {:controller=>"charts", :action=>"show"}
                    PUT    /artists/:artist_id/charts/:id(.:format)        {:controller=>"charts", :action=>"update"}
                    DELETE /artists/:artist_id/charts/:id(.:format)        {:controller=>"charts", :action=>"destroy"}
      artist_images GET    /artists/:artist_id/images(.:format)            {:controller=>"images", :action=>"index"}
                    POST   /artists/:artist_id/images(.:format)            {:controller=>"images", :action=>"create"}
   new_artist_image GET    /artists/:artist_id/images/new(.:format)        {:controller=>"images", :action=>"new"}
  edit_artist_image GET    /artists/:artist_id/images/:id/edit(.:format)   {:controller=>"images", :action=>"edit"}
       artist_image GET    /artists/:artist_id/images/:id(.:format)        {:controller=>"images", :action=>"show"}
                    PUT    /artists/:artist_id/images/:id(.:format)        {:controller=>"images", :action=>"update"}
                    DELETE /artists/:artist_id/images/:id(.:format)        {:controller=>"images", :action=>"destroy"}
    artist_podcasts GET    /artists/:artist_id/podcasts(.:format)          {:controller=>"podcasts", :action=>"index"}
                    POST   /artists/:artist_id/podcasts(.:format)          {:controller=>"podcasts", :action=>"create"}
 new_artist_podcast GET    /artists/:artist_id/podcasts/new(.:format)      {:controller=>"podcasts", :action=>"new"}
edit_artist_podcast GET    /artists/:artist_id/podcasts/:id/edit(.:format) {:controller=>"podcasts", :action=>"edit"}
     artist_podcast GET    /artists/:artist_id/podcasts/:id(.:format)      {:controller=>"podcasts", :action=>"show"}
                    PUT    /artists/:artist_id/podcasts/:id(.:format)      {:controller=>"podcasts", :action=>"update"}
                    DELETE /artists/:artist_id/podcasts/:id(.:format)      {:controller=>"podcasts", :action=>"destroy"}
      artist_tracks GET    /artists/:artist_id/tracks(.:format)            {:controller=>"tracks", :action=>"index"}
                    POST   /artists/:artist_id/tracks(.:format)            {:controller=>"tracks", :action=>"create"}
   new_artist_track GET    /artists/:artist_id/tracks/new(.:format)        {:controller=>"tracks", :action=>"new"}
  edit_artist_track GET    /artists/:artist_id/tracks/:id/edit(.:format)   {:controller=>"tracks", :action=>"edit"}
       artist_track GET    /artists/:artist_id/tracks/:id(.:format)        {:controller=>"tracks", :action=>"show"}
                    PUT    /artists/:artist_id/tracks/:id(.:format)        {:controller=>"tracks", :action=>"update"}
                    DELETE /artists/:artist_id/tracks/:id(.:format)        {:controller=>"tracks", :action=>"destroy"}
            artists GET    /artists(.:format)                              {:controller=>"artists", :action=>"index"}
                    POST   /artists(.:format)                              {:controller=>"artists", :action=>"create"}
         new_artist GET    /artists/new(.:format)                          {:controller=>"artists", :action=>"new"}
        edit_artist GET    /artists/:id/edit(.:format)                     {:controller=>"artists", :action=>"edit"}
             artist GET    /artists/:id(.:format)                          {:controller=>"artists", :action=>"show"}
                    PUT    /artists/:id(.:format)                          {:controller=>"artists", :action=>"update"}
                    DELETE /artists/:id(.:format)                          {:controller=>"artists", :action=>"destroy"}
        label_posts GET    /labels/:label_id/posts(.:format)               {:controller=>"posts", :action=>"index"}
                    POST   /labels/:label_id/posts(.:format)               {:controller=>"posts", :action=>"create"}
     new_label_post GET    /labels/:label_id/posts/new(.:format)           {:controller=>"posts", :action=>"new"}
    edit_label_post GET    /labels/:label_id/posts/:id/edit(.:format)      {:controller=>"posts", :action=>"edit"}
         label_post GET    /labels/:label_id/posts/:id(.:format)           {:controller=>"posts", :action=>"show"}
                    PUT    /labels/:label_id/posts/:id(.:format)           {:controller=>"posts", :action=>"update"}
                    DELETE /labels/:label_id/posts/:id(.:format)           {:controller=>"posts", :action=>"destroy"}
             labels GET    /labels(.:format)                               {:controller=>"labels", :action=>"index"}
                    POST   /labels(.:format)                               {:controller=>"labels", :action=>"create"}
          new_label GET    /labels/new(.:format)                           {:controller=>"labels", :action=>"new"}
         edit_label GET    /labels/:id/edit(.:format)                      {:controller=>"labels", :action=>"edit"}
              label GET    /labels/:id(.:format)                           {:controller=>"labels", :action=>"show"}
                    PUT    /labels/:id(.:format)                           {:controller=>"labels", :action=>"update"}
                    DELETE /labels/:id(.:format)                           {:controller=>"labels", :action=>"destroy"}
           sessions GET    /sessions(.:format)                             {:controller=>"sessions", :action=>"index"}
                    POST   /sessions(.:format)                             {:controller=>"sessions", :action=>"create"}
        new_session GET    /sessions/new(.:format)                         {:controller=>"sessions", :action=>"new"}
       edit_session GET    /sessions/:id/edit(.:format)                    {:controller=>"sessions", :action=>"edit"}
            session GET    /sessions/:id(.:format)                         {:controller=>"sessions", :action=>"show"}
                    PUT    /sessions/:id(.:format)                         {:controller=>"sessions", :action=>"update"}
                    DELETE /sessions/:id(.:format)                         {:controller=>"sessions", :action=>"destroy"}
               tags GET    /tags(.:format)                                 {:controller=>"tags", :action=>"index"}
                    POST   /tags(.:format)                                 {:controller=>"tags", :action=>"create"}
            new_tag GET    /tags/new(.:format)                             {:controller=>"tags", :action=>"new"}
           edit_tag GET    /tags/:id/edit(.:format)                        {:controller=>"tags", :action=>"edit"}
                tag GET    /tags/:id(.:format)                             {:controller=>"tags", :action=>"show"}
                    PUT    /tags/:id(.:format)                             {:controller=>"tags", :action=>"update"}
                    DELETE /tags/:id(.:format)                             {:controller=>"tags", :action=>"destroy"}
                    GET    /contact                                        {:controller=>"contact", :action=>"index"}
                    POST   /contact                                        {:controller=>"contact", :action=>"create"}
                    GET    /booking                                        {:controller=>"contact", :action=>"booking"}
            contact POST   /booking                                        {:controller=>"contact", :action=>"create_booking"}
               root        /                                               {:controller=>"info", :action=>"index"}
              login        /login                                          {:controller=>"sessions", :action=>"new"}
             logout        /logout                                         {:controller=>"sessions", :action=>"destroy"}
                           /:controller/:action/:id      

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.