multiple views with multiple rails layouts for multiple head scripts - html

I want each of my view files to have their own layout because each one needs different stuff in their <head> tags with different scripts being run but they should still inherit the same stuff from layouts/application.html.erb. Is this possible?
layouts/application.html.erb -> layouts/a.html.erb -> views/a.index.html.erb
layouts/application.html.erb -> layouts/b.html.erb -> views/b.index.html.erb

content_for :header_tags is for adding extra lines to the <head> of your application.html.erb or base.html.erb. Here's a live example from whatever.html.erb:
<% content_for :header_tags do %>
<%= javascript_include_tag :redmine_helpdesk, :plugin => 'redmine_contacts_helpdesk' %>
<%= stylesheet_link_tag :helpdesk, :plugin => 'redmine_contacts_helpdesk' %>
<% end %>
Then the base.html.erb calls <%= yield :header_tags -%> to express whatever was stored with the :header_tags key.

Related

Exclude a partial from certain views

in my current Rails app, I have a partial in my layouts folder for a navigation sidebar. I would like this sidebar to render for every page of my app except the new and thankyou actions of my score controller, because those two views are going to be a part of an iframe. To do this, I'm making another layout called iframe.html.erb where I'd like to put the logic to exclude the navigation sidebar.
Here is my application.html.erb layout file
<!DOCTYPE html>
<html>
<head>
<title>NPS</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<div id="wrapper">
<%= render 'shared/navigation' %>
<%= yield %>
</div>
</body>
</html>
And here is my iframe.html.erb file
<!DOCTYPE html>
<html>
<head>
<title>NPS</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<div id="wrapper">
<%= if (current_page?(controller: 'scores', action: 'new') || current_page?(controller: 'scores', action: 'thankyou' )) do %>
<%= yield %>
<% end %>
</div>
</body>
</html>
I initially just had an unless statement in my application layout but that wouldn't work on the sign-in page because devise would be looking for those scores views in the devise folder. I'm also not very good with html.erb so I'm sorry if it's just a syntax thing.
Edit:
Here is the error I get with devise
ActionController::UrlGenerationError at /users/sign_in
No route matches {:action=>"new", :controller=>"devise/scores"}
Thanks
in your application.html.erb...
<%= render 'shared/navigation' unless #disable_nav %>
Then in your score controller where you have you have your two views...
def new
#disable_nav = true
# ...
end
def thankyou
#disable_nav = true
# ...
end
You don't need two layouts for that and your syntax is incorrect. Remove iframe.html.erb.
There are several ways to do it. Try this one in application.html.erb:
<div id="wrapper">
<% if !current_page?(controller: 'scores', action: 'new') && !current_page?(controller: 'scores', action: 'thankyou') %>
<%= render 'shared/navigation' %>
<% end %>
<%= yield %>
</div>
It should just work. However, it can be improved. Move this logic into your application_helper.rb:
def should_show_navigation?
!current_page?(controller: 'scores', action: 'new') &&
!current_page?(controller: 'scores', action: 'thankyou')
end
And then, in your layout file (application.html.erb):
<div id="wrapper">
<% if should_show_navigation? %>
<%= render 'shared/navigation' %>
<% end %>
<%= yield %>
</div>
It gets more readable.

Why does my title tag not appear on search results using Ruby on Rails?

I'm trying to add a title tag to my website using ROR.
After adding the desired changes to ... in aplication.html.erb, then commiting and deploying it, I still do not see the complete title tag present with the changes I just made when I view the website on search results.
Why is this the case and is there anything I am forgetting to include?
<!DOCTYPE html>
<html>
<head>
<title>Website Name - Desired title tag added here but doesn't show in search results</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<%= render 'shared/user_info' %>
</head>
<body>
<div id="body-interior">
<%= render 'shared/header' %>
<div class="container">
<% flash.each do |name, msg| %>
<%= content_tag(:div, msg, class: "alert alert-info") %>
<% end %>
<%= yield %>
</div>
<%= render 'shared/footer' %>
</div>
<%= render 'shared/final_scripts' %>
</body>
</html>
Thanks!
When you say search results, I assume you mean search engines like Google? If so, you need to know that changes like these won't be recognized instantly by Google. The changes should be visible as soon as Google (or any other robot) crawls the page in question again.
As far as I know you can force this process with Google's Webmaster Tools.

