0

我为志愿者创建了一个视图,以注册特定的班次。我可以为班次编写视图和控制器,以便在单击 form_for 提交按钮时,将 current_user.id 推送到特定班次的 user_ids 属性上,该属性是一个数组。问题是它用当前用户的 id 更新了每个班次的 user_ids。我在这里想念什么?任何见解将不胜感激。谢谢。

志愿者.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>

移位.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

班次.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
4

0 回答 0