I found this mini-application on GIThub that allows users to create attend and cancel events here: http://ift.tt/1Xfqfz2 the only issue is that it was made for Rails 3. I'm trying to implement that app into my own practice application but using rails 4.2.4 so far i'm having issues with the attend events functionality.
I have three models that are connected using a rich join using has many through: user, event, and event_registration.
user.rb
class User < ActiveRecord::Base
has_many :event_registrations
has_many :events, through: :event_registrations
.
event.rb
class Event < ActiveRecord::Base
has_many :event_registrations
has_many :users, through: :event_registrations
.
event_registration.rb
class EventRegistration < ActiveRecord::Base
belongs_to :event
belongs_to :user
The functionality that creates a new event is located within event_registrations_controller.rb
class EventRegistrationsController < ApplicationController
def new
EventRegistration.new(event_id: params[:event_id].to_i, user_id:
current_user.id)
redirect_to event_path(params[:event_id])
flash[:notice] = "Thanks for attending this event!"
end
def destroy
EventRegistration.where(event_id: params[:event_id].to_i, user_id:
current_user.id).first.destroy
redirect_to event_path(params[:event_id])
end
private
# Use callbacks to share common setup or constraints between actions.
def set_event_registration
@event_registration = EventRegistration.find(params[:id])
end
def event_registration_params
params.require(:event_registration).permit(:user_id, :event_id)
end
end
This is a snippet of my events\show.html.erb that creates the event.
<% if session[:user_id] %>
<% if @event.attended_by(User.find(session[:user_id])) %>
<%= link_to "Cancel attendance", event_registration_path(event_id:
@event.id), method: :delete, class: "btn
btn-primary" %>
<% else %>
<%= link_to "Attend", new_event_registration_path(event_id: @event.id),
class: "btn btn-success" %>
<% end %>
<% end %>
The problem is that I do get the "Thanks for attending this event!" message but no new record is made in the event_registrations table at all.
Thank you in advance for your help.
Aucun commentaire:
Enregistrer un commentaire