Last active
August 13, 2017 20:44
-
-
Save davidwessman/7597e3eea4e71238d7f71a74928e64bd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| json.events(@events) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <% 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 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
Thank you very much. I wonder: will I still need
app/views/events/index.json.builderas suggested by Fernando Perales in his tutorial? If yes, I suppose I have to define@eventsin the index action as@events = Event.all.Also, if
app/views/events/index.json.builderis no more necessary, where can I instruct fullcalendar about events urls (as html)?Last question. My event model does not have an
event_urlattribute, but I want every event have as urlevents/id. Wouldfeed.json.jbuilderwork if instead I used the following content?