jyrkim
jyrkim

Reputation: 2869

Heroku: ActionController::RoutingError (No route matches [GET] "/nav/_nav.html")

I have completed Albert Pai’s AngularJS Tutorial: Learn to Build Modern Web Apps with Angular and Rails following the instructions. My app works locally when using WEBrick and Foreman. However after deploying the same app to Heroku, I ran into problems with the following AngularJS statement:

<div ng-include="'nav/_nav.html'"></div>

as at Heroku server the _nav.html partial view isn’t found. So I get the following error:

GET http://fathomless-sands-8666.herokuapp.com/nav/_nav.html 404 (Not Found)

Screenshot (I’m missing the navbar that allows login and registration, Device gem is used):

enter image description here

application.html.erb

<!DOCTYPE html>
<html>
<head>
 <title>FlapperNews</title>
  <%= stylesheet_link_tag    'application', media: 'all' %>
  <%= javascript_include_tag 'application' %>
  <%= csrf_meta_tags %>
</head>
<body ng-app="flapperNews">
    <p class="notice"><%= notice %></p>
    <p class="alert"><%= alert %></p>
    <div class="col-md-6 col-md-offset-3">
      <div ng-include="'nav/_nav.html'"></div>
      <ui-view></ui-view>
    </div>
</body>
</html>

_nav.html

<div class="collapse navbar-collapse pull-right" ng-controller="NavCtrl">
  <ul class="nav navbar-nav">
    <li><a href="#/home">Home</a></li>
    <li ng-hide="signedIn()"><a href="#/login">Log In</a></li>
    <li ng-hide="signedIn()"><a href="#/register">Register</a></li>
    <li ng-show="signedIn()"><a href="#/">{{ user.username }}</a></li>
    <li ng-show="signedIn()"><a ng-click="logout()">Log Out</a></li>
  </ul>
</div>

Heroku log:

2015-02-23T12:26:14.936469+00:00 heroku[router]: at=info method=GET path="/assets/application-71f8573cbf92d37602f8bd1449f54ed5.js" host=fathomless-sands-8666.herokuapp.com request_id=6695089f-3134-4397-81da-6832439bc1a4 fwd="212.149.201.185" dyno=web.1 connect=0ms service=10ms status=200 bytes=166290
2015-02-23T12:26:15.938797+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=fathomless-sands-8666.herokuapp.com request_id=61831df5-456c-492b-a315-f0c13faa64cf fwd="212.149.201.185" dyno=web.1 connect=0ms service=3ms status=200 bytes=143
2015-02-23T12:26:15.957956+00:00 heroku[router]: at=info method=GET path="/nav/_nav.html" host=fathomless-sands-8666.herokuapp.com request_id=c40773c6-2f68-4533-b2a3-d9a1bad30ce7 fwd="212.149.201.185" dyno=web.1 connect=0ms service=59ms status=404 bytes=1531
2015-02-23T12:26:15.898369+00:00 app[web.1]: source=rack-timeout id=c40773c6-2f68-4533-b2a3-d9a1bad30ce7 wait=2ms timeout=20000ms state=ready
2015-02-23T12:26:15.935577+00:00 app[web.1]: source=rack-timeout id=61831df5-456c-492b-a315-f0c13faa64cf wait=2ms timeout=20000ms state=ready
2015-02-23T12:26:15.955427+00:00 app[web.1]: 
2015-02-23T12:26:15.955433+00:00 app[web.1]: ActionController::RoutingError (No route matches [GET] "/nav/_nav.html"):
2015-02-23T12:26:15.955437+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'

routes.rb

FlapperNews::Application.routes.draw do
  devise_for :users
  root to: 'application#angular'
  resources :posts, only: [:create, :index, :show] do
    resources :comments, only: [:show, :create] do
      member do
        put '/upvote' => 'comments#upvote'
      end
    end

    member do
      put '/upvote' => 'posts#upvote'
    end
  end

The code that was deployed to Heroku can be found at Github: https://github.com/jyrkim/flapper-news/blob/master/app/views/layouts/application.html.erb I have also created Procfile and set the Puma gem as instructed at Heroku: https://devcenter.heroku.com/articles/getting-started-with-rails4#specify-ruby-version-in-app The only thing that I didn’t specify in the Gemfile was the Ruby version. My Ubuntu uses Ruby version 2.1 and Heroku wants to use version 2.0 for my app. (Maybe that could cause the problem)

If someone has a clue what might be causing the problem at Heroku environment, then please let me know - Thanks.

Upvotes: 1

Views: 1174

Answers (1)

Steve
Steve

Reputation: 15736

It looks like you ran an asset precompilation locally when you first deployed to Heroku (because you have older files in your public/assets directory) but have not run it again since making further changes to the site and re-deploying to Heroku.

This means that your pre-compiled JS is out of step with your application.html.erb. The JS your are serving from Heroku was compiled before nav/_nav.html was created and has not been updated since.

You can either run the asset pre-compilation again locally:

RAILS_ENV=production bundle exec rake assets:precompile

Or remove the public/assets directory, commit and redeploy so that Heroku performs the asset precompilation step as part of the deployment during slug compilation. Doing it this way means that you don't have to worry about running the rake task locally in future.

See https://devcenter.heroku.com/articles/rails-asset-pipeline

Upvotes: 2

Related Questions