Skip to content

Instantly share code, notes, and snippets.

@davidwessman
Last active August 13, 2017 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidwessman/7597e3eea4e71238d7f71a74928e64bd to your computer and use it in GitHub Desktop.
Save davidwessman/7597e3eea4e71238d7f71a74928e64bd to your computer and use it in GitHub Desktop.
const calendar = {
initialize() {
const calendars = document.getElementsByClass('fullcalendar');
Array.from(calendars).forEach(function(cal) {
const place = cal.dataset.place;
cal.html(''); // Remove any previous calendar
cal.fullCalendar({
eventSources: [{ url: '/events/feed.json?place=' + place }],
other_options:...
});
});
},
};
document.addEventListener('turbolinks:load', function(cal) {
calendar.initialize();
});
class EventsController
def feed
@events = set_events
end
def index
@place = params.fetch(:place, nil)
end
def set_events
if params[:place].present?
Event.place(params[:place])
else
Event.all
end
end
end
<% form_with(url: events_path) do |form| %>
<%= form.text_field('place') %>
<%= form.submit %>
<% end %>
<% if @place.present? %>
<div class='fullcalendar' data-place="<%= @place %>"></div>
<% end %>
@knzudgt
Copy link

knzudgt commented Jul 30, 2017

Thank you very much. I wonder: will I still need app/views/events/index.json.builder as suggested by Fernando Perales in his tutorial? If yes, I suppose I have to define @events in the index action as @events = Event.all.

Also, if app/views/events/index.json.builder is no more necessary, where can I instruct fullcalendar about events urls (as html)?

Last question. My event model does not have an event_url attribute, but I want every event have as url events/id. Would feed.json.jbuilder work if instead I used the following content?

json.array!(@events) do |event|
  json.extract! event, :id, :title, :description, :place
  json.start event.start_time
  json.end event.end_time
  json.url event_url(event, format: :html)
end

@davidwessman
Copy link
Author

@knzudgt I did not see this reply before.

If you add the feed.json.jbuilder you will not need the index.json.jbuilder.

I think the event_url looks great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment