json - fullcalendar-rails: different calendars for different events places -
i use gem 'fullcalendar-rails', based on jquery fullcalendar plugin adam shaw, organize application's events.
i read useful , written tutorial fernando perales, linked in readme.md file. since use fullcalendar-rails rails 5 application, , new json, ask questions.
my event model has place attribute, place event held. goal have different calendars different places, because each place has own events.
my idea insert in events index page following form:
<%= form_tag(events_path, method: :get, id: "search-event-place") %> <%= text_field_tag :search, params[:search], placeholder: "search place" %> <%= submit_tag "search", class: "btn btn-primary" %> <% end %> <% if @events.present? %> <div id="calendar"></div> <% end %> in way users can place , events held in place, organised in fullcalendar. so, instance, if user search event in london, calendar events in london...
the index action in events controller follows:
def index if params[:search] @events = event.search(params[:search]) else @events = nil end end would work if, after defining search method in event model, defined instance variable @events used index.json.builder file @events = event.search(params[:search]) ?
search method defined in event model follows:
def self.search(search) where("place ilike ?", "#{search}") end is index page of restful events resource right place define @events? events see if go to: localhost:3000/events.json or to: localhost:3000/events?
one of main concerns this: since show action of restful events resource corresponds url /events/1 event id=1, wonder if routed correctly using each event in index.json.builder file html format generates url such /events/1.html. in fact, common rails practice, insert partial generic event in app/views/events/show.html.erb
lastly: users choose day of event jquery date picker, attribute event_date saved string format "yy-mm-dd": how can set json start , end? start , end attributes necessary or can choose day of event?
hello!
1. query
i think more suitable scope:
scope(:place, ->(search) { where('place ?', "%#{search}%") }) then can chain other queries , such.
update:
this code should added event-model. can call event.place('search-string').where(sun: true) or other way around, chainable.
2. controller
you use index action, i.e.
def index respond_to |format| format.html format.json @events = set_events end end end def set_events if params[:place].present? event.place(params[:place]) else event.all end end and if have events#index route, can access extension .json , see rendered json index.json.jbuilder.
e.g. if access /events see index.html.erb , /events.json?place=place1 render events place place1 json.
update
the format.html allows rendering html json.
3. view
this requires decisions:
- are places predefined or user input?
- how users search places?
if places predefined (or can accessed) render calendars as:
<div class="fullcalendar" data-place="<%= 'place1' %>"> </div> js:
const calendars = document.getelementsbyclass('fullcalendar'); array.from(calendars).foreach(function(calendar) { const place = calendar.dataset.place; calendar.fullcalendar({ eventsources: [{ url: '/events.json?place=' + place }], other_options:... }); }); 4. rest
i not understand concerns regarding show or mean datepicker - please elaborate!
update
i tried put in gist time. https://gist.github.com/davidwessman/7597e3eea4e71238d7f71a74928e64bd please check if out if helps!
Comments
Post a Comment