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

考虑到位置 <=> 评论关联是多态的,这有点难看(更多细节在这里:带有祖先问题的多态评论)。

/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 错误。

4

1 回答 1

3

在视图中的某处,new您有一个断开的链接location_path,它转换为 {controller: 'location', action: 'show'},这是一条无效路线(它需要一个位置 ID)。

于 2012-04-29T06:51:48.937 回答