2

I have a question I am building a calendar using fullcalendar-rails, and I have a small problem. I am using the eventsources function (url parameter), so the calendar knows where to retrieve the events to display. But my events resources are nested under my users resources. Therefore to access the events i need to follow this route -> 'users/:id/events'. So my question is how do i go about transfering these required parameters to the eventsources :url function ? Also, I just want to mention that i am using json so the calendar maybe able to display the events. Inside my json array I have a url parameter that contains the event's url is there a way for me to transfer that json.url ?

I apologize that I have not displayed any of my codes yet but I am currently at work now and i do not have access to it. As sooon as get access to a pc ill reedit it and display my code. Its been bugging me for so long so I thought that Id just ask first.

Thanks in advance.

calendar.js.coffee

$(document).ready ->
  $('#calendar').fullCalendar
   editable: true,
   header:
   left: 'prev,next today',
   center: 'title',
   right: 'month,agendaWeek,agendaDay'
   defaultView: 'month',
   height: 500,
   slotMinutes: 30,

eventSources: url: "/events.json" <----This is the part that where the calendar reads the events from but i need it to be 'users/:id/events.json'

timeFormat: 'H(:mm) ',
dragOpacity: "0.5",

displayEventEnd: 
    month: true,
    basicWeek: true

eventDrop: (event, dayDelta, minuteDelta, allDay, revertFunc) ->
  updateEvent(event);

eventResize: (event, dayDelta, minuteDelta, revertFunc) ->
  updateEvent(event);


updateEvent = (the_event) ->
 $.update "/events/" + the_event.id,
   event:
     title: the_event.title,
     starts_at: "" + the_event.start,
     ends_at: "" + the_event.end,
     description: the_event.description

routes.rb

Rails.application.routes.draw do
  get 'staticpages/welcome'
  get 'staticpages/about'
  get 'staticpages/contact'
  get 'calendar/index'

  root 'staticpages#welcome'

  devise_for :users

  resources :users, :only => [:show] do
    member do
      resources :events, :as => :user_events
    end
  end

index.json.jbuilber

json.array!(@events) do |event|
json.extract! event, :id, :title, :description
json.start event.starts_at
json.end event.ends_at
json.allday event.all_day
json.url event_url(event, format: :html)
end
del woo
  • 21
  • 3
  • Do you want to show all the events or ones particular to the current_user? Can anyone access every event? – Miknash Dec 08 '14 at 14:07
  • no i want to show the events of only the user that is currently logged in. – del woo Dec 08 '14 at 15:22
  • okay, so you can use current_user.id in you url, right? – Miknash Dec 08 '14 at 15:22
  • can you try <%= url_to_event(current_user.id, event.id) %> – Miknash Dec 08 '14 at 15:46
  • hey I have added my code now, as you can see in the (calendar.js) i am trying to send the url with the correct :id parameter into the javascript. But the url that the eventsources takes is a string. So, i was wondering if it would be possible to send my json.url to the evenSources from calendar because it actually contains a string of the url with the correct :id parameter. If not was what is the correct way of doing it? Thanks. – del woo Dec 08 '14 at 19:59
  • Well I think you can call user in the url with <%= current_user.id %> at the urk, so in the end you can have : /users/<%= current_user.id %>/events.json – Miknash Dec 10 '14 at 18:46
  • where in the calendar.js.coffee file? I cant use erb in that file.. – del woo Dec 10 '14 at 19:17
  • eventSources: url: "/events.json" --> here. For using instance variables in coffee: http://stackoverflow.com/questions/8108511/how-to-access-instance-variables-in-coffeescript-engine-inside-a-slim-template – Miknash Dec 10 '14 at 19:18
  • del woo, I'm facing the same problem at the moment. How did u solve it? – Sean Magyar Dec 16 '15 at 20:08

1 Answers1

0

I figured it out.

Add a gem called 'Gon'.

In your Users controllers, add a variable to get the URL, something like:

@users_path = "users/#{current_user}/events.json"

You may change current_user for the user.id

Then, add the following:

gon.user_path = @users_path

In your FullCalendar JavaScript. Do the following:

$(document).on('turbolinks:load', function() {
    var user_path = gon.user_path;

    $('#calendar').fullCalendar({
        events: user_path,
    });
});

And that's it. It will render as you wanted.

Pang
  • 9,564
  • 146
  • 81
  • 122
Fred Setra
  • 26
  • 5