I am trying to display in my a table with most popular tag words, ordered by the count of impression. Then add kaminari to paginate through the records.
in my controller I tried:
#tag_answers = Tag.group(:content).page(params[:page]).order("count_all DESC").count
and in my view:
<table>
<thead>
<th>Tag</th>
<th>Count</th>
</thead>
<tbody>
<% #tag_answers.each do |tag_content, tag_count| %>
<tr>
<td> <%= tag_content %> </td>
<td> <%= tag_count %> </td>
</tr>
<% end %>
</tbody>
</table>
<%= paginate #tag_answers %>
but i am getting the following error
undefined method `current_page' for #<ActiveSupport::OrderedHash:0x000001031b8678>
Try
#tag_answers = Tag.group(:content).select('content, COUNT(*) as count').order("count DESC").page(params[:page])
Related
In my home page I iterate over collections of objects, and for each object I render its attributes in a table row. There are four collections of objects, defined as instance variables in my controller, all making Guard (according to the used method) raising one of the following errors:
ActionView::Template::Error: undefined method `each_with_index' for nil:NilClass
ActionView::Template::Error: undefined method `any?' for nil:NilClass
The code in my application view raising the above errors is:
<table class="col-md-4 col-md-offset-1">
<thead>
<tr>
<th> Rank </th>
<th> Gamer </th>
<th> Points </th>
</tr>
</thead>
<tbody>
<% #atp_gamers.each_with_index do |gamer, index| %>
<tr>
<td class="index"> <%= index+1 %> </td>
<td class="gamer"> <%= gamer.name %> </td>
<td class="atppoints"> <%= gamer.atpscore %> </td>
</tr>
<% end %>
<tr class="current-user">
<td> <%= #atp_gamers.to_a.index(current_user) + 1 %> </td>
<td> <%= current_user.name %> </td>
<td> <%= current_user.atpscore %> </td>
</tr>
</tbody>
</table>
<table class="col-md-4 col-md-offset-2">
<thead>
<tr>
<th> Year </th>
<th> Champion </th>
<th> Points </th>
</tr>
</thead>
<tbody>
<% if #atp_champions.any? %>
<% #atp_champions.each do |champion| %>
<tr>
<td class="year"> <%= champion.created_at.year %> </td>
<td class="winnername"> <%= champion.name %> </td>
<td class="winnerpoints"> <%= champion.points %> </td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
The above code is part of a partial (named _gamers_home.html.erb) rendered in the original home page:
<% if logged_in? %>
<% if current_user.gamer? %>
<%= render 'static_pages/gamers_home' %>
<% else %>
<%= render 'static_pages/non_gamers_home' %>
<% end %>
<% else %>
<%= render 'static_pages/non_logged_in_home' %>
<% end %>
The logged_in? method is defined as !current_user.nil?
The instance variables that result nil are: #atp_gamers, #wta_gamers, #atp_champions and #wta_champions, defined in the controller as follows:
def home
if logged_in? && !current_user.gamer?
...l
elsif logged_in? && current_user.gamer?
#gamers = User.where(gamer: true)
#atp_gamers = #gamers.order(atpscore: :desc).limit(50)
#wta_gamers = #gamers.order(wtascore: :desc).limit(50)
#atp_champions = AtpChampion.all
#wta_champions = WtaChampion.all
...
end
end
The first instance variable raising the error (each_with_index' for nil:NilClass) is #atp_gamers. In view I tried to change it with its explicit value, that is User.where(gamer: true).order(atpscore: :desc).limit(50), and the respective code is accepted. After this change, Guard raises an error for #atp_champions.
With rails console #atp_gamers and #wta_gamers are not empty, returning 50 records out of 100 users. #atp_champions and #wta_champions are not nil, but empty arrays.
I suspect that this might be an issue raised only by Guard, because the rails server succeeds in rendering the view.
def home
if logged_in? # delete this line
...
end # delete this line
end
Delete the if logged_in?, and see what happens.
Maybe you have to use before_action :logged_in_user, only :home in controller and define the logged_in_user method as private method.
If non-logged-in users also allowed to access the home action, you need to use if statement erb in the view. Like,
<% if logged_in? %>
<% #atp_gamers.each_with_index do |gamer, index| %>
...
<% end %>
--UPDATE--
Maybe, it needs to toss variables to the partial.
Replace
<%= render 'static_pages/gamers_home' %>
to
<%= render partial: 'static_pages/gamers_home', locals: {atg_gamers: #atp_gamers, wta_gamers: #wta_gamers, atp_champions: #atp_champions, wta_champions, #wta_champions} %>
and, replace the #atp_gamers, #wta_gamers, #atp_champions, #wta_champions in the partial to atp_gamers, wta_gamers, atp_champions, wta_champions.
Try and see what happens.
I am trying to submit a grid form like the below one,
show.html.erb
<%= form_for :datadef, url: update_all_vmodule_path do |fdf|%>
<table id="csvfiles" class="display" width="100%">
<thead>
<tr>
<th width="10%">Column No</th>
<th width="20%">Column name</th>
<th width="20%">Data type</th>
<th width="15%">Column format</th>
<th width="10%">Length</th>
<th width="25%">Comments</th>
</tr>
</thead>
<tbody>
<% #datadefrecords.each do |ddre| %>
<%= fields_for "datadef_records[]", ddre do |dr_fld| %>
<tr>
<td><%= ddre.column_no %></td>
<td><%= ddre.column_name %></td>
<td><%= dr_fld.collection_select :column_datatype, Datadef.select(:column_datatype).uniq, :column_datatype, :column_datatype, {:prompt => true} %> </td>
<td><%= dr_fld.text_field :column_format %></td>
<td><%= dr_fld.text_field :column_length %></td>
<td><%= dr_fld.text_field :comments %></td>
</tr>
<% end %>
<% end %>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td colspan="2"><%= fdf.submit 'Save' %> <%= link_to("<button>Cancel</button>".html_safe, cancelmodal_vmodule_path ) %></td>
</tr>
</tbody>
</table>
<% end %>
<% end %>
This the method i am calling it to submit
def update_all
session[:datadef] = nil
#updatedatadef = Datadef.all.where(:id => params[:datadef_records]).update_all(update_all_params)
redirect_to migproject_path(params[:vmodule][:migproject_id])
end
This is the strong parameters
def update_all_params
params.require(:datadef_records).permit( :column_datatype, :column_format, :column_length, :comments)
end
This is how i get the parameters from form
{"utf8"=>"✓",
"authenticity_token"=>"QrX8JYYYQOlfwKgAABJd0+A7VpugS2Y6n8doDsuKqeM=",
"datadef_records"=>{"12"=>{"column_datatype"=>"varchar",
"column_format"=>"2",
"column_length"=>"2",
"comments"=>"2"},
"13"=>{"column_datatype"=>"varchar",
"column_format"=>"2",
"column_length"=>"2",
"comments"=>"2"}},
"commit"=>"Save",
"id"=>"2"}
But i get this "Empty list of attributes to change" error which is not allowing me to write it in table. And i am not able to identify what could be the error.
Thanks in advance.
I think the problem is to do with your use of update_all, although I'm not 100% sure as to the syntax of the problem.
From what I understand, update_all needs a hash of actual params to populate your data; even then, it will only update what you pass to it (kind of like update_attributes).
Here's a good reference:
data = params[:datadef_records]
#updatedatadef = Datadef.update(data.keys, data.values)
This negates the use of your strong_params, which I'd recommend using. I'd have to spend some time thinking about getting it to work.
I am trying to run number_of_days.times do to create a table, and in the first column I need to iterate through dates. I am trying to add + 1.day for each .times do so that each row of the table has the next date. I am blanking on why my counter isn't working inside the .times do and what I should do about it. Here is my code:
#day_count is the number of days from a starting date to today, which I have confirmed is working to return the correct number integer and in fact is running through my times do correctly because it creates the correct number of table rows.
<table>
<thead>
<th><strong>Date</strong></th>
<th><strong>Total Entries</strong></th>
<th><strong>New Entries</strong></th>
<th><strong>New Form Entries</strong></th>
<th><strong>New Image Entries</strong></th>
</thead>
<tbody>
<% count = -1 %>
<% #day_count.times do %>
<tr>
<% count.to_i + 1 %>
<td><%= #start_date + count.day %></td>
<td> test </td>
<td> test </td>
<td> test </td>
<td> test </td>
</tr>
<% end %>
</tbody>
</table>
each#start_date + count.day produces the same date. I know I am missing something very simple here... thanks in advance for the help
I'm not sure why you wouldn't:
(1..#day_count).each do |count|
...
or
(#start_date..Date.today).each do |day|
...
I'm not sure you're incrementing the count variable each time the loop is iterated.
Try adding count += 1 somewhere in there.
<table>
<thead>
<th><strong>Date</strong></th>
<th><strong>Total Entries</strong></th>
<th><strong>New Entries</strong></th>
<th><strong>New Form Entries</strong></th>
<th><strong>New Image Entries</strong></th>
</thead>
<tbody>
<% count = 0 %>
<% #day_count.times do %>
<tr>
<td><%= #start_date + count.day %></td>
<% count += 1 %>
<td> test </td>
<td> test </td>
<td> test </td>
<td> test </td>
</tr>
<% end %>
</tbody>
</table>
I have a game, which creates logs. On display of the logs, I'd like to group them by turn to create an overall table, with a nested table for each turn within. In order to accomplish this, I am trying to group the logs by turn, so I can then iterate over them with an outer loop that runs once per turn, and an inner loop for each log item within that turn.
Right now, I have:
# logs_controller.rb
#logs = #game.logs.order("created_at ASC").group(:turn)
and in the view:
# logs/index.html.erb
<% #logs.each do |turn_logs| %> <table class="table">
<tr>
<td><%= turn_logs.first.turn%></td>
<td>
<table class="table table-striped">
<% turn_logs.each do |log| %>
<tr><td><%= log.text %></td></tr>
<% end %>
</table>
</td>
</tr>
</table>
This doesn't work, since Postgres complains that:
ActiveRecord::StatementInvalid: PG::GroupingError: ERROR: column "logs.id" must appear in the GROUP BY clause or be used in an aggregate function
I'm sure there's a simple way to do this that I'm overlooking, and I'll slap my head when it's pointed out, but right now it's eluding me.
Okay, I've got a good solution, I think. It involves using group_by to make an array of an array of log objects, with the turn as the key:
Controller:
# logs_controller.rb
#logs_by_turn = #game.logs.order("created_at ASC").group_by {|log| log.turn}
and in the view:
# logs/index.html.erb
<table class="table">
<% #logs_by_turn.each do |turn, turn_logs| %>
<tr>
<td><%= turn_logs.first.turn %></td>
<td>
<table class="table table-striped">
<% turn_logs.each do |log| %>
<tr><td><%= log.text %></td></tr>
<% end %>
</table>
</td>
</tr>
<% end %>
I have a loop in one of my views to display a table like so:
Each category object has 5 attributes called: level_1, level_2, level_3, level_4, level_5.
There will be an array with all the category objects. so there could be any amount of categories and no necessarily 3.
what would the best way to draw this up be? i have something like this at the moment but dont know how to select the relevant category level attribute in the 5.times loop.
<table border="0">
<tr>
<th>Maturity</th>
<% for category in #categories %>
<th><%= category.category_title %></th>
<% end %>
</tr>
<% 5.times do |i|%>
<% level = 5 - i %>
<tr>
<td>Level <%= level %> Maturity</td>
<% for category in #categories %>
<td><%= category.level_ #put in the level value here so it selects the relevant attraibute %></td>
<% end %>
</tr>
<% end %>
</table>
you need to change the rendering of level with this:
<% for category in #categories %>
<td><%= category.send("level_#{level}") %></td>
<% end %>
send is used to call a method on an object so that you can compose your method at runtime.
If you categories as variable no. then you shouldn't make it columns, but rows. And then levels will be you columns e.g.
<table>
<tr>
<th>Level 1</th>
<th>Level 2</th>
<th>Level 3</th>
<th>Level 4</th>
<th>Level 5</th>
<tr>
<% #category.each do |c| %>
<tr>
<td>#category.level_1<td>
<td>#category.level_2<td>
<td>#category.level_3<td>
<td>#category.level_4<td>
<td>#category.level_5<td>
<th>
<% end %>
Now in above code you may replace hard coded level_#{no} with iterations.