Seeking to make the '2' in the url field of the getEvents function dynamic by replacing it with the current user's id.
I was thinking of using ${current_user.id}
. Can someone guide me on how to achieve this? Ideally, I'm aiming for something like:
function getEvents() {
$.ajax({
url: 'http://localhost:3000/users/`${current_user.id}`/events',
method: 'get',
dataType: 'json'
}).done(function(data)
I have a current_user method in my ApplicationController file. I attempted applying json but encountered errors. Here is the complete code:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
helper_method :current_user, :logged_in?, :log_user_in #let these methods be used in views
private
def log_user_in
session[:user_id] = @user.id
end
def logged_in?
!!current_user
end
def current_user #should return previous user or look for current user
@current_user ||= User.find_by(id: session[:user_id]) if session[:user_id]
end
$(function() {
console.log('event.js is loaded...')
listenForClick()
});
function listenForClick() {
$('button#events-data').on('click', function(event) {
event.preventDefault()
getEvents()
})
function getEvents() {
$.ajax({
url: 'http://localhost:3000/users/2/events',
method: 'get',
dataType: 'json'
}).done(function(data) {
console.log("the data is: ", data)
debugger
let myEvent = new Event(data[0])
let myEventHtml = myEvent.eventHTML()
document.getElementById('ajax-events').innerHTML += myEventHTML
})
}
class Event {
constructor(obj) {
this.id = obj.id
this.name = obj.name
this.host = obj.host
this.description = obj.description
// this.host.user.id = obj.user.id
}
}
Event.prototype.newEventForm = function() {
(`
<strong>New Event</strong>
<form>
<input id='event-name' type='text' name='name'></input><br>
<input type='text' name='description'></input><br>
<input type='submit'/>
</form>
`)
};