I've been trying to launch my project https://github.com/robdrosenberg/news-hunt on Heroku the last couple days and heroku won't serve my public/index.html page.
Here is the error I'm currently getting:
ActionController::RoutingError (No route matches [GET] "/")
This error has been brought up all over StackOverflow and I've tried as many of those solutions as I could.
For example, routing to the file directly through a welcome controller gives me an error as well
Routes
Rails.application.routes.draw do
post 'user_token' => 'user_token#create'
post 'users' => 'users#create'
namespace :api do
get 'reddit' => 'posts#reddit'
get 'producthunt' => 'posts#producthunt'
get 'medium' => 'posts#medium'
get 'hackernews' => 'posts#hackernews'
get 'githubtrending' => 'posts#githubtrending'
get 'all' => 'posts#all'
get 'bookmarks' => 'bookmarks#index'
post 'bookmarks' => 'bookmarks#create'
delete 'bookmarks' => 'bookmarks#destroy'
end
root 'welcome#index'
end
Controller
class WelcomeController < ApplicationController
def index
render file: Rails.root.join('public','index.html')
end
end
Error
ActionView::MissingTemplate (Missing template public/index.html with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}. Searched in: app/app/views
Changing config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present? to = true hasn't made a difference.
I've tried using redirects which result in the app crashing from too many redirects.
What I find odd is when I try to render the file directly it searches in the app/views folder. Everything works fine locally, so it has to be something with the production environment and Heroku.
I'm using Rails in API mode and using Vue through CDN in my index.html file.
I deployed a different project the same way and had no issues. You can find that codebase here: https://github.com/robdrosenberg/commitment-ledger.
Any help is greatly appreciated!
You don’t actually have a public/index.html page in that repo, but rather a public/Index.html (note the case of the ‘i’ and ‘I’). This won’t make much difference if you’re developing on Windows or Mac, but on Linux (which is used on Heroku) they will be treated as different files.
Rename the file to index.html (use git mv and don’t forget to commit) and it should work.
Is there a reason you want to redirect to public/index? Of course, I don't know your application, so this may not be helpful. Have you considered moving your index.html file to the views/welcome directory? This is the default location that the welcome#index method will render a file from.
Try adding this to your config/routes.rb file:
get '', to: redirect('/Index.html')
That way the requests to the application root will redirect to the index file in the public folder.
As mentioned below you have named the file with a capital ‘I’, you will need to use the same case when referencing it in the redirect above, as updated.
Related
Summary
I want to build a rails monolith with Domain Objects that are easily portable as the app grows. I also want to start in the app/ directory instead of starting with a Rails Engine because engines seem to be a lot of overhead for a feature that may not endure, or the feature might get moved to an HTTP endpoint soon.
Progress
1. Scaffold
Running rails g scaffold post generates the following structure in app/
(in addition to other files)
app/
controllers/
posts_controller.rb
models/
post.rb
views
posts/
2. Swap Directory order
Is it possible to config load paths in order to invert directories so that the following would work;
app/
post/
controllers/
post_controller.rb
models/
post.rb
views/
index, show, etc...
(I want to have all my MVC for Post in the post/ directory in preparation to move post/:
down to lib/
to a gem
into an engine
to my micro service
or maybe even to the trash because it's a terrible feature
3. Uninitialized constant PostsController
Currently, simply inverting files provides;
uninitialized constant PostsController even with tinkering with variations of config.autoload_paths += %W( #{config.root}/app/post/* ) in application.rb. #bbozo's suggestion below worked by explicitly including files rather than use '*' like so;
class Application < Rails::Application
config.autoload_paths += %W( #{config.root}/app/ )
config.autoload_paths += %W( #{config.root}/app/post/controllers )
config.autoload_paths += %W( #{config.root}/app/post/models )
4. ActionView::MissingTemplate
The next issue I'm having is ActionView::MissingTemplate
ActionView::MissingTemplate (Missing template posts/index, application/index with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in:
* "/app/views"
):
.bundle/gems/actionview-4.2.0/lib/action_view/path_set.rb:46:in `find'
.bundle/gems/actionview-4.2.0/lib/action_view/lookup_context.rb:121:in `find'
.bundle/gems/actionview-4.2.0/lib/action_view/renderer/abstract_renderer.rb:18:in `find_template'
.bundle/gems/actionview-4.2.0/lib/action_view/renderer/template_renderer.rb:40:in `determine_template'
.bundle/gems/actionview-4.2.0/lib/action_view/renderer/template_renderer.rb:8:in `render'
.bundle/gems/actionview-4.2.0/lib/action_view/renderer/renderer.rb:42:in `render_template'
.bundle/gems/actionview-4.2.0/lib/action_view/renderer/renderer.rb:23:in `render'
.bundle/gems/actionview-4.2.0/lib/action_view/rendering.rb:100:in `_render_template'
.bundle/gems/actionpack-4.2.0/lib/action_controller/metal/streaming.rb:217:in `_render_temp
[cut out the rest]
From the rails guides I read about the file: flag for absolutes so I got to the Index when I added the following:
def index
#posts = Post.all
render file: 'app/post/views/posts/index'
end
The Index rendered successfully but there was lots of re-configuration I had to do for this single endpoint. I'm trying to determine if I can manage this in the guts and it just works for Post#show, Post#edit, Post#create and other Domain Objects such as Likes
5. Current status
Will I need to reconfigure application.rb and render file: for every endpoint?
Solving Rails load paths
This should work afaik:
config.autoload_paths += %W( #{config.root}/app/post/models )
config.autoload_paths += %W( #{config.root}/app/post/controllers )
config.autoload_paths += %W( #{config.root}/app/post/views )
But consider that maybe you're trying to reimplement a Rails engine, so I'd suggest investigating in that direction,
https://blog.pivotal.io/labs/labs/migrating-from-a-single-rails-app-to-a-suite-of-rails-engines
after some experience with breaking up rails apps into component engines, it seems to me from what you're trying to do that that's the direction I'd take
Solving view lookup paths
See How can I add a view path to Rails's partial rendering lookup?,
you could probably do something like this
class BasePostsController < ApplicationController
# I assume you have a base controller for Post from which other
# post controllers inherit from, this would be a good fit
prepend_view_path 'app/post/views'
end
Knowing when to go for plan B :P
Plan B would be to give up on folder structure with app/post/controller and go for folder structure 'app/controller/post' :)
No load paths updates necessary, in the models:
create folder app/models/post
create empty module in app/models/post.rb saying just module Post; end
every model you now put in 'app/models/post' folder can now be namespaced to Post::MyModel and loaded correctly by Rails
For controllers add a namespaced route, see this answer for a possible solution Rails Controller Namespace
ruby -v = 2.1.6p336
rails -v = 4.2.1
MySQL --version = 14.14
New to programming and Rails. Going through tutorials.
did rails generate controller welcome index.
Rails Server - localhost:3000 shows Welcome aboard page.
Change config/routes.rb file... un-comment out root 'welcome#index' (got rid of the #. (line 8)
Rails Server - localhost:3000 shows error.
----------copied from localhost:3000------------------------------------------
ExecJS::ProgramError in Welcome#index
Showing c:/row/dev/readit/app/views/layouts/application.html.erb where line #6 raised:
TypeError: Object doesn't support this property or method
Rails.root: c:/row/dev/readit
Application Trace | Framework Trace | Full Trace
app/views/layouts/application.html.erb:6:in `_app_views_layouts_application_html_erb___173287605_49138164'
commented the root 'welcome#index' out again, and I have the Welcome Aboard page back.
If I execute localhost:3000/index
I get a routing error. No route matches [GET] "/index"
I have:
controllers/welcome_controller.rb
views/welcome/index.html.erb
this is the start of the config/routes.rb file -----------------
Rails.application.routes.draw do
get 'welcome/index'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'welcome#index'
How do I get the config/routes.rb file to display the views/welcome/index.html.erb file?
Does this have anything to do with the MySQL socket I'm suppose to put in the config/database.yml ? Because I don't what to put there.
It looks like there is an error at line 6 on "application.html.erb". If you want to create a hyperlink from that page to the "root" page, you should use the following view helper:
<%= link_to "Home", root_path %>
When the page is rendered and you check the HTML code using the Chrome's inspector, you will see that above code is converted to:
Home
In your app/views/layouts/application.html.erb, try changing the line:
<%= javascript_include_tag 'application', data-turbolinks-track => true %>
To this (removing the data-turbolinks-track => true):
<%= javascript_include_tag 'application' %>
That might fix it. You shouldn't remove the entire line as it's likely you'll need JS available in your layout at some point in the project :)
Hope it helps!
I fixed it by renaming my [It may not sound correct but it worked for me , give it a try]
app/views/layouts/application.html.erb
TO
app/views/layouts/default.html.erb
I hope it works for you as well
My app works on my local machine, but won't work on the production server.
Using capistrano, everything is up and running under /var/www/current.
Restarted web server.
Getting the site's custom 404, but can't even retrieve stuff out of /public/images using the direct link.
Getting this in the log:
ActionView::MissingTemplate (Missing template access/index with {:locale=>[:en, :en], :handlers=>[:rxml, :builder, :erb, :rjs, :rhtml], :formats=>[:html]} in view paths "/var/www/releases/20110703002055/app/views", "/var/www/shared/bundle/ruby/1.8/gems/kaminari-0.12.4/app/views"):
I've been playing with routes.rb - here is the only line from it right now:
root :to => 'access#index', :as => 'access'
I do have these in access_controller.rb:
def index
end
def welcome
respond_to do |format|
format.html # welcome.html.erb
end
end
I have a template index.html.erb and welcome.html.erb in the app/views/access directory.
I don't really know capistrano, but if you are using Git or something like that to push your code to your server, it looks like you have not added all the files to your repository. (it whines over not finding access/index.html.erb)
In git it's like:
git add .
git commit -m "adding missing files"
I'm new to Ruby on Rails (formerly and currently PHP expert) so forgive my ignorance but I'm trying to get Sinatra working as middleware to redirect some old urls since I tried the gem rack-rewrite and couldn't get that to work either.
I am using code samples from ASCIIcast so in my routes.rb I have the following:
root :to => HomeApp
(^ I'm redirecting the root only for testing)
In my lib folder I have home_app.rb
class HomeApp < Sinatra::Base
get "/" do
"Hello from Sinatra"
end
end
When I start the server (or if its already running) it produces the error:
routes.rb:10: uninitialized constant HomeApp
Which seems that it just isn't aware of the lib/home_app.rb file.
I have included Sinatra in my Gemfile and ran bundle install and confirms it is included.
I just want to reroute old urls from my old site to my new ruby app but can't get any of this middleware/rack stuff working. All documentation assumes you aren't a total newb or is for RoR pre-3.0.
You don't need to use Sinatra if you want to redirect some URLs. You can use the new redirect method. See the Rails Dispatch article.
match "/stories/:year/:month/:day/:name" => redirect("/%{name}")
constraints :user_agent => /iPhone/, :subdomain => /^(?!i\.)/ do
match "*path" => redirect {|params, req| "http://i.myapp.com/#{req.fullpath}" }
end
In your specific case, the problem is that the HomeApp class is not loaded. Either add the /lib folder to your load path changing application.rb
config.autoload_paths += %W( #{config.root}/lib )
or require the file.
So I am getting a 500 server error when attempting to bring up a rhtml page in rails. When I start the WEBrick server, I get the welcome to rails homepage. The name of the app is hello. I generated the controller from the command line and it looks like
class HelloController < ApplicationController
def there
end
end
I have my view (there.rhtml) in views/hello/there.rhtml. However the http://localhost:3000/hello/there gets a 500 sever error. I am currently running this on a vista box. Any ideas?
Have you defined the route ? It is defined in routes.rb . Also you can try checking the development logs to see what exactly is the issue .
could post the error log message? as Nm suggested it can be a route problem and if it is try to add this route in routes.rb file
map.connect '/hello/there', :controller => 'hello', :action => 'there'