Config attributes in <html> with Rails - ruby-on-rails

Generally Rails main application views/layout omits the <html> tag, focusing on defining what should be inside of it, like the <head> and <body> tags.
What I'm actually trying to do is to define a simple attribute inside of the <html> top level tag, but I don't know where to write it.
Any tips?

It's available in application.html.erb in the views/layout folder
This file in included by default in all your views.

Related

How to Render a Grails View Without a Layout

If you have a controller action, but don't want the view to be rendered with the default layout (in my case layout/main.gsp), is there a render option you can enter into the controller action or something similar?
def comingSoon {
static layout = none; //not correct, but something like this?
}
In your view file, you might see something like this
<html>
<head>
<meta name="layout" content="main"/> // delete this line
<title>Coming Soon</title>
</head>
...
Remove the meta tag with the name="layout". This meta tag is the one that tells sitemesh to use the main layout
Without more details it's difficult to say 100% what case you are in so I'll attempt to answer both.
If you are using dynamically scaffolded views then you'll need to generate the GSPs so you can remove the <meta name="layout" tag from them. This will keep any layout from being used.
Alternatively you could alter the scaffolding templates (within the scaffolding plugin) to include some additional logic about not applying a layout when the domain class has some static property (as your question contains).
You can remove <meta name="layout" content="main"/> to vanish exists layout. Or you may customize layout by edit that main.gsp page in your-project\grails-app\views\layouts\

Render HTML file with CSS and local variables

I have the following content in an HTML file placed under public/company/ with a CSS file css/style.css:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<link href="css/style.css" rel="stylesheet" type="text/css" media="all"/>
</head>
<body>
<div class="title">Name {name}</div>
</body>
</html>
I want to render this HTML file with the CSS stylesheet from an action and replace {name} without changing the physical file content and place. I can render the HTML file but the CSS file would not be found. Can anyone help me render the HTML file with the CSS file and replace the {name}?
css/style.css when called from company/file.html will try to load company/css/style.css.
Click here to see an article the way you can do that. Hope it will help!
But, I'm not sure why you are looking to render that way where Rails has all the features in place.
If your CSS file is located at /public/company/css/style.css, then your static HTML file should link it with an absolute path. Use a leading / in the resource's path indicate an absolute path.:
<link href="/company/css/style.css" rel="stylesheet" type="text/css" media="all"/>
Note that the Rails router will likely catch this request and raise an exception. Depending on the web server, you may need to enable static assets or serve directly, bypassing Rails altogether.
Changing the content of a static HTML file server-side without actually modifying it (ie. adding ERb) isn't straightforward. You could:
inject some javascript into the response to perform modifications client-side. (ugly)
load the static HTML file using Nokogiri, et. al., modify the content, and send the output to the client. (expensive)
Sounds to me like these vanilla HTML/CSS files are third party (designer? client?) and you would like to drop it in /public and have it work automatically with Rails. That would be nice, but it's not that easy. You'd be better off using Rails' built-in templating system, but that means modifying files and moving them to the expected locations.

Grails Layout, how to do that?

I have a grails app named qotd which has a controller named quote. I have written view file for this controller named as random.gsp file as :
<html>
<head>
<title>Random Quote</title>
</head>
<body>
<div id ="quote">
<q>${content}</q>
<p>${author}</p>
</div>
</body>
Now i have to write down the Grails Layout for this controller. For example i have to locate my .css and images files etc, how to do that(better if an example is shown)? And where i should place that file?
Thanks in advance.
When you create a Grails application with grails create-app qotd, you get a file grails-app/views/layout/main.gsp. Take a look at that file, as it shows you how to include css, js and images. This file is the default Sitemesh layout file. To use it, change your random.gsp to
<head>
<meta name='layout' content='main'/>
<title>Random Quote</title>
</head>
<body>
<div id ="quote">
<q>${content}</q>
<p>${author}</p>
</div>
</body>
Then the layout file main.gsp will be wrapped around your content.
To learn more about Sitemesh, take a look at chapter 7 in the Grails documentation

