Reputation: 199
I'm currently trying to pass User_id and Event_id as params for Guest:
Right now in the console, I can create a new guest object using:
Guest.new(user_id: #, event_id: #)
After creating a guest object, I can call 'User.assited_events' to get all the events this user is assisting, same with events I can call 'Event.assitances' to gell all the users assisting this event.
I just want to figure out a way to submit the user_id and event_id from events#index.
I'm using a custom method called 'Assist' inside of Events Controller
def assist
@guest = Guest.create(:user_id => User.find(session[:current_user_id]), :event_id => Event.find(params[:id]))
respond_to do |format|
if @guest.save
format.html { redirect_to root_path, notice: 'You are now assiting this event!' }
format.json { head :no_content}
else
format.html { redirect_to root_path, notice: "An error happened you can't assist this event" }
format.json { render json: @guest.errors, status: :unprocessable_entity }
end
end
end
This the current line to link the assist_event_path at events#index
<td><%= link_to 'Assist', assist_event_path(event), method: :put, data: { confirm: 'Do you want to assist to this event?' } %></td>
The result in the server log is passing both ids but the Guest object is not created:
Processing by EventsController#assist as HTML
Parameters: {"authenticity_token"=>"8nddKRZpYcgYDkfJIv/VXK8Os1FmW1oZ+zRIQUnLlE/dhgIA92chq++leqplfaB+bdqIZnCWlB0vPLRfuoHOGw==", "id"=>"1"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 4], ["LIMIT", 1]]
↳ app/controllers/events_controller.rb:65:in `assist'
Event Load (0.2ms) SELECT "events".* FROM "events" WHERE "events"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ app/controllers/events_controller.rb:65:in `assist'
User model
class User < ApplicationRecord
has_many :events
has_many :guests
has_many :assisted_events, :through => :guests, :source => :event
end
Event model
class Event < ApplicationRecord
belongs_to :user
has_many :guests
has_many :assistances, :through => :guests, :source => :user
end
Guest model
class Guest < ApplicationRecord
belongs_to :user
belongs_to :event
end
routes file
Rails.application.routes.draw do
resources :events do
member do
patch :assist
put :assist
end
end
resources :users
root 'events#index'
end
EDIT ----
Events Controller
class EventsController < ApplicationController
before_action :set_event, only: [:show, :edit, :update, :destroy]
# GET /Events
# GET /Events.json
def index
@events = Event.all
end
# GET /Events/1
# GET /Events/1.json
def show
end
# GET /Events/new
def new
@event = User.find(session[:current_user_id]).events.build
end
# GET /Events/1/edit
def edit
end
# POST /Events
# POST /Events.json
def create
@event = User.find(session[:current_user_id]).events.build(event_params)
respond_to do |format|
if @event.save
format.html { redirect_to @event, notice: 'Event was successfully created.' }
format.json { render :show, status: :created, location: @event }
else
format.html { render :new }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /Events/1
# PATCH/PUT /Events/1.json
def update
respond_to do |format|
if @event.update(event_params)
format.html { redirect_to @event, notice: 'Event was successfully updated.' }
format.json { render :show, status: :ok, location: @event }
else
format.html { render :edit }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end
# DELETE /Events/1
# DELETE /Events/1.json
def destroy
@event.destroy
respond_to do |format|
format.html { redirect_to events_url, notice: 'Event was successfully destroyed.' }
format.json { head :no_content }
end
end
def assist
@guest = Guest.create(:user_id => User.find(session[:current_user_id]), :event_id => Event.find(params[:id]))
respond_to do |format|
if @guest.save
format.html { redirect_to root_path, notice: 'You are now assiting this event!' }
format.json { head :no_content}
else
format.html { redirect_to root_path, notice: "An error happened you can't assist this event" }
format.json { render json: @guest.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_event
@event = Event.find(params[:id])
end
# Only allow a list of trusted parameters through.
def event_params
params.require(:event).permit(:title, :body)
end
end
Events#Index file
<p id="notice"><%= notice %></p>
<h1>Events</h1>
<% if session[:current_user_id].is_a? Integer %>
<h3>Current User ID: <%= session[:current_user_id] %></h3>
<% else %>
<%= link_to 'Create a new user', new_user_path %>
<% end %>
<table>
<thead>
<tr>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @events.each do |event| %>
<tr>
<td><%= event.title %></td>
<td><%= event.body %></td>
</tr>
<tr>
<td><%= link_to 'Show', event %></td>
<td><%= link_to 'Edit', edit_event_path(event) %></td>
<td><%= link_to 'Assist', assist_event_path(event), method: :put, data: { confirm: 'Do you want to assist to this event?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New event', new_event_path %>
Upvotes: 0
Views: 111
Reputation: 6411
Well this is kind of a guess since you didn't post any of your form except the link_to. I have no idea what variables you have passed to the events#index
because you didn't post your controller code for that. That said you can pass any params you want with link_to
.
edit OK, so it looks like user is in session[:current_user_id]
so that is where the user id comes from...
latest edit to simplify
Add this route before your other routes to make sure it's at the top:
put '/assist' => 'events#assist'
Then in your form:
<td><%= link_to 'Assist', assist_path(event_id: event, user_id: session[:current_user_id]), method: :put, data: { confirm: 'Do you want to assist to this event?' } %></td>
Make sure your params are permitted in your strong parameters section.
# Only allow a list of trusted parameters through.
def event_params
params.require(:event).permit(:title, :body, :event_id, :user_id)
end
edit cleaning up your controller code:
def assist
@guest = Guest.new(:user_id => session[:current_user_id], :event_id => event_params[:id])
respond_to do |format|
if @guest.save
format.html { redirect_to root_path, notice: 'You are now assiting this event!' }
format.json { head :no_content}
else
format.html { redirect_to root_path, notice: "An error happened you can't assist this event" }
format.json { render json: @guest.errors, status: :unprocessable_entity }
end
end
end
You were passing the actual Event and User objects to create the @guest object. Also you were using the params, which means you weren't going through the event_params action. The purpose of the event_params action is to permit/deny so that someone can't submit params you don't want.
Upvotes: 1