Providing default content for section in layout(rails 3 .erb) - ruby-on-rails

Is it possible to provide default content for section in layout if it is not overridden in view?

if you are using, say a title tag in your head section of your application.html.erb layout file, you could do something as simple as <%= #title || "Some Default App Content" %>
Using this same approach, you can use it for content_for and other helper methods.

Now, Rails have content_for? method for it: http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-content_for

Related

How to prevent Rails from rendering links when using partials in Mailer

I would like to reuse my partials in mailer layouts, but I want to avoid rendering the hyperlinks and instead print out just their name, like using link_to_if / link_to_unless methods.
Which condition could I check to prevent the rendering all the links inside mailer layouts?
Inside your email view, you can use something like this :
<%= controller.class.name %>
The printed controller name will be a mailer : "ApplicationMailer", ...
You can then Regex or use a string helper ...

Setting content_for misunderstanding

I'm using this tutorial to set bootstrap modal content in forms, but I want to set the title...
I think I don't understand how to set the title content_for method...I understand it for templating, but if I want to send a title to the modal, is it best to make a helper for that? Is there a better Rails 4 way to do this?
Please advise, and remember, I'm not a CE--I've looked at the rails doc and google searched, so please don't demean my question by posting a google link. Thanks!
If you're going to be rendering the title of the modal dynamically, it's typically better to use a helper method. This will keep your view logic DRY and readable. This method will accept an argument for the title and if none is present it will render a default title.
In application_helper.rb:
def modal_title(title)
if title.empty?
"My Default Title"
else
title
end
end
In your modal partial where the title should appear:
<%= modal_title(yield(:title)) %>
In the view, you will have access to dynamically set the title using content_for. For example, you could pass a string as your title or pass an instance variable from your controller.
<% content_for :title, #your_variable %>

Make specific Rails view show certain helpers

How do you make a specific view show a different helper.
module ApplicationHelper
def about_title
%Q{Everything you need!}.html_safe
end
def contact_title
%Q{Contact us!}.html_safe
end
end
I have a partial header that renders in every view but I want to show a different header title without having to edit the views directly (or edit a variable? in the view to show what I want?)
<h1 class="title"><%= about_title %></h1>
What I'm thinking is making an if/else in the helper but I am new to rubyonrails and I'm not sure how is this done.
You need to set the title somewhere. There's a few ways you can handle this. You could use the controller + action name, and set up the titles in i18n.
en:
titles:
home_index: "Welcome!"
session_new: "Login"
Then in your layout:
<title><%= t("titles.#{controller.controller_name}_#{controller.action_name}") %></title>
Another way to do it is using content_for & yield but that means putting code in your views :)

print line in rails only for a specific page

I am new in rails and have the following issue:
I have a view that renders from the layout across several pages, it is in fact a part of the header. I want to remove a specific line from the header and hide it only for one page. I have tried to add a condition in the view to check if the page rendered comes from a specific controller/action but it doesn't work. what is the best practice for this case?
thanks
Just keep a instance variable in controller action and set is as true. Use this variable to with conditions to include/exclude specific code.
controller action
#include = true
view
if #include.present?
//place the line which should be excluded
end
I've asked a very similar question, and you are allready on a good way:
Use a condition like
<% if current_page?(view_path)==false %>
<your line>foobar</your line>
<%end%>
In the view you can access the controller from params[:controller] so you could have
<% if params[:controller] == foo %>
<p>conditionally shown</p>
<% end %>
Its not always best practise to use params directly inside the view though...

How to vary a helper called within a Rails 3 layout, depending on the controller

I'm using the same layout for several controllers, and inside this layout I include a menu using a call to a helper, like this:
<%= side_menu %>
What I'd like to do is vary the contents of side_menu depending on the controller that's invoking the layout. In an ideal world, I could define side_menu in application_controller.rb and in other helper files and then the appropriate helper would be selected depending on the controller; in other words, something like this:
# application_helper.rb
def side_menu
"generic menu This goes here"
end
# users_helper.rb
def side_menu
"menu for users goes here"
end
# guests_helper.rb
def side_menu
"menu for guests goes here"
end
This doesn't work because in Rails 3 all helper files are loaded and I have no control over which side_menu will actually be called. It would be great if there were an option to load only application_helper.rb and the controller-specific helper, but there's not one (yet).
What's the best way to vary the content of a helper depending on the controller? I'm currently defining side_menu once in application_helper.rb and then checking to controller to see what to add. This feels wrong, since the problem nearly screams for a subclass-and-override answer -- which I can't do due to the "helper :all" behavior of Rails 3. Suggestions?
You can define this method in controller and add:
helper_method :side_menu
But maybe different solution would be better. I think that you can add _side_menu.html.erb in each controllers view folder and when you call <%= render :partial => 'side_menu' %> it should look for different files depending on current controller (however rememeber to add this file for all controllers).
Or you can mix these two methods. Add this helper method to controller and inside it render right file. This way it is better, because you get some default side menu and it won't crash when there is no side menu partial for a controller.
You can also in layout add <%= yield :side_menu %> and if you want to put something in side menu, just add <% content_for :side_menu do %> bla bla bla <% end %>.

Resources