I have set up a title instance variable in application_helper.rb, but the base_title does not display in the browser. Can you tell me what's wrong?
application_helper.rb
module ApplicationHelper
# Return a title on a per-page basis
def title
base_title = "Ruby on Rails Tutorial Sample App"
if #title.nil?
base_title
else
"#{base_title} | #{#title}"
end
end
end
application.html.erb
<!DOCTYPE html>
<html>
<head>
<title><%= #title %></title>
<%= csrf_meta_tag %>
</head>
<body>
<%= yield %>
</body>
</html>
In the view you're not actually calling the helper function, it's just trying to output the instance variable (#title), don't prefix a function call with #:
<title><%= title %></title>
Related
Just like in title: What is the proper way to integrate SEO stuff like Facebook OpenGraph tags in apps that use rails backend API (installed with webpacker) with a framework like Vue?
This is how part of my config/routes.rb looks like:
namespace :api, defaults: { format: :json } do
namespace :v1 do
resources :posts, only: [:index, :show]
end
end
get '/', to: 'front#index', format: false
get '/*path', to: 'front#index', format: false
For now app is designed in the way, where all requests on front-end part are handled by vue-router.
My layouts/application.html looks almost, like on the attached snippet:
<html>
<head>
<%= javascript_pack_tag 'front' %>
<%= stylesheet_pack_tag 'front' %>
</head>
<body>
<div id="app">
<%= yield %>
</div>
</body>
</html>
My question is, what is the proper way to manage stuff like Facebook Open Graph when there is no straight forward access to <head> section?
I can't use fairly default rails stuff like content_for because everything happens inside script.
Or maybe am I wrong? Missing something fancy, important here?
It would be grateful to get any advice, thanks.
I am working on a rails app, which is getting upgraded from rails(3.0.9) to rails (3.2.22). I will try to demonstrate the problem through a demo rails app.
I created demo app's for different rails versions. I have something like this inside.
class ApplicationController < ActionController::Base
protect_from_forgery
layout :select_layout
def select_layout
#tmp = 'tmp variable'
#layout = 'application'
end
end
And accessing #tmp inside layouts/application.html.erb :-
<!DOCTYPE html>
<html>
<head>
<title>TwistageScopeIssue</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %> <br/>
<%= #tmp %> <br/>
</body>
</html>
Values(#tmp) displays correctly('tmp variable') in : 3.0.x and 3.1.x .
Values(#tmp) gets set to nil : 3.2.x onwards
I am still looking into the rails source code to get some information, meanwhile any thoughts/idea's would be really helpful.
Thanks
I am trying to build rails HTML emails, but the structure (header and footer) of the emails is duplicated in each one. Typically it's not a problem, but with the inline styles as well, it seems like it can be an issue if I want to change the color. How can I pull these elements out of each file and into one?
Also, is there anyway to eliminate the duplication of text between the html.erb and text.erb files.
A simple way to do it is to reference a couple of partials. Let's say they're named something like this:
_email_header.html.erb
_email_footer.html.erb
Then you can refer to them inside each of your emails:
<%= render :partial => 'email_header' %>
Blah, email-specific content here...
<%= render :partial => 'email_footer' %>
That will work, but will still lead to a bunch of copy-paste, albeit less than the original version with the full structure inlined. A cleaner way to manage this is to set up a custom layout for these emails.
The "Layout and Rendering" Rails Guide and the layouts section of the "Action Mailer" Rails Guide are helpful backgrounders for this, if you haven't done this before.
You will see from those references that there are multiple ways to invoke a layout within Action Mailer (and more ways still, outside of a mail context), but to take one example usage, you can create a layout template file here: app/views/layouts/{your_mailer_name}.html.erb. E.g., "user_mailer.html.erb"
Its contents could look something like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>My Fancy Email</title>
</head>
<body>
<%= render :partial => 'email_header' %>
<%= yield %>
<%= render :partial => 'email_footer' %>
</body>
</html>
Note the yield call, that's where your specific email content will render.
This approach keeps your content "DRY".
Am trying to do ajax pagination without using any pagination gems for menus (which is like template). For example to display brand menu on mouse hover of the brand menu name am using "menu_brand" render because when page loads at that time only am getting brands from the database. for this rendered view I have to do pagination function. Am successfully getting the current page contents in the console log.
In controller am doing like this
def menu_brand
$cnt=Brand.count
end
def menu_brand_page
$st=0
if !(params[:startingRow].nil?)
$st = params[:startingRow].to_i
end
$brands=Brand.limit(24).offset($st)
end
In _menu_brand.html.erb
<% if !($cnt.nil?) %>
<% #pg = ($cnt / 24).ceil %>
<div class="paginator">
<%
for j in 0..#pg %>
<%= link_to "#{j+1}", "javascript:GotoPage('#{j*24}')", %>
<% end
%>
</div>
<table width="700px"; align="center" id="menupage">
<%= render "menu_brand_page"%>
</table>
<% end %>
<script type="text/javascript">
function GotoPage(startrow) {
var theURL = '/menu_brand_page/'+<%= params[:deptid] %>
+'/' + startrow;
$.ajax({
url : theURL
});
}
</script>
In menu_brand.js.erb
$('#bval').html('<%= escape_javascript render ('menu_brand') %>');
In _menu_brand_page.html.erb
<% $brands.each do |brand| %>
<%= brand.name %>
<% end %>
In menu_brand_page.js.erb
$('#menupage').html('<%= escape_javascript render ('menu_brand_page') %>');
In layout
<div id="bval">
<%= render "menu_brand" %>
</div>
<script type="text/javascript">
$(document).ready(function() {
var theBrand = '/menu_brand/' + <%= params[:deptid] %>;
$.ajax({
url : theBrand
});
});
</script>
My router.rb
match "menu_brand_page/:deptid/:startingRow" => "product_details#menu_brand_page"
match "menu_brand/:deptid" => 'product_details#menu_brand'
console log output
$('#menupage').html('\n\n \nBC\n</td>\n\n \nBC Footwear\n</td>\n\n \nBCBG\n</td>\n</tr>\n\n\n \nBCBGeneration\n</td>\n\n \nBCBGMAXAZRIA\n</td>\n\n \nBCX\n</td>\n</tr>\n\n\n \nBe Creative\n</td>\n\n \nBeach Bunny\n</td>\n\n \nBeach House\n</td>\n</tr>\n\n\n \nBeach House Woman\n</td>\n\n \nbebe\n</td>\n\n \nBecca\n</td>\n</tr>\n\n\n \nBed Stu\n</td>\n\n \nBellatrix\n</td>\n\n \nBELLE BY SIGERSON MORRISON\n</td>\n</tr>\n\n\n \nBelle Du Jour\n</td>\n\n \nBelle Noel\n</td>\n\n \nBen Amun\n</td>\n</tr>\n\n\n \nBENCH\n</td>\n\n \nBerek\n</td>\n\n \nBernardo\n</td>\n</tr>\n\n\n \nBERNIE OF NEW YORK\n</td>\n\n \nBETMAR\n</td>\n\n \nBetsey Johnson\n</td>\n</tr>\n\n');
Please help me am not getting why contents are not changing in menu_brand view.
Thank you #agncleod. #agmcleod is right. I was calling render at document ready function and I have used the global variables a lot. Now am rendering on mouse hover event and am using instance variable. I corrected this mistake. I have changed all my global ($) variables to instance (#) variable. In layout am calling render function as I mentioned below.
In Layout
brands
<div id="bval">
<%= render "menu_brand" %>
</div>
<script type="text/javascript">
$("a#menubrand").hover(function () {
var theBrand = '/menu_brand/' + <%= params[:deptid] %>;
$.ajax({
url : theBrand
});
},
function () {
});
</script>
I'm trying to generate dynamic content in my views.
in my application.html.haml
!!!
%html
%head
%title YieldUsage
= stylesheet_link_tag "application", :media => "all"
= javascript_include_tag "application"
= csrf_meta_tags
%body
= yield
= yield :head
= yield :scripts
= yield :name_section
and my helper is like;
def name_section_form
content_for :name_section do
label_tag(#post.name)
end
end
and my view;
%article
= name_section_form
But it renders;
<body>
<article>
</article>
<label for="asfd">Asfd</label>
</body>
Why is it happening? Bug or something? Any ideas?
Thanks.
Çağdaş.
Your view gets dumped into the yield. The method sets the content for a specific yield. Remove the content_for in your helper.