Display fields within control when not null - silverstripe

I have the following code:
<% control StaffMembers %>
<li>
Name: <h2>$Name</h2>
</li>
<% end_control %>
I would like to ask how I would be possible to show the Name text field only if it is not null or empty.
An if statement within the control didn't work:
<% control StaffMembers %>
<% if $Name %>
<li>
Name: <h2>$Name</h2>
</li>
<% end_if %>
<% end_control %>
Thank you

Your code looks correct. Calling an if statement inside the control should work.
I would also recommend putting an if statement around your control to check if there are any StaffMembers before looping through them:
<% if $StaffMembers %>
<ul>
<% control $StaffMembers %>
<% if $Name %>
<li>
Name: <h2>$Name</h2>
</li>
<% end_if %>
<% end_control %>
</ul>
<% end_if %>
Or in Silverstripe 3 using loop instead of control:
<% if $StaffMembers %>
<ul>
<% loop $StaffMembers %>
<% if $Name %>
<li>
Name: <h2>$Name</h2>
</li>
<% end_if %>
<% end_loop %>
</ul>
<% end_if %>

Related

How to only show blog posts that have a category assigned to them when using the Silverstripe blog module?

I am using the silverstripe blog module: https://github.com/silverstripe/silverstripe-blog
I have blog posts looping out like so:
Blog.ss
<% loop $BlogPosts %>
<div class="row mtb20">
<div class="col-md-8">
<div class="blog-holder-image" style="background-image: url($FeaturedImage.Fill(700,340).URL);"></div>
</div>
<h2>$Title</h2>
<div>
<% if $Summary %>
$Summary
<% else %>
<p>$Excerpt</p>
<% end_if %>
</div>
<div>
<a class="call-to-action-link" href="$Link">Read more</a>
</div>
</div>
</div>
<% end_loop %>
At the top of the blog posts, I have a list of categories that you can click on which goes to that category:
<% if $Categories %>
<% loop $Categories %>
<a class="category-btn" href="$Link">$Title</a>
<% end_loop %>
<% end_if %>
However the blog posts are not being filtered out, its still showing all the blog posts instead of just the category that has been selected e.g "Design"
If I use the default $PaginatedList method that comes with it it works fine:
<% if $PaginatedList.Exists %>
<% loop $PaginatedList %>
<% include PostSummary %>
<% end_loop %>
<% else %>
<p><%t Blog.NoPosts 'There are no posts' %></p>
<% end_if %>
How do I get it to work my way though?
I guess you found the solution yourself... Looking at the code, $BlogPosts gets all child pages - unfiltered. $PaginatedList grabs prefiltered posts by category when you're in the category action in Blog_Controller. The simplest solution would be just to use $PaginatedList, that's how it's supposed to use.
But you might get the $CurrentCategory from the controller and loop over its $BlogPosts relation like
<% loop $CurrentCategory.BlogPosts %>
...
<% end_loop %>

How to divide column in 2 when having a Dynamic rendered list

I have an each that renders all of the categories in one list.
The thing is, I want to have 2 columns, instead of a single column with all of th categories.
How can I say "there should be 5 categories in each column" when using an each?
<ul class="menu">
<% #categories.each do |category| %>
<%= link_to spots_by_category_path(category.id) do %>
<li><%= category.name %></li>
<% end %>
<% end %>
</ul>
Use in_groups_of, e.g:
<% #categories.in_groups_of(5, false).each do |grouped_categories| %>
<ul class="menu">
<% grouped_categories.each do |category| %>
<%= link_to spots_by_category_path(category.id) do %>
<li><%= category.name %></li>
<% end %>
<% end %>
</ul>
<% end %>
You'll need to add some stylish here to have these list next to each other.

google-maps-api-3 ruby on rails

