lets say I have
# -----------------
# db
create_table "comments", :force => true do |t|
t.column "commentable_id", :integer
t.column "commentable_type", :string
t.column "body", :text
end
# model
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end
# -----------------
# this works just for articles
# model
class Article < ActiveRecord::Base
has_many :comments, :as => :commentable
end
# routes
map.resources :articles do |article|
article.resources :comments, :name_prefix => "article_"
end
# controller
class CommentsController < ApplicationController
make_resourceful do
actions :all
belongs_to :article
response_for :create, :update, :destroy do
redirect_to article_path(@article)
end
end
end
# view, e.g. new.html.erb
<% form_for(:comment, :url => article_comments_path(@article)) do |f| %>
# -----------------
# Now use it polymorphically, add a "doc" too
# model
class Doc < ActiveRecord::Base
has_many :comments, :as => :commentable
end
# how do i modify it to work with both docs and articles
I've tried
belongs_to :article, :doc
or
belongs_to :commentable
- Nathan
I've implemented a solution that's working for me.
While the authors are cogitating my changes, I've posted them on my
blog for anyone else that may need this support in the interim.
http://www.vaporbase.com/postings/make-
resourceful_and_nested_polymorphic_associations
regards,
linoj
I'm trying to come up with a RESTful controller using m_r and that
controller has to implement search and last_name_index (finds all by
first letter of last name). To further mix things up, search will be
called via Ajax, and if the accept format is HTML, it should render a
partial, otherwise if XML, an XML representation of the data objects.
the find-by... is quite similar, however, it will be called using a GET.
The problem: GET /localhost:3000/mycontroller/last_name_index?letter=H
resolves to:
:action => 'show'
:id => 'last_name_index'
:letter => 'H'
I understand that in REST a GET to a resource typically finds one
(show) if there is an argument (something after the resource name),
or finds all if there is no argument. What I'm wondering is how
people are customizing routes to implement search in their apps.
Thanks,
--steve
map.connect 'mycontroller/last_name_index', :controller =>
'mycontroller, :action => 'last_name_index'
That said, I'm not sure that's the best way to do it. What I'd do is
have something like GET /mycontroller/index?last_name_begins_with=H (or
maybe something a bit less verbose), and then have #index delegate the
searching to something else.
I do with there were a better solution. I've been toying with the idea
of integrating search into m_r, so you could do something like GET
/mycontroller/index?last_name=Smith and it would find everyone with the
last name Smith automatically. This could be extended to allow basic
pattern-matching: GET /mycontroller/index?last_name=~H*.
- Nathan
That said, I'm not sure that's the best way to do it. What I'd do is
have something like GET /mycontroller/index?last_name_begins_with=H (or
maybe something a bit less verbose), and then have #index delegate the
searching to something else.
I do with there were a better solution. I've been toying with the idea
of integrating search into m_r, so you could do something like GET
/mycontroller/index?last_name=Smith and it would find everyone with the
last name Smith automatically. This could be extended to allow basic
pattern-matching: GET /mycontroller/index?last_name=~H*.
Great. This feature is very eagerly awaited. :-)