I'm implementing a calendar displaying a weekly schedule by hours. I need something like this:
simple_calendar is a perfect gem for making simple calendars with events,
but I can't figure out how to make it group not by days but by hours.
This gem cannot do that. But you can make it and create a pull request.
Generate the view using
rails g simple_calendar:views
Then render the week calender like this
<%= week_calendar events: #appointments do |date, appointments| %>
<span class="date-str"><%= date.day %></span>
<div class="row">
#your hours logic...
<% (1..24).each do |h|%>
#deal with you hour view and render with some identifier
<label><%= h%></label>
<% end %>
</div>
<% appointments.each do |appointment| %>
<div>
<%= appointment.name %>
</div>
<% end %>
<% end %>
Then manage the simple_calender/_week_calendar.html.erb view.
I think its little bit helpful for you.
Related
I am using simple_calendar in my Rails 5.0 application to link to events, everything works fine except that I get some extra data rendered in the calendar, I would like to get some help to figure it out.
This is what I have in the 'views/eventos/calendario.html.erb'
<h3>Eventos (<%= #eventos.count %>)</h3>
<%= month_calendar events: #eventos do |date, eventos| %>
<%= date.day %>
<% eventos.each do |evento| %>
<div>
<%= link_to evento.tipoEvento, evento %>
</div>
<% end %>
<% end %>
And this is what it is rendering for each day with programmed events:
<td class="day wday-5 past current-month has-events">
9
<div>
Entrenamiento
</div>
[#<Evento id: 4, fecha: "2016-12-09", tipoEvento:
"Entrenamiento", equipo_id: 11, comment: "Cancha 2",
created_at: "2016-12-08 06:07:19", updated_at: "2016-12-08
06:22:03", registrado: true>]
</td>
For days without events it is rendering the empty []. See, it is rendering the correct tag but I don't know how to avoid it rendering the object data, Can somebody help me please?
The solution is to replace a line in the generated view: views/simple_calendar/_month_calendar.html.erb
from:
<%= block.call day, sorted_events.fetch(day, []) %>
to:
<% block.call day, sorted_events.fetch(day, []) %>
So Jagdeep's answer was right, but in a different code line.
<% if product.created_at %>
<span class="label label-warning">New!</span>
<% end%>
Web dev newbie.. Can't figure out how to use if statement in rails view to display the span class when product records is up to 3 days old. Any ideas?
I can query in my console with
variable = Product.where(created_at: 3.days.ago..Time.now)
and get the the correct records. But cannot understand how to translate this into rails.
The ERB block is going into a _partial
You can try this:
<% if product.created_at > 3.days.ago.beginning_of_day %>
<span class="label label-warning">New!</span>
<% end%>
I am trying to get simple_calendar gem to work in my rails application. but keep getting an "undefined method `has_key?' for nil:NilClass"
Running Rails 5.0.0
Gemfile -> gem "simple_calendar", "~> 2.0"
Ran "bundle install" command, restarted application
Screenshot of error --> http://imgur.com/a/umAQH
Pastebin of controller and view page -> http://pastebin.com/8WX4dZNs
Thanks in advance!
As per the documentation, the calendar helper takes a parameter which is a Hash. In your code snippet you've passed in a collection of events (#events), not a Hash of options.
If you want to pass the events in, the documentation shows how to do it.
<%= month_calendar events: #meetings do |date, meetings| %>
<%= date %>
<% meetings.each do |meeting| %>
<div>
<%= meeting.name %>
</div>
<% end %>
<% end %>
Source: https://github.com/excid3/simple_calendar#rendering-events
So in your case, you'll want to pass your #events collection in as a key value pair like so: <%= calendar events: #events do %>.
I think you need to add the start_time alias definition inside the model you use for your events, here I have this inside my app/models/evento.rb
def start_time
self.fecha
end
Here 'fecha' is the start date for my event. Then you have to manipulate your events as Carlos Ramirez explains, so you have to have something like this inside your 'calendar.html.erb':
<h3>Eventos (<%= #eventos.count %>)</h3>
<%= month_calendar events: #eventos do |date, eventos| %>
<%= date.day %>
<% eventos.each do |evento| %>
<div>
<%= link_to evento.title, evento %>
</div>
<% end %>
<% end %>
That should render the link to your event.
Copied files from https://github.com/excid3/simple_calendar/tree/master/lib directory into my applications lib directory
edited my calendar.html.erb file
<%= month_calendar do |date| %>
<%= date %>
<% end %>
Restarted server and got a calendar displaying
Looking for ways to improve the particularly bad performance I'm getting from my rails application. Here's the code from the page in question:
notifications_controller.rb
class NotificationsController < ApplicationController
def index
#questions = Question.all.order(:updated_at => :desc)
#users = User.all
#answers = Answer.all.order(:updated_at => :desc)
end
end
and here's the corresponding view. I know it's ugly but it's working.
<div>
<% if current_user %>
<div class="notifications-added col-md-8">
<h4 class="col-md-offset-2">Approvals & Answers</h4>
<span class="text-center">
<% current_user.questions.order(id: :desc).each do |question| %>
<% if question.approved == true %>
Your question, <%= link_to "#{question.title}", question_path(question) %>, has been <span class="notifications">approved.</span><br>
<% end %>
<% question.answers.each do |answer| %>
<%= answer.user.name %> <span class="notifications">added an answer</span> to your question, <%= link_to "#{question.title}", question_path(question) %>.<br>
<% end %>
<% end %>
</span>
</div>
<div class="notifications-voted col-md-4">
<h4 class="text-center">Votes</h4><span></span>
<% current_user.answers.order(updated_at: :desc).each do |answer| %>
<% #users.each do |user| %>
<% if user.voted_up_on? answer %>
<%= user.name %> <span class="notifications">upvoted</span> your answer to <%= link_to "#{Question.find(answer.question_id).title}", question_path(answer.question_id) %>.<br>
<% elsif user.voted_down_on? answer %>
<%= user.name %> <span class="notifications">downvoted</span> your answer to <%= link_to "#{Question.find(answer.question_id).title}", question_path(answer.question_id) %>. <br>
<% end %>
<% end %>
<% end %>
</div>
<% end %>
</div>
I think I'm just sorting too much. The page is taking a long time to load. What's the low hanging fruit for improving my performance? Any thoughts? Thanks in advance.
The first , you should move logic code in your view into model.
The second , use pluck method instead array active record objects. Array string are lightweight than array of active record. Right ?
The third , use slim template engine instead erb template engine.
The fourth , cache db.
The fiveth, use google PageSpeed plugin for google chrome to analytic what's slow.
Peter answer is correct, and I will add two things :
Eager loading
Doing this
current_user.questions.each do |question|
question.answers.each do |answer|
...
end
end
will generate a query for each question. Rails will load all the questions, then for each question load its associated answers (1 query + 1 query for any question).
If you replace the first line by
current_user.questions.include(:answer).each do |question|
Rails will load all the questions, then all the associated answers (2 queries).
Look at the log
Every information for any bad performance should be visible on logs in development mode. For example, if voted_up need to load any other models than answer, your query number will be too big.
Sorry for the amateur question but I am still quite new to rails. I currently have an app that creates jobs and I would like to display the jobs beneath the date they were created in the same way they do on Dribble
At the moment to display the jobs I have te following:
<% #jobs.each do |job| %>
<div class="job-wrapper"><%= link_to user_job_path(job.user_id ,job) do %>
<div class="job">
<div class="job-desc"><strong><%= job.company %></strong> are looking for a <strong><%= job.job_title %></strong>
<div class="job-sal"><%= job.job_salary %></div>
</div>
</div>
</div>
<% end %>
<% end %>
I am sure I need to create a loop of some kind to make this work but am unsure how to incorporate it so that the date only displays once at the top of the jobs and then all jobs created during that date are shown?
Any help would be much appreciated! Thanks
Try the pattern below --
<% #job.group_by{|x| x.created_at.to_date }.each do |date,jobs_on_that_date| %>
<%= date %>
<% jobs_on_that_date.each do |job| %>
# render the job
<% end %>
<% end %>
Basically you need to group your jobs by the date (or whatever you want to group on) then get a hash keyed on the stuff you grouped on. Then render the key (date) followed by the list of objects relating to that key.