I’ve got a Fullcalendar working in Rails. I’ve also added extra fields to the events table and a modal to get user input. When a record is saved, it automatically saves the user_id of the current user in the event table.
I would like to have the calendar display with only the current_user’s events.
Right now the calendar.js.coffee file has this for getting the events data:
eventSources: [{
url: '/events',
}],
How can I fix it so only the current_user’s event data shows up?
Do I put some code in the events controller? Or something in the coffee code?
Should I add some selection code in the json part of the controller?
class EventsController < ApplicationController
# GET /events
# GET /events.json
def index
@events = Event.scoped
@events = Event.between(params['start'], params['end']) if (params['start'] && params['end'])
respond_to do |format|
format.html # index.html.erb
format.json { render :json => @events }
end
end
The following doesn’t seem to work:
format.json { render :json => @events, :user_id => current_user.id }
Thanks!!!
You are on the right way 😉 Use a scope (I named it
for_user, I can post the code about this scope if needed) in the Event model to retrieve the events of a particular user.Now you need to pass a
params[:user_id]to the index action, which could becurrent_user.idin your case 😉WARNING: Security issue
Doing this will let you be able to put any user_id in the URL and see the data of other users. Add a verification on the index action to ensure you can only access to the current_user’s data: