lundi 12 juin 2017

Getting all users from db that participated for an activity

I am currently facing the following problem: Users are able to create activities. Other users have the possibility, to participate the activity. I now want that the users, which already participated to an activity are going to be listed in the view of the activity. So what I try is to show all the users that participated for an activity in the show method of the activity_controller with an activity_id.

The relations of the models are as follows:

 class Activity < ActiveRecord::Base
  has_many :users, through: :participations
  belongs_to :user
  has_many :participations
  has_many :reviews

end


 class Participation < ActiveRecord::Base
  belongs_to :user
  belongs_to :activity
  has_many :activities
end




 class User < ActiveRecord::Base
  has_many :activities
  has_many :participations
  has_many :reviews

end

My activity controller ist doing the following:

class ActivitiesController < ApplicationController
  before_action :set_activity, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:show]

  def index
    @activities = Activity.all
  end

  def show
    @participated = Participation.where("activity_id = ? AND user_id = ?", @activity.id, current_user.id).present? if current_user
    @reviews = @activity.reviews
    @hasReview = @reviews.find_by(user_id: current_user.id) if current_user

    @participant = Participation.where (:activity_id == 'activity_id')
  end



  def new
    @activity = current_user.activities.build
  end

  def create
    @activity = current_user.activities.build(activity_params)

    if @activity.save

      redirect_to edit_activity_path(@activity), notice: "We want to give you a warm welcome!"

    else
      render :new, notice: "Something seems to be missing!"
    end
  end

  def edit
    if current_user.id == @activity.user.id
      redirect_to root_path
    end
  end

  def update
    if @activity.update(activity_params)
      redirect_to edit_activity_path(@activity), notice: "Updated!"
    else
      render :edit
    end
  end

  def destroy
    @activity = Activity.find(params[:id])

    if @activity.destroy
      redirect_to activities_path, notice: "Deleted!"
    end
  end

  private

  def set_activity
    @activity = Activity.find(params[:id])
  end


  def activity_params
    params.require(:activity).permit(:activity_name,
                                     :activity_starttime,
                                     :activity_duration,
                                     :activity_intensity,
                                     :activity_distance,
                                     :activity_topography,
                                     :activity_address,
                                     :activity_track,
                                     :activity_summary)

  end
end

Meanwhile the participations controller handles this:

    class ParticipationsController < ApplicationController
  before_action :authenticate_user!

  def preload
    activity= Activity.find(params[:activity_id])
    participations = activity.participations
    render json: participations
  end

  def create

    if Participation.exists?(user_id: current_user.id)

      redirect_to activities_path(@activity), notice: "Du nimmst bereits teil!"

    else

      @participation = current_user.participations.create(participation_params)
      redirect_to @participation.activity, notice: "Klasse, du nimmst nun teil!"

    end

  end


  def participant
    @participant = user.participations
  end

  def your_matches
    @matches = current_user.participations
  end

  def your_participations
    @activities = current_user.activities
  end

  private

  def participation_params
    params.permit(:activity_id, :status)
  end
end

In my view, I called the controller by using:

<div class="row">
  <div class="col-md-12">
    <h3>Wer kommt bis jetzt mit? </h3>


    <table>
      <tr>
        <th></th>
      </tr>

      <% @participant.each do |participant| %>

          <tr>
            <td><%= participant.user.fullname %></td>
          </tr>

      <% end %>

    </table>

  </div>
</div>

Which gives me a list of all users that participated for any activity. But the only thing I want now is to show all the users which participated for the activity requested. I would say that my relations should work properly, therefore I guess I just need a small hint to solve the problem. At another point of my app I already got a list, where I am able to show the users which participated, but because of the different controllers, I am not able to call this from the activity controller. All in all, this causes an error because I am not able to call the participations_controller from an activitities_view.

<div class="panel-body">
    <% @activities.each do |activity| %>
      <% activity.participations.each do |participation| %>


      <div class="row">

        <div class="col-md-3">
          <%= link_to user_path(participation.user) do %>
              <%= participation.user.fullname %>
          <% end %>
      </div>

Would really appreciate any help and thanks in advance!

Aucun commentaire:

Enregistrer un commentaire