I have created a view for volunteers to sign up for specific shifts. I am able to write my view and controller for shifts so that, when the form_for submit button is clicked, the current_user.id is pushed onto the specific shift's user_ids attribute, which is an array. The problem is that it updates each of the shift's user_ids with the current user's id. What am I missing here? Any insight would be appreciated. Thanks.
volunteer.html.erb
<div class="container">
<div style="width:60%;margin:0 auto 0 auto;" class="inner-lower">
<div class="list-group">
<% @uniq_shifts.each do |title| %>
<% @shifts_by_title = @shifts.where(title: title) %>
<% @title_vols = @shifts_by_title.pluck(:vols_needed).sum %>
<% @times = @shifts_by_title.pluck(:time) %>
<% @time_vols = @shifts_by_title.pluck(:vols_needed) %>
<% if @title_vols > 0 %>
<!-- ACTIVITY TITLE -->
<div id=<%= "activity#{title}" %> class="activity">
<a href="#" class="list-group-item">
<%= title %>
<!-- ACTIVITY NUMBER VOLUNTEERS NEEDED-->
<span class="badge-volunteer"><%= @title_vols %></span>
</a>
</div>
<div class="sub" style="display:none;">
<% @shifts_by_title.each do |shift| %>
<!-- ACTIVITY SHIFT -->
<a href="#" class="list-group-item-sub">
<!-- ACTIVITY SHIFT TIME -->
<%= shift.time %>
<span class="badge">
<!-- ACTIVITY SHIFT NUMBER OF VOLUNTEERS NEEDED -->
<%= shift.vols_needed %>
</span>
</a>
<%= form_for shift, :method => :put do |f| %>
<%= f.hidden_field :user_ids, :value => shift.add_user_id(@user.id) %>
<%= f.submit "sign up", class: "btn btn-primary" %>
<% end %>
<% end %>
</div>
<% end %>
<% end %>
</div>
</div>
shift.rb
class Shift < ActiveRecord::Base
has_and_belongs_to_many :users
def add_user_id(user_id)
user_ids_will_change!
update_attributes user_ids: self.user_ids + [ user_id ]
self.save
end
end
shifts.controller.rb
class ShiftsController < ApplicationController
before_action :set_shift, only: [:show, :edit, :update, :destroy]
before_action :volunteer, only: [:show, :edit, :update, :destroy]
def index
@shifts = Shift.all
end
def volunteer
@shifts = Shift.all
@user = current_user
@shift_titles = @shifts.pluck(:title)
@uniq_shifts = @shift_titles.uniq
@vols_needed = @shifts.pluck(:vols_needed)
unless current_user
render action: 'new'
end
end
def show
end
def new
@shift = Shift.new
end
def edit
end
def create
@shift = Shift.new(shift_params)
if @shift.save
redirect_to @shift, notice: 'Shift was successfully created.'
else
render :new
end
end
def update
@user = current_user
if @shift.update(shift_params)
redirect_to @shift, notice: 'Shift was successfully updated.'
else
render :edit
end
end
def destroy
@shift.destroy
redirect_to pages_url, notice: 'Shift was successfully destroyed.'
end
private
# Use callbacks to share common setup or contraints between actions.
def set_shift
@shift = Shift.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def shift_params
params.require(:shift).permit(:title, :time, :vols_needed, :user_ids => [])
end
end