undefined method `total_pages' for nil:NilClass - ruby-on-rails

I have this error when run my pagination code:
undefined method `total_pages' for nil:NilClass
I cant understand, maybe you can help me?
products_controller.rb
#products = Product.order(:nome).page params[:page]
página:
<ul class="product-evaluation-rating">
<% avaliacao.questionario_respostas.approved.rated_questions.each do |answer| %>
<li class="product-evaluation-rating-item">
<%= answer.questionario_pergunta.custom_question %>
<%= content_tag :p, class: (answer.questionario_pergunta.custom_question == 'recomendaria' ? 'strong' : nil) do %>
<%= answer.custom_answer %>
<% end %>
</li>
<% end%>
</ul>
<%= paginate #products %>
kaminari_config.rb:
# frozen_string_literal: true
Kaminari.configure do |config|
# config.default_per_page = 25
# config.max_per_page = nil
# config.window = 4
# config.outer_window = 0
# config.left = 0
# config.right = 0
# config.page_method_name = :page
# config.param_name = :page
# config.params_on_first_page = false
end

When I use the will_paginate gem which I guess you are using I use
#products = Product.order(:nome).paginate(:page => params[:page])
Also, I see that your view iterates
avaliacao.questionario_respostas.approved.rated_questions but you are paginating #products. The gem works when you show a table where each line is a Product and you want to paginate them so that you do not show them all in the same view.
I hope this helps.

Related

Sorting multiple models in one controller

I am using Ruby on Rails 5.2.3 and Mongoid 7.0
I need to be able to sort multiple models (Item and Text) in one cotroller.
Now only Item or Text is sorted, it is necessary that Position was set in relation to each other.
class UsersController < ApplicationController
def sort
params[:item].each_with_index do |id, index|
Item.where(id: id).update_all(position: index + 1)
end
head :ok
end
def admin
#user_items = #user.user_feed
end
end
admin.html.erb
<div id="items" data-url="<%= sort_users_path %>">
<%= render partial: 'users/user_item', collection: #user_items %>
</div>
_user_item.html.erb
<% if user_item[:title].present? %>
<div id="item_<%= user_item[:id] %>">
<%= user_item[:position] %>
</div>
<% end %>
<% if user_item[:text].present? %>
<div id="item_<%= user_item[:id] %>">
<%= raw user_item[:position] %>
</div>
<% end %>
User.rb
def user_activity
activity_items = []
items.each do |item|
activity_item = {}
activity_item[:id] = item.id
activity_item[:url] = item
activity_item[:title] = item.title
activity_item[:position] = item.position
activity_item[:item_link] = item.url
activity_items << activity_item
end
texts.each do |text|
activity_item = {}
activity_item[:id] = text.id
activity_item[:url] = text
activity_item[:text] = text.text
activity_item[:position] = text.position
activity_items << activity_item
end
activity_items.sort_by! { |activity_item| activity_item[:position] }
activity_items
end
def user_feed
activity_items = user_activity
activity_items.sort_by! { |activity_item| activity_item[:position] }
activity_items
end
Just needed to add Text.where(id: id).update_all(position: index + 1)
def sort
params[:item].each_with_index do |id, index|
Item.where(id: id).update_all(position: index + 1)
Text.where(id: id).update_all(position: index + 1)
end
head :ok
end

How find users in database with same content in Ruby on Rails?

In my database I have column :inviter and some users have same content in that column
I want find all them,with same content
In my user controller:
def foll
#user = current_user
#ref = User.find_by_invite(#user.id.to_s)
if !#ref || !#ref.pay_s || !#ref.pay_1 || !#ref.pay_2 || !#ref.pay_3
#referals = false
else
#referals = User.find_by_invite(#user.id.to_s)
end
end
In my views
<% if #referals != false %>
<% #referals.each do |user| %>
<h4>
<%= link_to user.name, user %> status : Good
</h4>
<% end %>
<% else %>
<h3>You dont have any referrals</h3>
<% end %>
When I try I have this error
undefined method `each' for #<User:0x007f13e41b0068>
this will raise error since
#referals = User.find_by_invite(#user.id.to_s)
will return single record
you can try #where to query all referrals
#referals = User.where(invite: #user.id.to_s)
you can also refer here for other queries
http://guides.rubyonrails.org/active_record_querying.html
hope this helps
Hope this help:
controller:
def foll
#user = current_user
#ref = User.find_by_invite(#user.id.to_s)
if !#ref || !#ref.pay_s || !#ref.pay_1 || !#ref.pay_2 || !#ref.pay_3
#referals = []
else
#referals = User.where(invite: #user.id.to_s)
end
end
view:
<% if #referals.any? %>
<% #referals.each do |user| %>
<h4>
<%= link_to user.name, user %> status : Good
</h4>
<% end %>
<% else %>
<h3>You dont have any referrals</h3>
<% end %>

Rails error undefined method `include?' for nil:NilClass

