我阅读了这篇http://railscasts.com/episodes/154-polymorphic-association-revised帖子并按原样实施。但我也想在本教程中添加编辑和删除功能。我有comments_controller.rb 是这样的
class CommentsController < ApplicationController
before_filter :load_commentable
def index
@comments = @commentable.comments
end
def new
@comment = @commentable.comments.new
end
def create
@comment = @commentable.comments.new(params[:comment])
if @comment.save
redirect_to @commentable, notice: "Comment created."
else
render :new
end
end
private
def load_commentable
resource, id = request.path.split('/')[1, 2]
@commentable = resource.singularize.classify.constantize.find(id)
end
# def load_commentable
# klass = [Article, Photo, Event].detect { |c| params["#{c.name.underscore}_id"] }
# @commentable = klass.find(params["#{klass.name.underscore}_id"])
# end
end
我给定的 _comments.html.erb 是这样的
<div id="comments">
<% @comments.each do |comment| %>
<div class="comment">
<%= simple_format comment.content %>
</div>
<% end %>
</div>
我的路线是这样的
Blog::Application.routes.draw do
resources :articles do
resources :comments
end
resources :photos do
resources :comments
end
resources :events do
resources :comments
end
resources :comments
root to: 'articles#index'
end
我的 rake 路线是这样的
article_comment GET /articles/:article_id/comments/:id(.:format) comments#show
PUT /articles/:article_id/comments/:id(.:format) comments#update
DELETE /articles/:article_id/comments/:id(.:format) comments#destroy
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
photo_comments GET /photos/:photo_id/comments(.:format) comments#index
POST /photos/:photo_id/comments(.:format) comments#create
new_photo_comment GET /photos/:photo_id/comments/new(.:format) comments#new
edit_photo_comment GET /photos/:photo_id/comments/:id/edit(.:format) comments#edit
photo_comment GET /photos/:photo_id/comments/:id(.:format) comments#show
PUT /photos/:photo_id/comments/:id(.:format) comments#update
DELETE /photos/:photo_id/comments/:id(.:format) comments#destroy
photos GET /photos(.:format) photos#index
POST /photos(.:format) photos#create
new_photo GET /photos/new(.:format) photos#new
edit_photo GET /photos/:id/edit(.:format) photos#edit
photo GET /photos/:id(.:format) photos#show
PUT /photos/:id(.:format) photos#update
DELETE /photos/:id(.:format) photos#destroy
event_comments GET /events/:event_id/comments(.:format) comments#index
POST /events/:event_id/comments(.:format) comments#create
new_event_comment GET /events/:event_id/comments/new(.:format) comments#new
edit_event_comment GET /events/:event_id/comments/:id/edit(.:format) comments#edit
event_comment GET /events/:event_id/comments/:id(.:format) comments#show
PUT /events/:event_id/comments/:id(.:format) comments#update
DELETE /events/:event_id/comments/:id(.:format) comments#destroy
events GET /events(.:format) events#index
POST /events(.:format) events#create
new_event GET /events/new(.:format) events#new
edit_event GET /events/:id/edit(.:format) events#edit
event GET /events/:id(.:format) events#show
PUT /events/:id(.:format) events#update
DELETE /events/:id(.:format) events#destroy
comments GET /comments(.:format) comments#index
POST /comments(.:format) comments#create
new_comment GET /comments/new(.:format) comments#new
edit_comment GET /comments/:id/edit(.:format) comments#edit
comment GET /comments/:id(.:format) comments#show
PUT /comments/:id(.:format) comments#update
DELETE /comments/:id(.:format) comments#destroy
root / articles#index