Rails Simple Calendar Gem Error - ruby-on-rails

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

Related

Strange output from rails each do

Rails each do method is acting strangely and I do not know why.
controller
def index
#fabric_guides = FabricGuide.with_attached_image.all.order(:name)
end
index.html.erb
<div class="guide-items">
<%= #fabric_guides.each do |fabric| %>
<div class="guide-container">
<%= link_to fabric_guide_path(slug: fabric.slug) do %>
<%= image_tag fabric.image if fabric.image.attached? %>
<% end %>
<div class="guide-info">
<p class="g-name">
<%= link_to fabric.name,
fabric_guide_path(slug: fabric.slug) %>
</p>
</div>
</div>
<% end %>
</div>
I have two FabricGuide records so I expect two "guide-container" but I get three. Or more precisely I get two guide containers and a third block of text containing all the content from the last FabricGuide record.
I have almost an identical setup for articles and have never encountered this problem. I'd happily share more information if needed. Thank you!
Please remove = equal sign from your each loop of view code
like below :-
<% #fabric_guides.each do |fabric| %>
...
...
<% end %>
you have used this <%= #fabric_guides.each do |fabric| %> in your view that's why it shows all record in DOM.
The expression for erb tags is <% %>
now if we want to print that tag too then we apply <%= %>

Scope of each on rails template

I'm new to rails and I'm trying to build a view that will list the parents and related children
Ex:
Passport has many Visas
I want to list information about the passport and the visas that the passport has.
So I have
<% #passport_list.each do |passport| %>
# passportFields
<% passport.visas.each do |visa| %>
<%= t.text_field :visa_type %>
<% end %>
<% end %>
I'm getting the error
undefined method `visa_type' for #Passport:0x000000091b8b28
It looks like rails is trying to find the property visa_type for passport, instead of in visa. How does the scope work within each? Can I force it to access visa_type from visa?
I think you're looking for the fields_for form helper. This will allow you to create fields for the relevant visa attributes. Replace your code sample with the following, and you should be all set.
<% #passport_list.each do |passport| %>
# passportFields
<% t.fields_for :visas do |visa_fields| %>
<%= visa_fields.text_field :visa_type %>
<% end %>
<% end %>
You can also iterate over the list as follows:
<% #passport_list.each do |passport| %>
# passportFields
<% passport.visas.each do |visa| %>
<% t.fields_for :visas do |visa_fields| %>
<%= visa_fields.text_field :visa_type %>
<% end %>
<% end %>
<% end %>
For more information on fields_for, check out the link I added above, and to customize further for your use case, check out the "One-to-many" section.
IMO you should always handle the null case of an object.
Something like this if you use rails (present? is a Rails function)...
<% if #passport_list.present? %>
<% #passport_list.each do |passport| %>
passportFields
<% passport.visas.each do |visa| %>
<%= t.text_field :visa_type %>
<%end%>
<%end%>
<% else %>
<p>Nothing to see here</p>
<% end %>
However if your #passport_list is backed by an ActiveRecord Query, you can handle this in the model/helper/controller by returning the .none query on the model. Note that this differs from an empty array because it is an ActiveRecord Scope, so you can chain AR queries onto it
# scope on AR model
def self.awesomeville
where(country_of_origin: "awesomeville")
end
# method queried in controller
#passport_list = Passport.all
if #passport_list.present?
#passport_list
else
Passport.none
end
# additional filtering in view is now possible without fear of NoMethodError
#passport_list.awesomeville
Whereas a ruby Array would raise an error as it would respond to the Array methods.

How can post parameters month and day in calendar date select tag?

I installed calendar_date_select gem and I'm trying to post format params in calendar_date_select tag with only month and day.
Calendar Date select script works perfect but when I select a date it shows this format "Year-Month-day" so what I want is do click this will show only the month and day inside calendar_date_select_tag is that possible? or maybe another way to remove the year in log like this?
Here is the log:
Processing TestController#sending (for 127.0.0.1 at 2014-08-22 17:17:06) [POST]
Parameters: {"d1"=>"01-01", "d2"=>"31-01", "authenticity_token"=>"yeah=", "commit"=>"Search"}
I don't want to remove the year each time to make this working because actually is working fine when remove the year and keep the month and day.
Here is the controller
def sending
#clients= Client.find_by_mysql(["select * from clients where date_format(happy_birthday,'%m-%d') BETWEEN ? AND ?",params[:d1],params[:d2] ])
end
Here is the view
<head>
<% CalendarDateSelect.format=(:hyphen_ampm )%>
<% calendar_date_select_style "silver" %>
<% translation_calendar %>
</head>
<% form_tag :controller=>"test",:action=>"sending" do %>
From: <%= calendar_date_select_tag "d1",params[:d1] %>
TO: <%= calendar_date_select_tag "d2",params[:d2] %>
<%= submit_tag "Search", :class=>"button" %>
<% end %>
Here is the screenshot
I tried this but I got undefined method `strftime' for "01-01":String
<%= calendar_date_select_tag "d1",params[:d1].strftime("%m-%d")%>
Please somebody can help me or maybe advice?
Thanks in advance.
I saw your question and that's so easy, try this:
In your controller:
def sending
##### THIS IS IF YOU HAVE PROBLEMS WITH NIL PARAMS IN CALENDAR TAG
if params[:d1].nil?
#d1 = params[:d1]
else
if params[:d1].present?
#d1 = Date.parse(params[:d1]).strftime("%m-%d")
end
end
if params[:d2].nil?
#d2 = params[:d2]
else
if params[:d2].present?
#d2 = Date.parse(params[:d2]).strftime("%m-%d")
end
end
################
#clients= Client.find(:all,:conditions=>['date_format(happy_birthday,"%m-%d") BETWEEN ? AND ?',#d1,#d2] )
end
In your view:
<% form_tag :controller=>"test",:action=>"sending" do %>
From: <%= calendar_date_select_tag "d1",#d1 %>
TO: <%= calendar_date_select_tag "d2",#d2 %>
<%= submit_tag "Search", :class=>"button" %>
<% end %>
Hope this tips can help.

Combining two associations in my "each do" loop method

I'm a little new to back-end programming...I'm currently running the following code in my rails 4 app to show a basic list of all the admins on a project (if there are any)...
<% if project.projectadmins.any? %>
<div class="row-fluid">
<% project.projectadmins.each do |user| %>
<div class="collaborator">
<%= link_to user do %>
<%= image_tag user.image_url(:thumb).to_s, :class => "profile-pic-thumb" %>
<% end %>
</div>
<% end %>
</div>
<% end %>
However, I also have projectcollaborators for each project, so I'd like to know what the most effective way would be to combine those and provide a list of both projectadmins AND projectcollaborators (with project admins being listed first if there are any...other than that ordering is not important).
I assume the if statement at the beginning would change to...
<% if project.projectadmins.any? || project.projectcollaborators.any? %>
but I'm not 100% sure and am lost on the rest...any help is much appreciated.
You could create a scope, for example project_admins_and_collaborators, which gets all the needed records and then use it in your loop.
You can also do this in following way
project_admins_and_collaborators = project.projectadmins
project_admins_and_collaborators << project.projectcollaborators
project_admins_and_collaborators.flatten.uniq do |user|
#your code
end

Listing objects under the date they were created

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.

Resources