I am getting the following error
ReferenceError: Can't find variable: google
when I am rendering a page the first time which should display the google maps. The first part of the page is shown correctly, but at the bottom the entire div with the map is missing. After click on refresh the page is rendered correctly. This error is the same in dev and prod and shows up for page in the application containing a google map. Don't know how to fix it.
Here are more details about the error from the Safari's console:
TypeError: 'undefined' is not a constructor (evaluating 'new google.maps.OverlayView()')
TypeError: 'undefined' is not a constructor (evaluating 'new google.maps.OverlayView')
Below is the code of one of this pages generating the errors above.
<% provide(:title, 'Current TBMs Location and Statistics') %>
<% if signed_in? %>
<h3>TBMs location and statistics - Today's date: <%=#current_date%></h3>
<div>
<div class="pull-left">
<%= link_to "Daily Progress", progresstable_path, class: "btn btn-sm btn-primary" %>
<%= link_to "Daily Stats", tbms_stats_table_path, class: "btn btn-sm btn-primary" %>
</div>
<div class="pull-right">
<%= "Today's: " %>
<%= link_to "Schedule", schedules_path, class: "btn btn-sm btn-primary" %>
<%= link_to "Readings List", all_readings_path, class: "btn btn-sm btn-primary" %>
</div>
</div>
</br></br>
<div class="container">
<div class="pull-left">
<h5>
<%= "TBM ID: " %>
<%= #tbms_progress_W.tbm_id %>
<%= "aka TBM1 or 26900"%>
</h5>
<%= "Last Date: " %>
<%= #tbms_progress_W.daily_date %>
<%= "Max Station: " %>
<%= number_with_precision #tbm_w_location, precision: 2 %>
<%= "Status: " %>
<%= #tbms_progress_W.tbm_status %>
<%= "Since: " %>
<%= #tbm_w_days_since %>
</br>
<%= "Days total: " %>
<%= #tbm_w_days_since_commissioned %>
<%= "Days working: "%>
<%= #tbm_w_working_days %>
<%= "Days stopped: " %>
<%= #tbm_w_stopped_days %>
<%= "Located between: " %>
<% if #smp_w_after.present? %>
<%= #smp_w_after.smp_id %>
<% else %>
<%= "Unknown" %>
<% end %>
<%= " and: " %>
<% if #smp_w_before.present? %>
<%= #smp_w_before.smp_id %>
<% else %>
<%= "Unknown" %>
<% end %>
</br>
<%= "Total excavated: " %>
<% if #tbm_w_location.present? %>
<%= #tbm_w_location - #tbms_W_initial.max_station %> <%="m"%>
<% else %>
<%= "Unknown" %>
<% end %>
<%= "Excavation speed: " %>
<% if #tbm_w_location.present? %>
<%= number_with_precision (#tbm_w_location - #tbms_W_initial.max_station) / #tbm_w_working_days, precision: 2 %> <%="m/day"%>
<% else %>
<%= "Unknown" %>
<% end %>
<%= "Excavated since yesterday cut off time: " %>
<% if #tbm_w_location.present? %>
<%= #tbm_w_location - #tbms_progress_W2.max_station %> <%="m"%>
<% else %>
<%= "Unknown" %>
<% end %>
</br>
<%= "Average distance excavated in the previous 24 hours: " %>
<% if #tbm_w_location.present? %>
<%= #tbms_progress_W2.max_station - #tbms_progress_W1.max_station %> <%="m"%>
<% else %>
<%= "Unknown" %>
<% end %>
<% if #tbm_w_lat == nil or #tbm_w_lon == nil %>
</br>
<%= "TBM latitude and longitude cannot be calculated, missing near SMP northing and easting" %>
</br>
<%= "TBM approximate address: not available" %>
<% else %>
</br>
<%= "Latitude: " %>
<%= number_with_precision #tbm_w_lat, precision: 6 %>
<%= " Longitude: " %>
<%= number_with_precision #tbm_w_lon, precision: 6 %>
<%= "Address: " %>
<% if #tbm_w_result.empty? %>
<%= "Cannot calculate TBM W address, Geocoder unavailable" %>
<% else %>
<%= #tbm_w_result[0].address %>
<% end %>
<% end %>
<% if #current_date != #tbms_progress_W.daily_date %>
<h6 class="error-message">
<%= "Today's location for TBM W is not loaded" %>
<%= " Last TBM W recorded date was: " %>
<%=#tbms_progress_W.daily_date %>
</h6>
<% end %>
<h6> SMPs ahead of the current TBM W location: </h6>
<% if #tbm_w_location.present? and #smps_w_ahead.present? %>
<ol>
<li>
<%= "First line: " %>
<%= #smps_w_ahead[0].smp_id %> <%= #smps_w_ahead[0].final_inst_status %> <%= #smps_w_ahead[1].smp_id %> <%= #smps_w_ahead[1].final_inst_status %>
</li>
<li>
<%= "Second line: " %>
<%= #smps_w_ahead[2].smp_id %> <%= #smps_w_ahead[2].final_inst_status %> <%= #smps_w_ahead[3].smp_id %> <%= #smps_w_ahead[3].final_inst_status %>
</li>
<li>
<%= "Third line: " %>
<%= #smps_w_ahead[4].smp_id %> <%= #smps_w_ahead[4].final_inst_status %> <%= #smps_w_ahead[5].smp_id %> <%= #smps_w_ahead[5].final_inst_status %>
</li>
<li>
<%= "Fourth line: " %>
<%= #smps_w_ahead[6].smp_id %> <%= #smps_w_ahead[6].final_inst_status %> <%= #smps_w_ahead[7].smp_id %> <%= #smps_w_ahead[7].final_inst_status %>
</li>
</ol>
<% else %>
<%= "Unknown" %>
<% end %>
<h5>
<%= "TBM ID: " %>
<%= #tbms_progress_E.tbm_id %>
<%= "aka TBM2 or 27000"%>
</h5>
<%= "Last Date: " %>
<%= #tbms_progress_E.daily_date %>
<%= "Max Station: " %>
<%= number_with_precision #tbm_e_location, precision: 2 %>
<%= "Status: " %>
<%= #tbms_progress_E.tbm_status %>
<%= "Since: " %>
<%= #tbm_e_days_since %>
</br>
<%= "Days total: " %>
<%= #tbm_e_days_since_commissioned %>
<%= "Days working: "%>
<%= #tbm_e_working_days %>
<%= "Days stopped: " %>
<%= #tbm_e_stopped_days %>
<%= "Located between: " %>
<% if #smp_e_after.present? %>
<%= #smp_e_after.smp_id %>
<% else %>
<%= "Unknown" %>
<% end %>
<%= " and: " %>
<% if #smp_e_before.present? %>
<%= #smp_e_before.smp_id %>
<% else %>
<%= "Unknown" %>
<% end %>
</br>
<%= "Total excavated: " %>
<% if #tbm_e_location.present? %>
<%= #tbm_e_location - #tbms_E_initial.max_station %> <%="m"%>
<% else %>
<%= "Unknown" %>
<% end %>
<%= "Excavation speed: " %>
<% if #tbm_e_location.present? %>
<%= number_with_precision (#tbm_e_location - #tbms_E_initial.max_station) / #tbm_e_working_days, precision: 2 %> <%="m/day"%>
<% else %>
<%= "Unknown" %>
<% end %>
<%= "Excavated since yesterday cut off time: " %>
<% if #tbm_e_location.present? %>
<%= #tbm_e_location - #tbms_progress_E2.max_station %> <%="m"%>
<% else %>
<%= "Unknown" %>
<% end %>
</br>
<%= "Average distance excavated in the previous 24 hours: " %>
<% if #tbm_e_location.present? %>
<%= #tbms_progress_E2.max_station - #tbms_progress_E1.max_station %> <%="m"%>
<% else %>
<%= "Unknown" %>
<% end %>
<% if #tbm_e_lat == nil or #tbm_e_lon == nil %>
</br>
<%= "TBM latitude and longitude cannot be calculated, missing near SMP northing and easting" %>
</br>
<%= "TBM approximate address: not available" %>
<% else %>
</br>
<%= "Latitude: " %>
<%= number_with_precision #tbm_e_lat, precision: 6 %>
<%= " Longitude: " %>
<%= number_with_precision #tbm_e_lon, precision: 6 %>
<%= "Address: " %>
<% if #tbm_e_result.empty? %>
<%= "Cannot calculate TBM E address, Geocoder unavailable" %>
<% else %>
<%= #tbm_e_result[0].address %>
<% end %>
<% end %>
<% if #current_date != #tbms_progress_E.daily_date %>
<h6 class="error-message">
<%= "Today's location for TBM E is not loaded" %>
<%= " Last TBM E recorded date was: " %>
<%=#tbms_progress_E.daily_date %>
</h6>
<% end %>
<h6> SMPs ahead of the current TBM E location: </h6>
<% if #tbm_e_location.present? and #smps_e_ahead.present? %>
<ol>
<li>
<%= "First line: " %>
<%= #smps_e_ahead[0].smp_id %> <%= #smps_e_ahead[0].final_inst_status %> <%= #smps_e_ahead[1].smp_id %> <%= #smps_e_ahead[1].final_inst_status %>
</li>
<li>
<%= "Second line: " %>
<%= #smps_e_ahead[2].smp_id %> <%= #smps_e_ahead[2].final_inst_status %> <%= #smps_e_ahead[3].smp_id %> <%= #smps_e_ahead[3].final_inst_status %>
</li>
<li>
<%= "Third line: " %>
<%= #smps_e_ahead[4].smp_id %> <%= #smps_e_ahead[4].final_inst_status %> <%= #smps_e_ahead[5].smp_id %> <%= #smps_e_ahead[5].final_inst_status %>
</li>
<li>
<%= "Fourth line: " %>
<%= #smps_e_ahead[6].smp_id %> <%= #smps_e_ahead[6].final_inst_status %> <%= #smps_e_ahead[7].smp_id %> <%= #smps_e_ahead[7].final_inst_status %>
</li>
</ol>
<% else %>
<%= "Unknown" %>
<% end %>
</div>
</div>
<div style='width: 800px;'>
<div id="map" style='width: 920px; height: 500px;'></div>
</div>
<% else %>
<%= render 'instruments/unsigned' %>
<% end %>
<script src="//maps.google.com/maps/api/js?v=3.13&sensor=false&libraries=geometry" type="text/javascript"></script>
<script src='//google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.14/src/markerclusterer_packed.js' type='text/javascript'></script>
<script src='//google-maps-utility-library-v3.googlecode.com/svn/trunk/richmarker/src/richmarker-compiled.js' type='text/javascript'></script>
<script src='//google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.9/src/infobox_packed.js' type='text/javascript'></script>
<script type="text/javascript">
buildMap (<%=raw #hash.to_json %>);
</script>
Problem solved: put all links refs to css and js in application.html.erb, after turbolinks and good by errors. Seems that browsers need to cache all of this before page x in the app is due for render.

Rails & Bootstrap Make button inline with text field

I'm working on a front end of web app. I would like to know how to make the button and textfield in line. Another question is how to make the error message displayed just below the textfield?
Picture:
https://www.dropbox.com/s/vsdy3730flraes3/Screen%20Shot%202013-12-01%20at%204.47.20%20PM.png
Here is the code:
<div class="input-group" align="center" >
<%= form_tag(location_search_path,method: "get" ) do %>
<%= text_field_tag('location', params[:location], :size => 150, :placeholder=> "Enter city or zip code") %>
<%= button_tag(type: "submit", class: "btn btn-success ") do %>
Search
<i class="icon-search"></i>
<% end %>
<% end %>
</div>
<% [:notice, :error, :alert].each do |level| %>
<% unless flash[level].blank? %>
<div data-alert="alert" class="alert-message <%= flash_class(level) %> fade in" align="center">
<%= content_tag :p, flash[level] %>
</div>
<% end %>
<% end %>
Add the <div class="form-inline"> to your form element to get the button and field onto the same line, and I guess you could us a <p> to make the message appear below it, though I would probably set up the input group in a row with columns to center it rather than the align tag you are using, and then put the messages into a new <row> below it in the same column.
<div class="input-group" align="center" >
<div class="form-inline">
<%= form_tag(location_search_path,method: "get" ) do %>
<%= text_field_tag('location', params[:location], :size => 150, :placeholder=> "Enter city or zip code") %>
<%= button_tag(type: "submit", class: "btn btn-success ") do %>
Search
<i class="icon-search"></i>
<% end %>
<% end %>
</div>
</div>
<p>
<% [:notice, :error, :alert].each do |level| %>
<% unless flash[level].blank? %>
<div data-alert="alert" class="alert-message <%= flash_class(level) %> fade in" align="center">
<%= content_tag :p, flash[level] %>
</div>
<% end %>
<% end %>
bootstrap 3 has input groups... which are nice. http://getbootstrap.com/components/#input-groups
Try this in your code...
<%= form_tag location_search_path, method: 'get' do %>
<%= text_field_tag :location, params[:location], size: 150, placeholder: "Enter city or zip code" %>
<span class='input-group-btn'>
<%= submit_tag 'Search', class: 'btn btn-success' %>
</span>
<% end %>

How to organize my views with posts on different columns and lines

I am implementing a website using Ruby on rails, and bootstrap.
I have several post (small text with picture) for an user for example and I want to print them on the screen.
I use will_paginate to make them appear on different pages. But my problem is when I want to organize them on the page, for the moment i use so my page looks like :
User : post
User : post
User : post
...
But I would like to have :
User : post User : post User : post
User : post User : post User : post
User : post User : post User : post
....
EDIT
My actual views :
_feed.html.erb
<% if #feed_items.any? %>
<ol class="microposts">
<%= render partial: 'shared/feed_item', collection: #feed_items %>
</ol>
<%= will_paginate #feed_items %>
<% end %>
_feed_item.html.erb
<li id="<%= feed_item.id %>">
<%= link_to gravatar_for(feed_item.user), feed_item.user %>
<span class="user">
<%= link_to feed_item.user.name, feed_item.user %>
</span>
<span class="photo">
<% if feed_item.image? %>
<%= image_tag feed_item.image.url %>
<% end %>
</span>
<span class="timestamp">
Posted <%= time_ago_in_words(feed_item.created_at) %> ago.
</span>
<% if current_user?(feed_item.user) %>
<%= link_to "delete", feed_item, method: :delete,
data: { confirm: "You sure?" }%>
<% end %>
</li>
Final edit : result
<% if #feed_items.any? %>
<% #feed_items.each_slice(3) do |slice| %>
<% slice.each do |photo| %>
<ol class="microposts">
<%= render partial: 'shared/feed_item', collection: #feed_items %>
</ol>
<%end%>
<% end %>
<%= will_paginate #feed_items %>
<% end %>
I ran into this just a little while ago actually. The way I ended up doing it was using the each_slice method. It's really handy! It would look something like:
<% if #feed_items.any? %>
<% #feed_items.each_slice(3) do |feed_items_slice| %>
<ol class="microposts">
<%= render partial: 'shared/feed_item', collection: feed_items_slice %>
</ol>
<% end %>
<%= will_paginate #feed_items %>
<% end %>
Obviously the styles will have to change. I would suggest floating the li tags to the left, and then making the ol tag clear:both to make sure it gets put on a new line.

Resources