これが私のルートファイルです:
resources :locations do
resources :comments
end
およびコントローラー:
class CommentsController < InheritedResources::Base
def index
@commentable = find_commentable
@comments = @commentable.comments.where(:company_id => session[:company_id])
end
def show
@comment = Comment.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @comment }
end
end
def new
@comment = Comment.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @comment }
end
end
def create
@commentable = find_commentable
@comment = @commentable.comments.build(params[:comment])
@comment.user_id = session[:user_id]
@comment.company_id = session[:company_id]
if @comment.save
flash[:notice] = "Successfully created comment."
redirect_to :id => nil
else
render :action => 'new'
end
end
private
def find_commentable
params.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
end
end
nil
end
end
およびrake routes
:
location_comments GET /locations/:location_id/comments(.:format) comments#index POST /locations/:location_id/comments(.:format) comments#create new_location_comment GET /locations/:location_id/comments/new(.:format) comments#new edit_location_comment GET /locations/:location_id/comments/:id/edit(.:format) comments#edit location_comment GET /locations/:location_id/comments/:id(.:format) comments#show PUT /locations/:location_id/comments/:id(.:format) comments#update DELETE /locations/:location_id/comments/:id(.:format) comments#destroy locations GET /locations(.:format) locations#index POST /locations(.:format) locations#create new_location GET /locations/new(.:format) locations#new edit_location GET /locations/:id/edit(.:format) locations#edit location GET /locations/:id(.:format) locations#show PUT /locations/:id(.:format) locations#update DELETE /locations/:id(.:format) locations#destroy
場所<=>のコメントの関連付けが多形であることを考えると、少し醜いです(詳細はこちら:祖先の問題を伴う多形コメント)。
/locations/1/comments/
と同じようにロードします/locations/1/comments/1
が/locations/1/comments/new
、ルーティングエラーをスローしますNo route matches {:action=>"show", :controller=>"locations"}
。
私のログは次のように表示されています。
Started GET "/locations/100041506421W500/comments/new" for 127.0.0.1 at 2012-04-29 00:37:43 -0600
Processing by CommentsController#new as HTML
Parameters: {"location_id"=>"100041506421W500"}
Rendered comments/_form.html.erb (2.4ms)
Rendered comments/new.html.erb within layouts/application (3.9ms)
Completed 500 Internal Server Error in 25ms
ActionController::RoutingError (No route matches {:action=>"show", :controller=>"locations"}):
app/views/comments/_form.html.erb:1:in `_app_views_comments__form_html_erb__3729217817518084843_70306885927480'
app/views/comments/new.html.erb:3:in `_app_views_comments_new_html_erb___4071030466810932409_70306918426640'
app/controllers/comments_controller.rb:19:in `new'
Rendered /Users/dan/.rvm/gems/ruby-1.9.3-p0@global/gems/actionpack-3.2.1/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.9ms)
フォームをレンダリングしているように見えますが、どこかで500エラーが発生します。