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