Sprockets::FileNotFound at / couldn't find file 'action_cable' with type 'application/javascript'

时间:2016-08-31 18:26:26

标签: ruby-on-rails ruby ruby-on-rails-4

New to ROR and working through a sample page but am experiencing the following error when I go to localhost:3000

couldn't find file 'action_cable' with type 'application/javascript' Checked in these paths: <...>

I'm not sure what is causing this/how to fix but if I add "layout false" to my controller file then everything works ok. Below are the files:

Here is my gem file:

source 'https://rubygems.org'
ruby '2.3.0'
gem 'rails', '4.2.4'

# Rails defaults
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'byebug'
gem 'web-console'
gem 'spring'
gem 'foundation-rails', '~> 5.5'
gem 'gibbon'
gem 'high_voltage'
gem 'simple_form'
group :development do
  gem 'better_errors'
  gem 'quiet_assets'
  gem 'rails_layout'
end
gem 'sqlite3'
group :production do
  gem 'pg'
  gem 'rails_12factor'
end

View file:

 <h1>Home</h1>
 <p>Welcome to the home of <%= @owner.name %>. </p>
 <p>I was born on <%= @owner.birthdate %>
 <p>Only <%= @owner.countdown %> days until my birthday!</p>

Model File:

class Owner
    def name
            name = 'Foobar Radigan'
    end

        def birthdate
            birthdate = Date.new(1990,12,22)
        end

        def countdown
                today = Date.today
                birthday = Date.new(today.year, birthdate.month, birthdate.day)
                if birthday > today
                        countdown = (birthday - today).to_i
                else
                        countdown = (birthday.next_year - today).to_i
                end
        end

end

Controller file:

 class VisitorsController < ApplicationController
        # Do not use layout
        # layout false

    def new
            Rails.logger.debug 'DEBUG: entering new method'
            @owner = Owner.new
            render 'visitors/new'
            Rails.logger.debug 'DEBUG: owner name is ' + @owner.name
    end

end

*Note that if uncomment the 'layout false' line everything works, but I have no layout.

2 个答案:

答案 0 :(得分:3)

简短的答案:

只需删除 / app / assets / javascripts / 中的 cable.js 即可解决问题。

LONG ANSWER:

为了确保您不再出现此问题,这里有一些回顾性推测。

我的猜测是你在全局系统中安装了RoR 5.x,并且使用

生成了你认为是4.x RoR应用程序的内容之后
#rails _4.x.x_ new app_name

你最终得到了一个5.x RoR应用程序。现在,即使你手动更新Gemfile,表明你需要rails 4.x.x,你的项目也会 还有一些剩菜,其中一个是cable.js文件。这就是我认为你得到的错误。

现在,如何避免这种情况。据我所知,上面强制执行旧版rails版本的方法没有记录,并不总是有效。这是另一种肯定有效的方法。

假设您在全局安装了5.x RoR,并且想要为名为rails4_app的项目强制执行RoR v.4.x.x,那么:

#mkdir rails4_app
#cd rails4_app
#echo "source 'https://rubygems.org'" > Gemfile
#echo "gem 'rails', '3.2.17'" >> Gemfile
#bundle install
#bundle exec rails new . --force --skip-bundle
#bundle update

现在你走了 - 现在你已经开始使用rails 4.x.x app了。

答案 1 :(得分:0)

ActionCable is a feature of Rails 5, but you specified Rails 4 in your Gemfile