【发布时间】:2011-04-20 02:33:22
【问题描述】:
我已经学习 Rails 大约 6 周了,所以还是个菜鸟!
我正在关注 Ryan Bates 关于多态关联的截屏视频,但在导航到 /model/xx/cmets 时出现“无路由匹配”错误。
在这转了两天之后,我完全被难住了——一切似乎都到位了。
评论模型:
create_table "comments", :force => true do |t|
t.text "content"
t.integer "user_id"
t.integer "commentable_id"
t.string "commentable_type"
t.datetime "created_at"
t.datetime "updated_at"
end
评论类:
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end
其他模型类:
class ModelName < ActiveRecord::Base
has_many :comments, :as => :commentable
end
Routes.rb
resources :modelname, :has_many => :comments
cmets_controller.rb
def index
@commentable = find_commentable
@comments = @commentable.comments
end
private
def find_commentable
params.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
end
end
nil
end
这一切都按照教程进行,但仍然返回“没有路由匹配”。
我已尝试将路由的替代格式设置为嵌套资源。
resources :modelname do |modelname|
modelname.resources :comments
end
在 routes.rb 中显式定义 cmets
resources :comments
以及routes.rb中各种术语的组合
resources :modelname, :has_many => :commentables
或
resources :modelname, :has_many => :comments
或
resources :modelname, :has_many => :comments, :through => :commentable
都没有成功。
有没有其他人遇到过这种情况?我不知道从哪里开始寻找。
非常感谢
【问题讨论】:
标签: polymorphic-associations ruby-on-rails-3