0

I'm creating an application to store sport games and compare stats.

Now on the stat page I'm trying to create a column where in the head you select the other user and the it should show his stats to compare with you own.

Controller:

class StatsController < ApplicationController
    def index

        @user_list =  User.all_except(current_user).where(:league_id => current_user.league_id).order('username ASC').map{|u| [ u.username, u.id ] } 


    end


end

VIEW:

<div class="container-fluid">
        <div class="panel panel-default">
            <div class="panel-heading">
                PERSONAL STATS
            </div>

    <div class="table-responsive">
<table class="table table-bordered">
    <thead>
                 <th style="text-align: center;width:150px"></th>
                 <th style="text-align: center">SINGLES</th>
                 <th style="text-align: center">MULTIS</th>
                 <th style="text-align: center">TOTAL</th>

                 <th style="text-align: center">

                    <%= select :username, @user_list.pluck(:id) %>

                </th>

                <th style="text-align: center;width:10px"><t  class="fa fa-unlock"> </t></th>
    </thead>

I can imagine that the dropdown has to save to an instance variable so that I can use it's data.

Example

EDIT:

                <%= form_tag stats_path, :method => :get, :class => 'form-search' do %>
                 <div class="input-append">
                  <%= collection_select :opponent_user, :id, User.all, :id, :username %>
                  <button type="submit" class="btn">Search</button>
                 </div>
                <% end %>

I've added this block of code, it now alows me to select the user and submit.

I'm getting this URL on search:

http://localhost:3000/stats?utf8=%E2%9C%93&opponent_user%5Bid%5D=4

How can I now this variable in the view?

EDIT:

I've added the ransack gem for this.

VIEW:

 <%= search_form_for @q, url: stats_path do |f| %>


<%= f.collection_select :id_eq, User.all, :id, :username, {include_blank: "Select User"} %>

    <%= f.submit %>
<% end %>

<%= @q.id  %> 

Controller:

def index


    @q = User.all_except(current_user).ransack(params[:q])

    @opponent_user = @q.result(distinct: true).to_a.uniq

end

URL after selecting a user

http://localhost:3000/stats?utf8=%E2%9C%93&q%5Bid_eq%5D=2&commit=Search

So he selects the correct user.id but now I want to use this user and it's attributes in the view.

  • so you want query that returns list of users except current user, right?? – Amol Udage May 11 '16 at 08:20
  • Yeah but that I allready managed, now I want the user to be able to select one of the users from the list in the view. The selected user his info should show up underneath – Louis De Beule May 11 '16 at 08:25
  • Hi Louis, can you specify "How can I now this variable in the view"? I'll try to help. – Yimanei May 11 '16 at 09:46
  • So on the view page (stats/index.html.erb) I'm showing all the stats. This shows stuff like current_user.wins, ... etc Now in the column where you can select another user his stats should appear. I've tried <%= opponent_user.wins %> but this doenst work. I'm thinking maybe specify it in the controller or something but I have no idea how – Louis De Beule May 11 '16 at 09:55
  • Yes, you need to use the controller. The Ransack gem can help you with it. The view will work with the code I gave you in my answer, and you'll set up your controller thanks to the gem. You'll be able to access the result in your view. Here it is https://github.com/activerecord-hackery/ransack – Yimanei May 11 '16 at 11:07
  • Is it working now? – Yimanei May 12 '16 at 07:05
  • I've updated the question @Yimanei – Louis De Beule May 12 '16 at 12:52
  • If you have the `user.id` in `opponent_user` now you can do `User.find(opponent_user)` to get the user object. – Yimanei May 12 '16 at 13:52
  • Couldn't find User with 'id'=# I'm getting this error. – Louis De Beule May 13 '16 at 08:59
  • I see, Ransack returns a collection. Having a look at your answer, I see that you had the user id in the url before? Sorry I overlooked it. Then the solution is to use it in the controller to find the @opponent_user: `@opponent_user = User.find(params[:user_id])` – Yimanei May 13 '16 at 11:42

2 Answers2

0

try this

@user_list =  User.where.not(current_user).where(:league_id => current_user.league_id).order('username ASC').map{|u| [ u.username, u.id ] } 
Pravesh Khatri
  • 2,124
  • 16
  • 22
  • Thanks for the reply, however I got this part allready working, as you can see on the image I've added I managed to get the array with all the users I need. Now the problem is in the view, the user should be able to choose from a dropdown which user to watch, and then his stats should appear underneath. – Louis De Beule May 11 '16 at 08:41
  • As per your requirement `select_tag 'user_id', options_for_select(@user_list.collect{ |u| [u.name, u.id] })` – Pravesh Khatri May 11 '16 at 08:52
0

For this type of thing you can use collection_select:

<%= f.collection_select :user_id, User.all, :id, :name, {include_blank: "Select User"} %>

More on collection_select: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select, and Can someone explain collection_select to me in clear, simple terms?

Community
  • 1
  • 1
Yimanei
  • 242
  • 2
  • 8