-1

How can I have a link_to the day when an event starts through a rails link_to?

On one page I have the calendar and on another page I have a table with a list of events like:

 - @events.each do |event|
  = event.id
  = link_to "Today", calendar_path(fullcalendar.gotoDate(event.starts_at))`

(the link_to is not working, of course, but kind of describes the need. How can I make it work?)

I want to click on the event on one page and it should open another calendar HTML page and take you to the right date in fullCalendar

The basic calendar code in application.js:

function eventCalendar() {
  return $('#event_calendar').fullCalendar({ 
    });
};
Yshmarov
  • 3,450
  • 1
  • 22
  • 41
  • You can't. Not like your example. Because `link_to` and `calendar_path` are both server-side ruby/rails and `fullcalendar.gotoDate` is client-side javascript. – jvillian Aug 04 '19 at 14:54

1 Answers1

0

If you want to make the calendar to go the day on which the event starts, when the event is clicked, then you can do this with entirely client-side code, since the start date is a property of the event in fullCalendar. I can't see why you would need any ruby code for that.

You can give fullCalendar a callback to be run when the event is clicked, and it can read the clicked event's start date, and run the goToDate method:

$('#event_calendar').fullCalendar({ 
  //...your other calendar options, and then
  eventClick: function(event) {
    $("#event_calendar").fullCalendar("gotoDate", event.start);
  });
});

See https://fullcalendar.io/docs/v3/eventClick and https://fullcalendar.io/docs/v3/gotoDate for relevant documentation.

Note that the above is using fullCalendar v3 syntax. In the sample code in your question you appeared to be mixing v3 (e.g. $('#event_calendar').fullCalendar) and v4 (e.g. fullcalendar.gotoDate syntax, which will not work. It looked like the latter was probably psuedocode, so I've assumed you're using v3. If not, it's not difficult to amend the code for v4.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Looks like you didn't get it so I've clarified my question. I have a list of events on another page in an html table that does not have the calendar. I want the url link_to a `datetime` field to bring me to the `gotoDate` in fullcalendar – Yshmarov Feb 21 '20 at 13:14
  • So you mean, if you click on an event in fullCalendar it should take you to the HTML page for that event? Or you mean if you click on the event on your other page it should open the calendar HTML page and take you to the right date in fullCalendar? Your description is not clear enough for me to know which way round you want it to work. (Not sure what "bring me to the gotodate" actually means, btw, it makes no sense to be "brought" to a method call.) – ADyson Feb 21 '20 at 13:24
  • click on the event on your other page it should open the calendar HTML page and take you to the right date in fullCalendar – Yshmarov Feb 21 '20 at 13:49
  • 1
    Ok. So you can't do what you've attempted and try to run a fullCalendar method in your other page - because fullCalendar doesn't exist in that page. The approach needs to be: the link on your other page should set a querystring parameter in the URL which links to the calendar page, whose value is the start date of the event in YYYY-MM-DD format, e.g. `https://www.example.com/calendar.html?date=2019-12-01` When the calendar page loads, some JavaScript needs to read that date value from the querystring and set it as the `defaultDate` option in the calendar setup code – ADyson Feb 21 '20 at 13:57