Having a bit of a rough time on this example, my code reads as:
view
<% monthNumber = (Time.now - 1.month).strftime("%m") #
yearNumber = (Time.now).strftime("%Y") #
monthWord = (Time.now - 1.month).strftime("%B")#
monthYear = "#{monthNumber}-#{yearNumber}"
createdAtCount = 0
creationDateCount = 0 %>
<h1>Stats for <%= monthWord %></h1>
<p><%= monthNumber %>-<%= yearNumber %></p>
<% #votsphonebooks.each do |entry| %>
<% if entry.CreationDate.include? monthYear %>
<p>test</p>
<% end %>
<% end %>
controller
def reports
#votsphonebooks = Votsphonebook.order("id DESC").paginate(:page => params[:page], :per_page => 5000)
end
I can, for example in the loop, run through all the first names, so the loop is working OK, but as soon as I try to test if the 'CreationDate' includes '10-2016' (which also works fine), it gets the error.
CreationDate column has the format: 11-10-2016 - 10:51 AM
Thanks!

Rails: How to iterate over products from hash? (Amazon API / Vacuum)

Expanding on Rails: How to extract values from hash? (Amazon API / Vacuum) -- how do I make these newly dug up values available to my views?
Currently getting:
undefined method `image_url' for #<Hash:0x848402e0>
Extracted source (around line #5):
<%= link_to image_tag(product.image_url), product.url %>
main_controller.rb:
class MainController < ApplicationController
def index
request = Vacuum.new('GB')
request.configure(
aws_access_key_id: 'ABCDEFGHIJKLMNOPQRST',
aws_secret_access_key: '<long messy key>',
associate_tag: 'lipsum-20'
)
params = {
'SearchIndex' => 'Books',
'Keywords'=> 'Ruby on Rails',
'ResponseGroup' => "ItemAttributes,Images"
}
raw_products = request.item_search(query: params)
hashed_products = raw_products.to_h
# puts hashed_products['ItemSearchResponse']['Items']['Item'].collect{ |i| i['ItemAttributes']['Title'] }
# puts hashed_products['ItemSearchResponse']['Items']['Item'].collect{ |i| i['DetailPageURL'] }
# puts hashed_products['ItemSearchResponse']['Items']['Item'].collect{ |i| i['LargeImage']['URL'] }
#products = []
#products = hashed_products['ItemSearchResponse']['Items']['Item'].each do |item|
product = OpenStruct.new
product.name = item['ItemAttributes']['Title']
product.url = item['DetailPageURL']
product.image_url = item['LargeImage']['URL']
#products << product
end
end
end
index.html.erb:
<h1>Products from Amazon Product Advertising API</h1>
<% if #products.any? %>
<% #products.each do |product| %>
<div class="product">
<%= link_to image_tag(product.image_url), product.url %>
<%= link_to product.name, product.url %>
</div>
<% end %>
<% end %>
I wouldn't turn the entire Amazon hash into an OpenStruct. And since product.name is nil you can't do a find on it.
Instead, just loop through the Amazon items, assign them to your product, and then add to the #products array:
#products = []
hashed_products['ItemSearchResponse']['Items']['Item'].each do |item|
product = OpenStruct.new
product.name = item['ItemAttributes']['Title']
#products << product
end

link_to edit page default path?

I'm using Rails3 to build a simple customer information page (in table format). On this page user can edit each customer's detailed information. It's possible for each customer to have multiple records. I use link_to to get to the edit page:
<td class="edit" id="edit">edit
<%= link_to 'edit', :action => :edit, :cust_id => ci.cust_id %>
</td>
edit.html.erb:
<h1>Editing cust</h1>
<%= form_for(#cust_info) do |cust| %>
Customer: <%= cust.text_field :cust_schema %>
<%= cust.submit "Save" %>
<% end%>
In my controller, I have this:
def edit
cust_id = params[:cust_id]
#cust_info = CustInfo.find(:all, :conditions => ["cust_id = ?", cust_id])
end
The customer info page shows right. When I click the edit link, I got the error message:
ActionView::Template::Error (undefined method `cust_info_cust_info_path' for #<#<Class:0x7fb0857ce7b8>:0x7fb0857ca780>):
1: <h1>Editing cust</h1>
2:
3: <%= form_for(#cust_info) do |cust| %>
4: Customer: <%= cust.text_field :cust_schema %>
5: <%= cust.submit "Save" %>
6: <% end%>
app/views/track/edit.html.erb:3:in `_app_views_track_edit_html_erb___102324197_70198065248580_0'
Where does `cust_info_cust_info_path' come from?
EDIT: here is the code in the controller:
class TrackController < ApplicationController
def display
end
def information
end
def customerinfo
#cust_info = CustInfo.find(:all, :order=>"cust_id")
#cust_info.each do |ci|
if ci.upload_freq == 'W'
ci.upload_freq = 'Weekly'
elsif ci.upload_freq == 'M'
ci.upload_freq = 'Monthly'
end
end
end
def edit
cust_id = params[:cust_id]
#cust_info = CustInfo.find(:all, :conditions => ["cust_id = ?", cust_id])
end
end
end
Change #cust_info = CustInfo.find(:all, :conditions => ["cust_id = ?", cust_id])
to
#cust_info = CustInfo.find(cust_id)

Resources