linking to html file in /public - ruby-on-rails

I have a file 'maps.html' located in /public. I am loading up an index page with contents as follows:
<%= link_to 'redirect_click_here', 'maps.html' %>.
Application.html.erb takes care of the other necessary html elements for the page.
The result of clicking that link is that I am sent to /map/maps.html.
This is slightly logical: the page hosting the link was in the 'map' controller. Still, I want to 'escape' the controller and access the public html file.
I realize that this is a kind of pointless request because I could just put the html file in app/views, but it's just for completion's sake that I put forth this request.
edit
One reason I want to include this file from the /public/ directory is that I don't want it to go through the asset pipeline and inherit the html document structure from application.html.erb. I am going to be including HTML files which include custom heads and I don't want to have to replace the contents of application.html.erb every time.

You should use '/maps.html' in your link, so that it knows it is in the root public folder.

You can access it by
<%= link_to "Maps", "/your_project/maps.html" %>

Related

What is the difference between index.html and index.html.erb in Rails?

I am trying to look up the index.html file in the 'public' directory but it doesn't appear there.
I only find index.html.erb in 'Posts' directory.
My route.rb says the following:
resources :posts
root 'posts#index'
index.html is a static file. If you want static html files, you don't need Ruby on Rails. You want it to be dynamic. You want it to show your posts from your database.
index.html.erb is dynamic, the content is handled by rails before sending it to the browser.
If you have a webserver with static pages, and you just request the domain name, the webserver is normally give you back the static file index.html.
But remember, we don't want static pages. So the root command in the route.rb file specifies what to do when you request only the domain name. It specifies that the index-action of the PostsController is called. This action then will then render your index.html.erb template in the Posts directory.
erb stands for "Embedded RuBy". .erb.html file is a file where you can put html and ruby codes together.
For example:
<%= link_to "Dashboard",root_path%> #ruby code
<a>Dashboard</a>
In .html file you can put only html codes.

Relative URLs within sub application on IIS site

I have "test" website on IIS and "sub1" sub-application within it.
When I request domain.com/sub1, IIS processes sub-application sucessfully.
Some of URLs in code are relative, and when I have something like this in code /component/test, application requests following url domain.com/component/test instead of domain.com/sub1/component/test.
What do I need to change in order to make relative URLs to be valid.
in your code, use #Url.Content("~/") or <% =Url.Content("~/") %> to get to the relative root of the application.
Or, sometimes, you can get away with prefixing with ~/, but generally only with js and css files in your layout
note: the # is for razor view engine, and the <% %> is for aspx view engine, you didn't specify which you were using
EDIT: (based on comment from OP)
CSS should be relative from the directory your CSS is hosted, you can use ../ to traverse directories. For your JS, can you edit those files? I would define a SITE_ROOT variable (above all js files) in your layout and then reference that variable from within your JS files
example: var SITE_ROOT = '#Url.Content("~/")';
then prefix your calls in the JS file with SITE_ROOT + '/relative_url'

Where do you put small snippets of CSS codes for rails app?

I know that most CSS codes go under app/assets/stylesheets, but I have some snippets of CSS codes that are specific to only certain pages. For now, I just have these small CSS codes included in the view files, but I feel like there's ought to be a better way of handling this.
Any suggestion?
Rails convention is to put these in controller specific CSS files:
For example, if a ProjectsController is generated, there will be a new
file at app/assets/javascripts/projects.js.coffee and another at
app/assets/stylesheets/projects.css.scss. You should put any
JavaScript or CSS unique to a controller inside their respective asset
files, as these files can then be loaded just for these controllers
with lines such as <%= javascript_include_tag params[:controller] %>
or <%= stylesheet_link_tag params[:controller] %>.
Putting the CSS inside the views isn't a good idea as you lose features (fingerprinting, auto minification) that the asset management in Rails provides.
Read more here: http://guides.rubyonrails.org/asset_pipeline.html#how-to-use-the-asset-pipeline

Ruby questions about web layer layout

I am trying to make a URL like Link Title from within my application. What is the file that I need to edit to link this URL to a controller and then a view?
Also, I am still working my way through this tutorial:
http://guides.rubyonrails.org/layouts_and_rendering.html
and I am wondering when they do something like this:
<%= stylesheet_link_tag "http://example.com/main.css" %>
is that supposed to live in the application.html.erb file or the index.html.erb file?
What is the file that I need to edit to link this url to a controller and then a view?
routes.rb
See the rails guide
is that supposed to live in the application.html.erb file or the index.html.erb file?
The simple answer is: application.html.erb, inside the head section. There are ways of injecting view template stuff into the head, but if you're just starting out, stick with application.html.erb.

Display html file inside iframe

How to display a HTML file from my system in iframe using rails?
I will explain my issue...
I have a view file that has an iframe which calls an action through <iframe src="controller/action?param=somevalue"></iframe> and the action renders a HTML file based on the params.
The HTML file called has reference to stylesheets and javascripts in the format <script type="text/javascript" src="../common/About.js"></script>
When viewed in browser the HTML file displays correctly with the styles and javascript but when viewed in the application the styling and scripts are not working from the external file. On viewing the source code for the external files i get "Unknown action" error.
What is that i am doing wrong in this?
(reacting to yr last comment): you need to specify css and js files in the iframed html page separately. html in an iframe is rendered completely independent from the surrounding page.
This is not a rails-related question imho.
I found the mistake I made. Its because of routes being not defined properly. When I give relative urls in the html file, the rails views assumes the full path to be some thing like src="controller/common/About.js". As there is no action defined by the name common I was getting the Unknown action error. I have redefined my routes and its working fine now.

Resources