I am creating a calendar in Rails using simple_calendar.
enter image description here
However, the date of the calendar appears in the image as 2018-08-23.
I would like to display the date on the calendar day by day like 23 instead of 2018-08-23.
_month_calendar.html.erb
<div class="simple-calendar">
<div class="calendar-heading">
<%= link_to t('simple_calendar.previous', default: 'Previous'), calendar.url_for_previous_view %>
<span class="calendar-title"><%= t('date.month_names')[start_date.month] %> <%= start_date.year %></span>
<%= link_to t('simple_calendar.next', default: 'Next'), calendar.url_for_next_view %>
</div>
<table class="table table-striped">
<thead>
<tr>
<% date_range.slice(0, 7).each do |day| %>
<th><%= t('date.abbr_day_names')[day.wday] %></th>
<% end %>
</tr>
</thead>
<tbody>
<% date_range.each_slice(7) do |week| %>
<tr>
<% week.each do |day| %>
<%= content_tag :td, class: calendar.td_classes_for(day) do %>
<% if defined?(Haml) && respond_to?(:block_is_haml?) && block_is_haml?(block) %>
<% capture_haml(day, sorted_events.fetch(day, []), &block) %>
<% else %>
<% block.call day, sorted_events.fetch(day, []) %>
<% end %>
<% end %>
<% end %>
</tr>
<% end %>
</tbody>
</table>
</div>
According to the documentation, you have to call day on the date object.
I assume that in your case, the date object is the one named day, so you have to change 2 lines in your view (those inside if/else statement):
<% if defined?(Haml) && respond_to?(:block_is_haml?) && block_is_haml?(block) %>
<% capture_haml(day.day, sorted_events.fetch(day, []), &block) %>
<% else %>
<% block.call day.day, sorted_events.fetch(day, []) %>
<% end %>
Related
I want to show two columns of the code in table data tag"" per row while using '.each' method. But the issue is that the following code displays one column in a row.
<table>
<% #lease.apartment.roommates.each do |roommate| %>
<tr>
<td colspan="5">
<% unless roommate == #lease.second_occupant || roommate == #lease.user %>
<% if roommate.current_room.present? %>
<p>
<%= roommate.full_name %> -
<% if roommate.current_room.apartment == #lease.apartment%>
<%= roommate.current_room&.label %>
<% end %>
<br>Email:<%= roommate.email %><br>Phone:<%= roommate.phone %><br>
<% if #lease.end_at.present? %>
Lease End date (if applicable):<%= #lease.end_at %>
<% end %>
</p>
<% end %>
<% end %>
</td>
</tr>
<% end %>
</table>
You can use the each_slice method for dividing the array into slices of 2 elements each:
<table>
<% #roommates.each_slice(2) do |roommate_slice| %>
<tr>
<td colspan="5">
<%= roommate_slice[0].full_name %>
<%= roommate_slice[1].full_name %>
</td>
</tr>
<% end %>
</table>
As you can see below, I am having a problem of getting extra spaces in my search result. As you see in the photo, between the numbers and the elements, I am getting spaces and I do not why?
<td>
<% saved_element = ""%>
<% sensor.base_material.elests.each_with_index do |elest, v| %>
<% if elest.element.include? "O" %>
<% saved_element = elest %>
<% else %>
<%= elest.element.split('-').last %>
<% if elest.stoich != 1 %>
<sub><%= elest.stoich.to_i %></sub>
<% end %>
<% end %>
<% end %>
<% if saved_element.present? %>
<%= saved_element.element.split('-').last%>
<% if saved_element.stoich != 1 %>
<sub><%= saved_element.stoich.to_i %></sub>
<% end %>
<% end %>
</td>
My controller passes this array to the view:
#ipd = Ipdw.connection.select_all("select * from [My Preferences]")
This is my view code:
<h1>Hello, Rails</h1>
<body>
<% #ipd.each do |ip| %>
<p><%= ip %></p>
<% end %>
</body>
This is displayed in the browser:
{"Pref ID"=>1, "Source ID"=>1, "Username"=>"RS2559", "Field Name"=>"CATEGORY", "String Value"=>"IP Placemat Deliverables", "Integer Value"=>nil, "Decimal Value"=>nil, "Created Dt Tm"=>2009-08-10 03:01:36 UTC, "Update Dt Tm"=>2009-12-14 16:04:01 UTC}
{"Pref ID"=>2, "Source ID"=>1, "Username"=>"RS2559", "Field Name"=>"TYPE", "String Value"=>nil, "Integer Value"=>nil, "Decimal Value"=>nil, "Created Dt Tm"=>2009-08-10 03:01:40 UTC, "Update Dt Tm"=>2009-12-14 16:04:01 UTC}
...
I want to show the "Username" and "Field Name" in a table. How can I loop through the array to show only these columns in a readable matter?
It looks like your query is returning an array of hashes. Just use the keys to pick the values you are interested in:
<% #ipd.each do |ip| %>
<p>
<%= ip['Username'] %><br />
<%= ip['Field Name'] %>
</p>
<% end %>
How about something like:
<h1>Hello, Rails</h1>
<body>
<% #ipd.each do |ip| %>
<p>
<% ["Username", "Field Name"].each do |k| %>
<%= ip[k] %>
<% end %>
</p>
<% end %>
</body>
That, naturally, will just put the values on your page. You could do other things like build a table or make bullet points or whatever.
That might look something like:
<h1>Hello, Rails</h1>
<body>
<table>
<tr>
<% #field_names.each do |field_name| %>
<th>
<%= field_name %>
</th>
<% end %>
</tr>
<% #ipd.each do |ip| %>
<tr>
<% #field_names.each do |field_name| %>
<td>
<%= ip[field_name] %>
</td>
<% end %>
</tr>
<% end %>
</table>
</body>
This assumes in your controller you did something like:
#field_names = ["Username", "Field Name"]
That way, you can decide in your controller which fields you want (and in what order) and not have to futz with your html.erb file.
If you wanted to pretty up those label names a little, in your controller you could do:
#label_mapping = {"Username"=>"User Name", "Field Name"=>"Field Name"}
And then:
<h1>Hello, Rails</h1>
<body>
<table>
<tr>
<% #field_names.each do |field_name| %>
<th>
<%= label_mapping[field_name] %>
</th>
<% end %>
</tr>
<% #ipd.each do |ip| %>
<tr>
<% #field_names.each do |field_name| %>
<td>
<%= ip[field_name] %>
</td>
<% end %>
</tr>
<% end %>
</table>
</body>
There are maybe 2 ways:
First you have a model for this kind of data:
<% #ipd.each do |ip| %>
<p><%= ip.username %></p>
<% end %>
Secound:
Your Dataset you get from your external Database looks like a hash. So try something like this:
<% #ipd.each do |ip| %>
<p><%= ip["username"] %></p>
<% end %>
I think kind 2 is what you need ;).
In a view i wouldn't use a shorter way of looping, be cause the code get less readable with something like this: abc.each{|letter| "<li>#{letter}</li>"}
For some reason I am getting duplicates of show.html.erb. The duplicates happen to coincide with what the .count is, even if I remove .count from the show file.
users/show.html.erb
<% if #user.habits.any? %>
<h2>Habit Challenges (<%= #user.habits.count %>)</h2>
<%= render #habits %>
<% end %>
<% if #user.valuations.any? %>
<h2>Values (<%= #user.valuations.count %>)</h2>
<%= render #valuations %>
<% end %>
<% if #user.goals.any? %>
<h2>Current Goals (<%= #user.goals.unaccomplished.count %>)</h2>
<%= render #unaccomplished_goals %>
<% end %>
<% if #user.goals.any? %>
<h2>Accomplished Goals (<%= #user.goals.accomplished.count %>)</h2>
<%= render #accomplished_goals %>
<% end %>
<% if #user.quantifieds.any? %>
<h2>Stats: Monthly Average (<%= #user.quantifieds.count %>)</h2>
<%= render #averaged_quantifieds %>
<% end %>
<% if #user.quantifieds.any? %>
<h2>Stats: Instances (<%= #user.quantifieds.count %>)</h2>
<%= render #instance_quantifieds %>
<% end %>
users_controller.rb
def show
#user = User.find(params[:id])
#habits = #user.habits.all
#valuations = #user.valuations.all
#accomplished_goals = #user.goals.accomplished.all
#unaccomplished_goals = #user.goals.unaccomplished.all
#averaged_quantifieds = #user.quantifieds.averaged.all
#instance_quantifieds = #user.quantifieds.instance.all
end
As you can see by the picture if I create 3 habits, 3 tables of habits will show. If I create 4, 5, so on the same thing happens. The same goes for the rest, like with values (#valuations) 12 repetitions are being created.
habits/_habit.html.erb
<!-- Default bootstrap panel contents -->
<div id="valuations" class="panel panel-default">
<div class="panel-heading"><h4><b>HABITS</b></h4></div>
<!-- Table -->
<table>
<thead>
<tr>
<th>Level</th>
<th>Left</th>
<th>Strike</th>
<th>Trigger</th>
<th>Action</th>
<th>Target</th>
<th>Reward</th>
<th>Days</th>
</tr>
</thead>
<tbody>
<% #habits.each do |challenged| %>
<tr>
<td><%= challenged.current_level %></td>
<td>
<%= link_to edit_habit_path(challenged) do %>
<%= [params[:missed]].flatten.length %>
<% end %></td>
<td><%= challenged.trigger %></td>
<td class="category">
<b><%= raw challenged.tag_list.map { |t| link_to t.titleize, tag_path(t) }.join(', ') %></b>
</td>
<td><%= challenged.target %></td>
<td class= "committed">
<%= challenged.committed.map { |d| d.titleize[0,3] }.join ', ' %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
Do you have any ideas? I'm baffled.
You can try this:
<h2>Habit Challenges (<%= #habits.count %>)</h2>
<%= render #habits %>
<h2>Values (<%= #valuations.count %>)</h2>
<%= render #valuations %>
<h2>Current Goals (<%= #unaccomplished_goals.count %>)</h2>
<%= render #unaccomplished_goals %>
<h2>Accomplished Goals (<%= #accomplished_goals.count %>)</h2>
<%= render #accomplished_goals %>
<h2>Stats: Monthly Average (<%= #averaged_quantifieds.count %>)</h2>
<%= render #averaged_quantifieds %>
<h2>Stats: Instances (<%= #instance_quantifieds.count %>)</h2>
<%= render #instance_quantifieds %>
Change your show action:
def show
#user = User.find(params[:id])
#habits = #user.habits
#valuations = #user.valuations
#accomplished_goals = #user.goals.accomplished
#unaccomplished_goals = #user.goals.unaccomplished
#averaged_quantifieds = #user.quantifieds.averaged
#instance_quantifieds = #user.quantifieds.instance
end
Change your partial, stop iterating over #habits again:
<tbody>
<tr>
<td><%= habit.current_level %></td>
<td><%= link_to edit_habit_path(habit) do %>
<%= [params[:missed]].flatten.length %><% end %>
</td>
<td><%= habit.trigger %></td>
<td class="category"><b><%= raw habit.tag_list.map { |t| link_to t.titleize, tag_path(t) }.join(', ') %></b></td>
<td><%= habit.target %></td>
<td class= "committed"><%= habit.committed.map { |d| d.titleize[0,3] }.join ', ' %></td>
</tr>
</tbody>
Update:
Another way around, which will only repeat tbody (as per your request).
<h2>Habit Challenges (<%= #habits.count %>)</h2>
<%= render partial: 'habit', locals: {habits: #habits} %>
Then change your habit partial:
<% habits.each do |habit| %>
<tbody>
<tr>
...
...
</tr>
</tbody>
<% end %>
That's the normal behavour if you render a collection of objects. A partial for each object in the collection is rendered.
Look at section 3.4.5 of the guides.rubyonrails.org on layouts and rendering...
http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials
The habit partial should probably be edited to show only what you want to show per element in the collection. If it is currently showing only the information for the first element, then you may not be passing the object correctly to the partial. See section 3.4.6 in the above document.
Are they part of the same controller? If so, call the partial without the variable...
<% if #user.habits.any? %>
<h2>Habit Challenges (<%= #user.habits.count %>)</h2>
<%= render 'habits' %>
<% end %>
If not, call the partial with a local variable:
<% if #user.habits.any? %>
<h2>Habit Challenges (<%= #user.habits.count %>)</h2>
<%= render 'habits', :locals => {:habits => #habits} %>
<% end %>
If that's the case, you have to change the partial to stop using # based variable for #habits.
I wonder if/how to change the name of the param :keyword when using acts as taggable?
Today my url looks like this:
http://www.foo.bar/tagged?keyword=baz
I would like to change the "keyword" to another word.
controller
def tagged
#current_keyword = params[:keyword]
#tags = FeedEntry.tag_counts_on(:keyword)
#tagged_feed_entries = FeedEntry.tagged_with(params[:keyword]).order("published_at desc").paginate :page => params[:sida]
end
View:
<table class="table table-striped">
<tbody>
<% if #tags %>
<% #tagged_feed_entries.each do |feed_entry| %>
<tr>
<td>
<% today = Date.today %>
<% if (today === feed_entry.published_at.to_date) %>
<span class="label label-success">
<% else %>
<span class="label">
<% end %>
<%= format_stamp_to_date(feed_entry.published_at) %>
kl:
<%= I18n.localize(feed_entry.published_at, :format => '%H:%M') %>
</span>
</td>
<td><%= link_to feed_entry.name, feed_entry_path(feed_entry) %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
<%= will_paginate #tagged_feed_entries, :param_name => :sida %>
You should just be able to replace all instances of params[:keyword] to params[:whatever]. Your path then becomes http://www.foo.bar/tagged?whatever=baz.
If you have a search form, you'll have to make the relevant changes there, as well.