I have methods that set a title for the browser and for the page itself. On my Devise pages I would like to set these two methods but am not sure how to.
My Code:
helpers/application_helper
def title # for entire application
base_title = "Testing"
if #title.nil?
base_title
else
"#{base_title} - #{#title}"
end
end
def page_title # for page header partial
"#{#page_title}"
end
views/layouts/application
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<%= csrf_meta_tag %>
<%= favicon_link_tag %>
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>
</head>
<body>
<div id="container">
<%= render "shared/page_header" %>
<%= render "shared/flash_message" %>
<%= yield %>
</div>
</body>
</html>
views/shared/_page_header
<% unless #page_title.blank? %>
<div id="page-header">
<span><%= #page_title %></span>
</div>
<% end %>
Now I have a RegistrationsController to override the functionality whenever I need to but as it inherits to the DeviseController, I don't think it can get to the Application Helper? I also tried to put this same code in the Registration Helper but that didn't work either. What should I do?
Maybe you can use;
application_helper.rb
module ApplicationHelper
def title(page_title)
content_for(:title) { page_title }
end
end
application.html.erb
<title><%= yield :title %></title>
view
<%= title "Your page title here" %>
Related
I do not understand why it is loading the page instead of directly redirecting. It used to work I don't know what it could be.
This is my application.erb layout template:
<!DOCTYPE html>
<html>
<head>
<title>Pandora</title>
<!-- Include needed.css files for use -->
<%= stylesheet_link_tag 'application' %>
<%= stylesheet_link_tag 'utilities/bootstrap.min'%>
<%= stylesheet_link_tag 'utilities/font-awesome.min' %>
<%= stylesheet_link_tag 'elements/navbar' %>
<%= stylesheet_link_tag 'helpers' %>
<%= stylesheet_link_tag 'home' %>
<!-- Include all existing js files for use -->
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<!-- Prelang: Analytics / Google Analytics -->
<%= analytics_init if Rails.env.production? or Rails.env.development? %>
</head>
<body>
<% require_user_signed_in %>
<!-- Render the navbar -->
<%= render partial: "elements/navbar" %>
<!-- Render the sidebar -->
<%= render partial: "elements/sidebar" %>
<!-- Moves main content to the side to leave place for sidebar -->
<div class=""></div>
<!-- MAIN CONTENT Start -->
<!--main content start-->
<section id="main-content">
<section class="wrapper">
<div class="row">
<div class="col-lg-9 main-chart">
<!-- Renderes the popup notifications -->
<% if flash[:notice] %>
<div class="notice"><%= flash[:notice] %></div>
<% end %> <br>
<!-- Renders everything in the page file -->
<div class="main-move-up">
<%= yield %>
</div>
</div><!-- /col-lg-9 END SECTION MIDDLE -->
<div class="col-lg-3 ds">
<%= render partial: "elements/notificationbar" %>
</div><!--/col-lg-3 ds -->
</section>
</section>
<!--main content end-->
</section>
<!-- Renders the footer -->
<%= render partial: "elements/footer" %>
<!-- js placed at the end of the document so the pages load faster -->
<%= javascript_include_tag('dropdowns.js') %>
</body>
</html>
And I have this in my application controller:
def require_user_signed_in
unless user_signed_in?
# If the user came from a page, we can send them back. Otherwise, send
# them to the root path.
# if request.env['HTTP_REFERER']
# fallback_redirect = :back
if defined?(root_path)
fallback_redirect = root_path
else
fallback_redirect = "/"
end
redirect_to fallback_redirect, flash: {error: "You must be signed in to view this page."}
end
end
My problem: I am getting the error undefined method 'id' for nil:NilClass from <%#companies=Company.where(:user_id => current_user.id)%> (located in home.erb); this is normal since current_user doesn't exists when loading the page and not logged in. However what is not normal is that it is loading <%= yield %> which loads home.erb. It should load the method <% require_user_signed_in %> (located at the top of the template) first shoudn't it? And therfore redirect before it loads the rest of the page.
Please let me know if you would like me to post more of my files from the project.
You might be interested in Controller Filters. From the RoR Guides:
class ApplicationController < ActionController::Base
before_action :require_login
private
def require_login
unless logged_in?
flash[:error] = "You must be logged in to access this section"
redirect_to new_login_url
end
end
end
I'm having problems with this Gem, when using content for.
In my application.html.erb, I have the following:
<head>
<% if current_page?(root_path) %>
<meta name="description" content="My page">
<% else %>
<%= yield :description_meta %>
<% end %>
</head>
And then, in my views, I do the following:
<% content_for :description_meta do %>
<% set_meta_tags :description => "My description" %>
<% end %>
It wont paint any metas, and if I render it with <%=, it will show in the body of the page as normal text.
Any ideas? Thanks
Read MetaTags Usage.
Seems like set_meta_tags just configure meta tags and display_meta_tags render them.
I can't seem to figure out how to turn a Post title on the blog app I am building into the page title. Below is the helper I am using and the line of code in the application layout. I would like to know what I need to do in order to have each post title also be the page title. Thank you in advance for any help you can provide.
Application helper
module ApplicationHelper
def full_title(page_title)
base_title = "Business Name"
if page_title.empty?
base_title
else
"#{base_title} | #{page_title}"
end
end
end
Application Layout
<title><%= full_title(yield(:title)) %></title>
show.html.erb
<h1><%= #post.title %></h1>
<p>
<%= #post.body.html_safe %>
</p>
<%= link_to 'Back to Posts', posts_path %>
in your posts/show.html.erb view
<% content_for :title, #post.title %>
As commented By #jbmyid,use content_for like this
def full_title(page_title)
base_title = "Business Name"
if page_title.empty?
content_for :title, base_title.to_s
else
"#{base_title} | #{page_title}"
end
use it in your application layout as like:
<title><%= yield(:title) %></title>
then call it in your templates:
<% full_title(#post.title %>
In your application.html.erb
<head>
<%= yield :title %>
</head>
In your view
<% content_for :title do %>
<title><%=full_title(#post.title)%></title>
<% end %>
I have a controller (not model related) which searches for stuff on YouTube via API. What I'm trying to achieve is simple enough...if someone clicks the search button when no string was entered then a flash notice will appear saying 'please enter search string'.
The 'main' view is as follows:
<%= form_tag({:controller => 'main', :action => 'index'}, :method => 'get') do %>
<%= label_tag(:text, 'Search:') %>
<%= text_field_tag(:text) %>
<%= submit_tag('Search') %>
<% end %>
<div id="flash">
<% if flash.now[:notice] %>
<p id="notice"><%= flash.now[:notice] %></p>
<% end %>
</div>
My index action in my 'main' controller looks like this:
def index
if params[:text].blank?
flash.now[:notice] = 'Please enter a search string'
render 'index'
else
#do searches on YouTube
end
I've looked at other question similar to this on stackoverflow as well and tried a few thing and still doesn't work. I've played around with using flash[] versus flash.now[] and still the notice message just sticks even when I first load the page. I might be missing something quite trivial here but I've spent a while figuring it out and still stuck. Anyway help would be appreciated.
UPDATE:
Ok, I have found an answer. I simply added the following in my form in the view:
<%= hidden_field_tag :searching, true %>
then i changed my controller to:
flash.now[:notice] = 'Please enter a search string' if params[:searching]
This worked fine. This is courtesy of the answer here:
How do I stop a flash error message from showing on page load until after button is clicked in ruby on rails?
FOLLOW UP QUESTION: in the link above, it suggested a cleaner answer which is to use:
params[:search].blank? && !params[:search].nil?
However, this doesn't work for me. I get the error:
undefined method `gsub' for nil:NilClass
I'm a newbie in Ruby so is this a syntactical error?
This may help, worth a try. In my case, my flash message was not shown because I was using
flash.now[:notice]
When changed to
flash[:notice]
started showing it.
Full code, controller:
def create
user_data = user_params
user_data[:password] = SecureRandom.uuid
#user = User.new(user_data)
#user.save
flash[:notice] = 'An email was sent for the user to confirm their account.'
respond_with(#user)
end
aaplication.html.erb
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><%= full_title(yield(:title)) %></title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
<%= render 'layouts/shim' %>
</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
<% flash.each do |message_type, message| %>
<div id=<%= message_type %> class="alert"><%= message %></div>
<% end %>
<%= yield %>
<%= render 'layouts/footer' %>
</div>
</body>
</html>
I have a Messages controller, with actions like: Sent messages,Received messages,Send new message. They are displayed to the user in a toolbar form. The thing is, when I render each view, I have to manually render the toolbar as well. So, here's how the code for the views looks like:
sent_messages.html.erb
<%= render "shared/toolbar" %>
# render stuff for sent messages
received_messages.html.erb
<%= render "shared/toolbar" %>
# render stuff for received messages
new.html.erb
<%= render "shared/toolbar" %>
# render stuff for new message
The views don't look very DRY. Is there a way I could specify that I want the toolbar to render before everything else, in the Messages controller?
app/views/layouts/application.html.erb
<html>
<head>
<title></title>
</head>
<body>
<div id="content"><%= content_for?(:content) ? yield(:content) : yield %></div>
</body>
</html>
app/views/layouts/messages.html.erb
<% content_for :content do %>
<%= render "shared/toolbar" %>
<%= yield %>
<% end %>
<%= render file: "layouts/application" %>
From the documentation:
Suppose you have the following ApplicationController layout:
app/views/layouts/application.html.erb
<html>
<head>
<title><%= #page_title or "Page Title" %></title>
<%= stylesheet_link_tag "layout" %>
<style><%= yield :stylesheets %></style>
</head>
<body>
<div id="top_menu">Top menu items here</div>
<div id="menu">Menu items here</div>
<div id="content"><%= content_for?(:content) ? yield(:content) : yield %></div>
</body>
</html>
On pages generated by NewsController, you want to hide the top menu and add a right menu:
app/views/layouts/news.html.erb
<% content_for :stylesheets do %>
#top_menu {display: none}
#right_menu {float: right; background-color: yellow; color: black}
<% end %>
<% content_for :content do %>
<div id="right_menu">Right menu items here</div>
<%= content_for?(:news_content) ? yield(:news_content) : yield %>
<% end %>
<%= render template: "layouts/application" %>
That's it. The News views will use the new layout, hiding the top menu and adding a new right menu inside the "content" div.