Rails error undefined method `include?' for nil:NilClass - ruby-on-rails

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!

Related

undefined method `total_pages' for nil:NilClass

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.

How can I get the model name of object in rails?

I got four different models.
Here's an example,
#single = Single.all
#coe = Coe.all
#blend = Blend.all
#production = #single+#coe+#blend
then how to check which model #production is?
I tried
<% #production.each do |p| %>
<%=p.class.name%>
<% end %>
but it returns "Array"
It seems to be simple, but I can't find out
(I edited question)
The problem is here
#single = Single.all
#coe = Coe.all
#blend = Blend.all
#production = #single+#coe+#blend
change these lines with
#single = Single.all.to_a
#coe = Coe.all.to_a
#blend = Blend.all.to_a
#production = #single+#coe+#blend
and then if you will check
#production.first.class.name #Single
#production.last.class.name #Blend
so in your view you can do this
<% #production.each do |p| %>
<% p.each do |product| %>
<%= product.class.name %>
<% end %>
<% end %>
if while iterating on #production it returns array so you need to try this.
<% #production.each do |p| %>
<% p.each do |product| %>
<%= product.class.name %>
<% end %>
<% end %>
#production is a collections of combinations of single, coe, and blend thats why #production.class.name doesnt work, you need to iterate each object like this:
<% #production.each do |object| %>
<%= object.class.name %>
<% end %>

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

Getting Template::Error (Undefined method 'each') for nil:NIL Class

View:-
<% #dating_advices.each do |da| %>
<% if da['is-displayed'][0]['content'] == 'true' %>
<div class="dating_advice">
<h4><%= da['title'][0] %></h4>
<p><b><%= da['author'][0] %></b></p>
<p><%= da['content'] %></p>
</div>
<hr>
<% end %>
<% end %>
Controller:-
def dating_advices
#current_menu = "MatchMasters"
logger.debug "*** Current site id: #{#current_site.id}"
##hide_quick_search = true
#passed_args = {
'event_action' => 'ws',
'site_id' => #current_site.site_id
}
result = call_dating_advices_ws(#passed_args)
if result && result['errorcode'][0] == 'success'
#dating_advices = result['payload'][0]['payload']
end
end
Now when I click on dating advices on my webpage it gives an error"Getting Template::Error (Undefined method 'each') for nil:NIL Class"
You probably didn't get any #dating_advices -- which would mean you're calling each on an instance var that doesn't exist.
The way your controller is written, #dating_advices is never defined if the result variable is not defined.
You could quickly patch it with a #dataing_advices = [] (or a hash -- anything with an .each method) and figure out why your controller is failing.
You could do something like
if result && result['errorcode'][0] == 'success'
... your dating advice code
else
raise result
(or #dating_advice = {})
end
basically you need to always defined #dating_advice OR you need to handle that value not being defined in your view.

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