Rails application not displaying index.html in development mode - ruby-on-rails

I have placed an index.html file in the public directory of my Rails 4 application but it still shows this deleted file as rails welcome page. I am using Webrick in development mode. I have another app almost identical in setup which works fine.

As written above, Rails 4 has removed the default index.html page in the /public folder. If you want to achieve the functionality anyway, you have to render the page manually. Just create a controller, and render the index page in your chosen controller action like this:
def index
render :file => 'public/index.html' and return
end
EDIT:
If you want to serve the files without using the Rails stack, you have to configure your Web server (Apache/Nginx/whatever) to do so. Configurations for Nginx and Apache.
Additionally, you have to disable Rails' static file rendering in your configuration by setting serve_static_assets to false:
config.serve_static_assets configures Rails itself to serve static assets. Defaults
to true, but in the production environment is turned off as the server software (e.g.
Nginx or Apache) used to run the application should serve static assets instead.
Unlike the default setting set this to true when running (absolutely not recommended!)
or testing your app in production mode using WEBrick. Otherwise you won´t be able use
page caching and requests for files that exist regularly under the public directory
will anyway hit your Rails app.
So, if you want to use this in development mode, set config.serve_static_assets to false in environments/development.rb.

Rails 4 uses dynamic index.html. Read on http://blog.remarkablelabs.com/2012/12/dynamic-index-html-rails-4-countdown-to-2013

Press F5 in navigator, the index can be in the cache.

Related

Run built frontend (react) as a static file in Rails

So I'm trying to run a React app as a static html file (it's index file) in a Rails app. This works in express/node and it's quite common in projects, but i can't find a solution for this in Rails. So the react app is already built and i'm trying to serve it as a static html from the /public folder. EDIT However i can't get it to run, it shows me that it's loaded, there's no error in the rails console or the browser console, however nothing gets shown on the screen.
routes.rb
get "react_app" => "projects#react_app"
projects_controller.rb
def react_app
render :file => 'public/react_app/public/index.html'
end
i also have
config.serve_static_assets = true
I'm using Rails 5.2. I am not using yarn, i'm trying to run it via the plain assets pipeline.
Any ideas ? Or is there another better way to do it maybe ?
This app needs to be all in one place, and unfortunately gems such as react-on-rails or react-rails are not an option, since they require too much strange customization and often crash due to Redux.

Can I Identify The Environment Using HTML Code?

I just figured out how to create custom error pages in the public folder using I18n. Those error pages have links that will return the user to a page in the application such as the home page.
When my application was running 3.2.13 I had the custom error pages in the app folder using config.exceptions_app = self.routes in application.rb. When I had that code all I had to do was use a link_to statement pointing to the route I wanted the user to return to. If I was in the development environment it would go to http://localhost:3000/somelink and in production it would go to http://myrailsapp.com/somelink. I replaced these with the ones in the public folder after rewriting them in Rails 4 because it appears that config.exceptions_app = self.routes is ignored in Rails 4.
My error pages in the public folder are html, not html.erb so there is no ruby/rails code. I would like to replicate what I had previously where I can check the environment in my error pages and point to localhost for development or my domain URL for production. I currently have code in my error page similar to this:
My Link Text
I'm definitely open to changing these to html.erb files. I initially thought about that but from my research and trying what I found nothing is working for Rails 4.
Any help would be appreciated.
You don't need any "autodetection" in html, just cut the domain part from the url and use a local path instead.
Just change http://myrailsapp.com/en/home to /en/home.
And yes, you do need the opening slash to be independent from the current_url in your link.

How do I stop caching of layout in production with Ruby on Rails?

I'm building a rails app allows the admin to load (and reload) an .erb template that will function as a layout. i.e. the application.html.erb
On occasion when updating an already created template to my hosted app the changes do not take effect. If I load a differently named file to the same template this does show the changes.
This is not an issue in DEV on my local environment but is an issue for PROD. Is there a setting that controls the caching of the layout for a page? The dynamic content is no problem but the static layout does not change unless I restart the app. Please let me know if you have any brilliant insights about this issue, or if it needs greater explanation.
Just figured it out - add this to your production.rb environment file;
config.action_view.cache_template_loading = false

Where should I put files to be sent via send_file in Rails 3?

Where am I supposed to put files to be served via send_file in Rails 3?
Suppose I'm serving a browser extension that may be updated once a month or two, should I put it in the asset pipeline? And how can I access it from the controller?
If I refer to it using #{Rails.root} it won't work in the production environment.
I don't want to have to change config.action_dispatch.x_sendfile_header every time I'll deploy on a different webserver, and I don't want to set config.serve_static_assets = true since this is a distortion of the development environment.
You should put them in a directory outside of Public, otherwise they will be viewable by the browser directly without authentication.
I usually create a directory in Rails.root, something like
Rails.root
- app
- config
- db
- secure_files
So the files will be present in secure_files.

Why does setting directory path in cache_store =: file_store make no difference?

I set
config.cache_store = :file_store, "#{Rails.root}/public/cache"
but everything is written directly into the /public directory.
what could be wrong?
Ruby 1.8.7, Rails 3.2
Maybe this can help. It's from the Rails 2.3 doc, though:
By default, the page cache directory is set to Rails.public_path
(which is usually set to File.join(self.root, "public") – that is, the
public directory under your Rails application’s root). This can be
configured by changing the configuration setting
config.action_controller.page_cache_directory. Changing the default
from /public helps avoid naming conflicts, since you may want to put
other static html in /public, but changing this will require web
server reconfiguration to let the web server know where to serve the
cached files from.
http://guides.rubyonrails.org/v2.3.11/caching_with_rails.html
From this it may seem like your problem:
but everything is written directly into the /public directory.
May be because it was just writing to the default cache dir.

Resources