Ruby on Rails - Ruby code in view file gets printed

I have a Rails 3 project in Aptana Studio 3 with a html.erb view file containing the following code:
<% if #books.blank? %>
<p>
There are not any books currently in the system.
</p>
<% else %>
<p>
These are the current books in our system
</p>
<ul id="books">
<% #books.each do |c| %>
<li>
<%= link_to c.title, {:action => 'show', :id => c.id} -%>
</li>
<% end %>
</ul>
<% end %>
<p>
<%= link_to "Add new Book", {:action => 'new' }%>
</p>
Then in the embedded terminal, I run rails server, click the "Run with Firefox Server" button in Aptana which opens the application with firefox, and directs me to this link: http://127.0.0.1:8020/library/app/views/book/book.html.erb
The problem is that I get this output:
<% if #books.blank? %>
There are not any books currently in the system.
<% else %>
These are the current books in our system
<% #books.each do |c| %>
<%= link_to c.title, {:action => 'show', :id => c.id} -%>
<% end %>
<% end %>
<%= link_to "Add new Book", {:action => 'new' }%>
Seems like the ruby code isn't getting evaluated but rather printed, however the syntax looks alright to me... Does anyone know what might be the problem?
Aptana doesn't open the right page. If you're just using the default server, then you probably want to open localhost:3000.
Some more info: look at the url, it's just the path to a file, not the url for the books index.
Your file path (http://127.0.0.1:8020/library/app/views/book/book.html.erb) also seems strange...
First, the book folder name should be plural (app/views/books). And second, your view code seems like it's the books index page, so it probably should be in app/views/books/index.html.erb.

Inserting rails into HTML file

When writing an HTML file, why use <%= INSERT RAILS HERE %> vs. <% INSERT RAILS HERE %>
<%= %> emits a string, <% %> runs code.
On the pedantic side, you're writing an ERb template, not an HTML file--the syntax is the same whether it's a template for HTML, JS, or whatever.
The ERB docs provide additional (but not complete) information.
<%= %> will return value and display in your page. Assume that you have person.name = 'Dark'
<%= person.name %>
will display Dark in your web page.
<% %> will not return any value to your page. It just embed simple ruby code. Usually used with `control statement'.
<% if person.present? %>
<span><%= person.name %></span>
<% end %>
When we use <%= %> it simply displays the value returned, on the html page.
<% %> executed the code but doesn't dispaly it on the html page.

Placing link at top

How do I place a link at the top of my page when the URL that it is pointing to is not determined until later down the page. In this example, I want to move Create and Edit Scenario links to the top of the page, but as you can see Edit Scenario depends on knowing the #scenario_id first.
<%= will_paginate #scens, :next_label => 'Older', :prev_label => 'Newer' %>
<div class="box">
<% for scenario in #scens %>
<% #created = scenario.created_at %>
<% #updated = scenario.updated_at %>
<% #scenario_id = scenario.id %>
<% if scenario.scenario_image.exists? %>
<%= scenario_image_tag(scenario) %>
<% end %>
<%= simple_format(scenario.description) %>
<% end %>
</div>
<% if session[:role_kind] == "controller" %>
<p>
<%= button_to "Create new scenario", :action => "create" %>
<% if #scens.size > 0 %>
<%= button_to "Edit scenario", :action => "edit", :id => #scenario_id %>
<% end %>
</p>
You can add the link at the top but you will need to programmatically access it later and then assign the URL to it. That needs some kind of reference or look-up capability, I'm thinking client-side javascript but that's as I don't know Ruby.
Alternatively you could create the link later when you have the URL and place the link at the top using CSS positioning. The actual position of all the DOM elements on the page need not match the order in which they are rendered.
One way to do this is to use a helper:
In your helper.rb file:
def stack_example(scens, &block)
html = 'Scenario Details'
edit_link = 'Edit Link'
yield html, edit_link
end
Then in your partial you could have something like:
<% stack_example(#scens) do |html, edit_link| %>
<%= edit_link %><br>
<%= html %>
<% end %>
Should output the following:
Edit Link
Scenario Details
I don't get it. Why do you create model in the view layer? Why wouldn't you create the model variables in the controller? Sth like:
class your_controller
def your_method
#scenario_id = ...
end
end
I think that your problem lays in the invalid MVC usage. Don't you think that all the #member #variables should be initialized before the view starts to render?