2012-04-29 2 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), учитывая, что ассоциация комментариев 4464870395428245080548888 => является полиморфной.

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

Это выглядит как его делает форму, а затем где-то 500 ошибка выплывает.

ответ

3

В какой-то точке зрения new у вас есть неработающая ссылка на location_path, что соответствует {controller: 'location', action: 'show'}, что является неверным маршрутом (ему нужен идентификатор местоположения).

+0

Yup ... никогда бы не подумал об этом через миллион лет. Просто нужно вычислить наш полиморфный_path, чтобы заменить его. –

Смежные вопросы