Rails - is there a way to get Rails to render a Style .css file inside of the view?

Instead of the page making another request for the CSS, I would like to have the Rails view render the CSS file in the page, so it's only 1 request.
Is this possible?
Altough the typical way to include CSS is using stylesheet_link_tag (in particular so it gets cached by the client), it is possible to put it directly in your <HEAD> in Rails 3.1:
<HEAD>
....
<STYLE type="text/css">
<%= YouAppName::Application.assets["your_stylesheet.css"].to_s.html_safe %>
</STYLE>
</HEAD>
I adapted this from this post.
<%= stylesheet_link_tag :all %> assuming your CSS file is in your public/stylesheets folder (which is the conventional place to store stylesheets). Of course, instead of :all, you can specify a specific file.
in your page's block.
I usually do this in my application.html.erb (which is a layout in your app/views/layouts) folder. But you can do this in any view file with a block.
Good luck!
Just wrap your CSS in
<style></style>
within your view.

How does Grails know to apply a "layout" to pages it renders?

I've been going through the book "The Definitive Guide to Grails" (Rocher/Brown) and in Chapter 04, this mysterious thing called a "layout" just appeared with no explanation. (And there's no "layout" in the index. As far as I know, it's never explained.)
How does the system know to "inherit" the pages from layout/main.gsp? There's nothing about "layouts" in the index, and it seems to have just appeared.
On their sample app, a simple store site, the URL mappings for the / homepage say
"/"(controller:"store")
and store controller's empty "index" closure
package com.g2one.gtunes
class StoreController {
def index = {
}
}
simply tells it to render store/index.gsp
The store/index.gsp just has a few lines of HTML; no layout gets included with any directive
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="layout" content="main">
<title>gTunes Store</title>
<g:javascript library="prototype"></g:javascript>
</head>
<body id="body">
<h1>Your online music store and storage service!</h1>
<p>Manage your own library, browse music and purchase new tracks as they become available</p>
</body>
</html>
When I run the sample the page shown for "/" isn't just this simple HTML, it's the contents of "layouts/main.gsp" with this information magically inserted inside it.
I don't see how the information in layout/main.gsp gets applied to the page, how the elements get mixed together. I've been following through the book page by page and this functionality just "appeared" with no explanation.
The <meta name="layout" content="main"> tag includes the layout in the gsp page.
You can view the grails-app/views/layouts/main.gsp to view and modify the layout. You can copy main.gsp to mymain.gsp, modify it, then change layout entry in the gsp page to reference mymain.gsp instead of main.gsp and experiment with customizing your layout preserving your ability to easily back out your changes.
Grails uses sitemesh under the covers (like it uses hibernate and spring) to do view layouts. There is a web-app/WEB-INF/sitemesh.xml configuration file in the project directory as well. This particular file isn't that helpful, but it references a class in the groovy project if you wanted to deeply understand how grails is using sitemesh (this probably isn't necessary).
Here's your directive:
<meta name="layout" content="main">
main.gsp contains <g:layoutHead> and <g:layoutBody>, where the <head> and <body> content of the index.gsp are folded into the layout to create the final page.
One recent trick that appears to work, if you name your layout to match your controller it appears (in Grails 2.3.4 at least) to automatically use that template.
For example, my controller:
// grails-app/controllers/myapp/HomeController.groovy
package myapp
class HomeController {
def index() {
[ myvar: "Test" ]
}
}
my layout:
// grails-app/views/layouts/home.gsp
<html>
<head></head>
<body>
<h1>Home</h1>
<g:layoutBody />
</body>
</html>
my view:
// grails-app/views/home/index.gsp
<p>${ myvar }</p>
renders using the home layout.
Also, you can specify a layout for all your actions in a controller like this:
class HomeController {
static layout = "someotherlayout"
// actions will render using grails-app/views/layouts/someotherlayout.gsp
}

Resources