2012-04-29 6 views
0

を失敗:ネストされたルーティング - 新しいアクションが唯一の私のルートファイルです。ここ

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 

にです少し醜い(詳細はこちら:Polymorphic Comments with Ancestry Problems)、場所は< =>コメントの関連付けは多型です。

/locations/1/comments//locations/1/comments/1がロードされますが、/locations/1/comments/newはルーティングエラーNo route matches {:action=>"show", :controller=>"locations"}をスローします。

私のログが表示されます。そのフォームをレンダリングして、どこかに500エラーがアップ作物のよう

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/[email protected]/gems/actionpack-3.2.1/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.9ms) 

に見えます。

答えて

3

newビューのどこかに、location_pathへのリンクがあります。これは無効なルート(場所IDが必要)である{controller: 'location'、action: 'show'}に変換されます。

+0

うん... 100万年後には決して考えなかっただろう。 polymorphic_pathを置き換えるには、これを理解する必要があります。 –

関連する問題