Search code examples
rubyruby-on-rails-3herokuroutesscaffolding

Heroku Rails app throwing routing error for resource show action only in production


I have a Rails 3.2 application hosted on Heroku, and after local development of a new blog resource it for some reason is running into issues when on my Heroku staging application by throwing an error that says

 Started GET "/blog_posts" for 24.16.156.31 at 2014-09-16 01:27:59 +0000
   Rendered blog_posts/index.html.haml within layouts/application (25.9ms)

 ActionController::RoutingError (No route matches {:action=>"show",  :controller=>"blog_posts", :id=>nil}):
 app/views/blog_posts/index.html.haml:89:in `_app_views_blog_posts_index_html_haml___3120682798112947929_70212833553560'
 app/controllers/blog_posts_controller.rb:10:in `index'

Yet when I run

heroku run rake routes

to display all available routes I can clearly see

                       blog_posts GET        /blog_posts(.:format)                                            blog_posts#index
                                  POST       /blog_posts(.:format)                                            blog_posts#create
                    new_blog_post GET        /blog_posts/new(.:format)                                        blog_posts#new
                   edit_blog_post GET        /blog_posts/:id/edit(.:format)                                   blog_posts#edit
                        blog_post GET        /blog_posts/:id(.:format)                                        blog_posts#show
                                  PUT        /blog_posts/:id(.:format)                                        blog_posts#update
                                  DELETE     /blog_posts/:id(.:format)                                        blog_posts#destroy

at the top of my routes.rb file I have

resources :blog_posts

The only thing I could think of was that when I inherited this project and went ahead to generate the scaffolding for a blog, it created the blog_post_controller of type

class BlogPostsController < InheritedResources::Base

instead of the normal

class BlogPostsController < ApplicationController

which has all the explicitly defined show, index, new, edit, etc, actions that you can alter.

So to cause the scaffolding to return to ApplicationController I added to application.rb this line

config.app_generators.scaffold_controller = :scaffold_controller

Any ideas?

Edit:

blog_posts_controller.rb show and index actions

def index
    @blog_posts = BlogPost.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @blog_posts }
    end
  end

  # GET /blog_posts/1
  # GET /blog_posts/1.json
  def show
    @blog_post = BlogPost.find(params[:id])
    hash = session.exists?
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @blog_post }
    end
  end

Additionally, the URL being accessed is

http://app.herokuapp.com/blog_posts

which should be triggering the index action, not show.

When I attempt to hit blog_posts/1 where 1 is a valid blog_post id, I get

Parameters: {"id"=>"1"}
Rendered blog_posts/show.html.haml within layouts/application (31.9ms) 
ActionController::RoutingError (No route matches {:action=>"show", :controller=>"blog_posts", :id=>nil}):

routes.rb

resources :blog_posts

Solution

  • You try to build an url with a BlogPost that does not exist. Did you double check that a BlogPost with the id 1 exists in your production database? Did you load the record that you pass to your url helper or did you hard code that id?