<%= render :layout => "some_layout" do %>
<p>Some stuff</p>
<% end %>
In "some_layout"
<%= yield %>
Is there a way to detect if the yield block has any content? Example:
<% if block_has_content? %>
<%= yield %>
<% else %>
<p>Default content</p>
<% end %>
I think you should have a look to this documentation:
http://apidock.com/rails/ActionView/Helpers/CaptureHelper/content_for
You can define somewhere:
<% content_for :some_layout do %>
some content
<% end %>
And in an other part of your code:
<%= content_for(:some_layout) || 'Default content' %>
If content for :some_layout is not defined, it will take the default one.
Related
Here I have a requirement to use yield in a partial other than application.html.erb , when I try this in partial it was showing me the blank value and if I try the yield in application.html.erb it was showing the value.
application.html.erb
body>
<div id="main-container">
<%= content_for?(:page_header) ? yield(:page_header) : (render :partial => "/shared/home_header") %>
<div id="middle-container">
<%= yield %>
</div>
<div id="bottom-container">
<%= render "/shared/bottom_partial" %>
</div>
index.html.erb
<% content_for :page_header do %>
<%= render :partial => "/shared/employer_header" %>
<% end %>
<% content_for :page_name do %>
<p>My Account</p>
<% end %>
<div>
xxxxxxxxxxxxxx
xxxxxxxxxxxxxx
</div>
/shared/_employer_header.html.erb
<div class="heading" >
<%= yield(:page_name) %>
</div>
When I am trying like above I am getting blank value in yield :page_name in employer header if I try the yield :page name in application.html.erb I am getting the value.Can any one help me to sort this out.Thank'U'.
Swap the order of your two content_fors so the :page_name is defined before the partial is rendered.
index.html.erb
<% content_for :page_name do %>
<p>My Account</p>
<% end %>
<% content_for :page_header do %>
<%= render :partial => "/shared/employer_header" %>
<% end %>
I am a beginner working on a RoR app that allows users to update their resume with experiences and education. I have two models for these different items and would like to display them together in chronological order. I want to know how to set this up in my profile controller and view.
I'd like to adopt this same practice to a separate part of the app that will combine post and discussion items from users in the same way. However, currently, my focus is on the experience and education portion.
profiles/show.html.erb:
<% if #experiences.any? or #educations.any? %>
<div class="postExpOuter">
<% #experiences.each do |experience| %>
<div class="postExp">
<div>
<h2><%= experience.position %></h2>
<h3><%= experience.company %> | <%= experience.location %></h3>
<h4>
<%= experience.start_month %> <%= experience.start_year %>
<% if experience.end_year %>
<%= " - " + experience.end_month %> <%= experience.end_year %>
<% else %>
<span> - Present</span>
<% end %>
</h4>
</div>
</div>
<% end %>
<% #educations.each do |education| %>
<div class="postExp">
<div>
<h2><%= education.degree %></h2>
<h3><%= education.school %></h3>
<h4>
<% if education.end_year %>
<span>Class of </span><%= education.end_year %>
<% else %>
<%= education.start_year %><span> - Present</span>
<% end %>
</h4>
</div>
</div>
<% end %>
</div>
<% end %>
just hacked in my browser, don't know if it works directly:
<%- (#experiences.to_a + #educations.to_a).sort_by(&:end_year).each do |item| -%>
<%- if item.is_a? Experience -%>
your markup here...
<%- else -%>
your other markup here...
<%- end -%>
<%- end -%>
haven't written ERB since... uhh, long. basically it's just the following: merge the 2 ActiveRecord-Relations as Arrays into one big Array, sort them by the timestamp you want (you may add .reverse at the end if you want). While iterating through the list, check the type of object you have.
Hope this helps.
profiles_controller.rb:
class ProfilesController < ApplicationController
def show
#user = User.find_by_profile_name(params[:id])
if #user
#posts = #user.posts.all(:order => "created_at DESC", :limit => 3)
#experiences = #user.experiences.all(:order => "start_year DESC")
#educations = #user.educations.all(:order => "start_year DESC")
#items = (#experiences.to_a + #educations.to_a).sort_by(&:start_year).reverse[0,3]
render action: :show
else
render file: 'public/404', status: 404, formats: [:html]
end
end
end
profiles/show.html.erb:
<% if #items.any? %>
<div class="postExpOuter">
<% #items.each do |item| %>
<% if item.is_a? Experience %>
<div class="postExp">
<div>
<h2><%= item.position %></h2>
<h3><%= item.company %> | <%= item.location %></h3>
<h4>
<%= item.start_month %> <%= item.start_year %>
<% if item.end_year %>
<%= " - " + item.end_month %> <%= item.end_year %>
<% else %>
<span> - Present</span>
<% end %>
</h4>
</div>
</div>
<%- else -%>
<div class="postExp">
<div>
<h2><%= item.degree %></h2>
<h3><%= item.school %></h3>
<h4>
<% if item.end_year %>
<span>Class of </span><%= item.end_year %>
<% else %>
<%= item.start_year %><span> - Present</span>
<% end %>
</h4>
</div>
</div>
<% end %>
<% end %>
</div>
<% end %>
The following index code works fine:
<ul id="sortable1" class="connectedSortable" data-update-url="<%= sort_tasks_url %>">
<% current_user.tasks.each do |task| %>
<%= content_tag_for :li, task do %>
<%= task.taskstatus.statuscode %>
<%= " - " %>
<%= link_to h(task.taskname), task %>
<%= " - " %>
<%= task.taskdesc %>
<% end %>
<% end %>
But, I can't figure out how to add a class to each li line. I get a syntax error with this:
<%= content_tag_for(:li, :class => 'ui-state-highlight'), task do %>
Thanks for the help!
Figured it out:
<%= content_tag_for( :li, task, :class => "ui-state-highlight") do %>
I have a rails3 form that allows the user to edit a list of answers, as part of an assessment.
I use a fields_for loop to generate each text input:
app/models/assessment.rb :
class Assessment < ActiveRecord::Base
serialize :answers, Hash # answers is a t.text field used to store all answers.
end
app/view/assessments/new.html.erb :
<p>Initialized answers: <%= #assessment.answers %></p>
<% item_counter = 0 %>
<% form.fields_for :answers do |answer_fields| %>
<% item_id = "item" + item_counter.to_s %>
<% item_counter = item_counter + 1 %>
<div class="field">
<%= answer_fields.label "the appropriate question, omitted for brevity" %>
<br/>
<% #assessment.answers[item_id] = "" %>
<%= answer_fields.text_field item_id, :value => #assessment.answers[item_id] %>
</div>
<% end %>
PROBLEM: The fields_for loop does zero iteration, no field gets printed.
( despite "Initialized answers:" showing correctly: {"a"=>143, "b"=>42} )
This should do. Tested locally.
<p>Initialized answers: <%= #assessment.answers %></p>
<% #assessment.answers.each do |key, value| %>
<%= form.fields_for :answers, #assessment.answers[key] do |answer_fields| %>
<div class="field">
<%= answer_fields.label key %>
<br/>
<%= answer_fields.text_field key, :value => value %>
</div>
<% end %>
<% end %>
Turns Hash to OpenStruct object solved my problem.
<% form.fields_for :answers, OpenStruct.new(answers) do |answer_fields| %>
<% item_id = "item" + item_counter.to_s %>
<% item_counter = item_counter + 1 %>
<div class="field">
<%= answer_fields.label "the appropriate question, omitted for brevity" %>
<br/>
<% #assessment.answers[item_id] = "" %>
<%= answer_fields.text_field item_id, :value => #assessment.answers[item_id] %>
</div>
Am trying to display a 'total comments' number (i.e.7) count beside the article on the index page and not on the article page. Would like to use a ruby method for this as its probably the most straight forward...?
views/articles/_article.html.erb
<div class="article_header">
<b>Title: </b> <%= truncate(article.title, :length => 50) %>
by <%= article.user.username %> on <%= article.created_at.strftime("%d %B, %Y") %>
<b>Detail:</b> <%= truncate(article.body, :length => 225) %>
</div>
<br />
<%= blog.comments.count %>
<%= link_to 'Read', article %>
<% if can? :update, article %>
| <%= link_to 'Edit', edit_article_path(article) %> |
<% end %>
Pass in a variable when you call your partial:
= render "article", :display_count => true
Then in your partial:
<% display_count ||= false %>
<%= display_count ? blog.comments.count : '' %>
The correct way to do this would be:
<td><%= link_to "Comment count = #{article.comments.count}", article_path(article) %>
This will simply add another column to the output on your index page. It doesn't have to be linked if you simply want to display the count:
<td><%= "Comment count = #{article.comments.count}" %>