How to fix the error "undefined method" in my Rails Geocoder? - ruby-on-rails

My code is working in console but not in App
➜ Meet-and-Eat git:(master) ✗ rails c
Running via Spring preloader in process 15789
Loading development environment (Rails 5.2.2)
2.5.3 :001 > i = ["10 Palmerston Street", "DERBY"]
=> ["10 Palmerston Street", "DERBY"]
2.5.3 :002 > result = Geocoder.search("#{i[0]}, #{i[1]}").first.coordinates
=> [52.9063415, -1.4937474]
My code :
<% #places = [] %>
<% #placesCoordinations = [] %>
<% #information.each do |i| %>
<% #places.push([i.address1, i.town, i.postcode, information_path(i)]) %>
<% end %>
<% #places.each do |i| %>
<% result = Geocoder.search("#{i[0]}, #{i[1]}").first.coordinates %>
<% #placesCoordinations.push(result) %>
<% end %>
Error :
NoMethodError in Information#full_map_adresses.
Showing /Users/mateuszstacel/Desktop/Meet-and-Eat/app/views/information/full_map_adresses.html.erb where line #10 raised:
undefined method `coordinates' for nil:NilClass
<% #places.each do |i| %>
<% result = Geocoder.search("#{i[0]}, #{i[1]}").first.coordinates%> //this line is breaking my app
<% #placesCoordinations.push(result) %>
<% end %>
But if I use only single location or postcode or street address that work but i need to use both of them to be more precision.
<% #places = [] %>
<% #placesCoordinations = [] %>
<% #information.each do |i| %>
<% #places.push([i.address1, i.town, i.postcode, information_path(i)]) %>
<% end %>
<% #places.each do |i| %>
<% result = Geocoder.search("#{i[2]}").first.coordinates %>
<% #placesCoordinations.push(result) %>
<% end %>

The error message undefined methodcoordinates' for nil:NilClassindicates that theGeocoder.search("#{i[0]}, #{i[1]}")itself is successful, butGeocoder.search("#{i[0]}, #{i[1]}").firstsimply returnsnil`.
It seems like your #information array contains at least one address that cannot be resolved. There might be many reasons: Perhaps there is just a typo in the address or it is a very small village or an address in a country which is not supported by the service you are using.
Tip to debug: Change your code to show what it passes to the method and if there were any results. Something like this might help:
<% #places.each do |i| %>
Address string: <%= "#{i[0]}, #{i[1]}" %>
<% result = Geocoder.search("#{i[0]}, #{i[1]}").first %>
Result: <%= result.present? %>
<% #placesCoordinations.push(result.coordinates) if result.present? %>
<% end %>
Furthermore: I suggest moving code like this to a model, the controller or a helper. It feels like this doesn't belong in an ERB view.

Finally that work !
Inside of the model :
class Information < ApplicationRecord
geocoded_by :address
after_validation :geocode
def address
[address1, address2, town, postcode].compact.join(", ")
end
end
then in terminal run command :
rake geocode:all CLASS=Information SLEEP=0.25 BATCH=100

Related

How to specify syntax error in Ruby on Rails

My controller is like this
def show
#receipt = Receipt.find(params[:id])
#hospitalizations=#receipt.hospitalizations
#outpatients=#receipt.outpatients
#surgeries=#receipt.surgeries
end
my show.html.erb is like this.
<h1>details of receiptid: <%= #receipt.id %></h1>
<% #hospitalizations.each do |hospitalization| %>
<p>hospitalization_id:<%= hospitalization.id %>hospitalization_start :<%= hospitalization.hospitalization_start %> hospitalization_end:<%= hospitalization.hospitalization_end %> </p>
<% #surgeries.each do |surgery| %>
<p>surgeryid:<%= surgery.id %> surgery_day :<%= surgery.surgery_day %> </p>
<% #outpatients.each do |outpatient| %>
<p>outpatientid:<%= outpatient.id %>outpatient_day :<%= outpatient.outpatient_day %> </p>
<% end %>
When I access it's page,some error like below was incurred. I tried to specify error location,but didn't work well.
It seems syntax error,where should end insert?
If someone has experienced same issues,please let me know.
Each of your each loop requires an <% end %> clause, you can't find the specific line since the error says it reached the end when it expected "end". Close all your loops like this:
<% #hospitalizations.each do |hospitalization| %>
<p>hospitalization_id:<%= hospitalization.id %>hospitalization_start :<%= hospitalization.hospitalization_start %> hospitalization_end:<%= hospitalization.hospitalization_end %> </p>
<% end %>

Ruby on rails create a new variable to make an each

i'm new into ruby on rails and i want to assign a variable to make an each like this
<% 3.times do |calendar| %>
<% test = #lessons_calendar %>
<% test.each do |lesson| %>
display html here
<% end %>
<% end %>
The thing is that in my controller i have assigned 3 variables like this #lessons_1 #lessons_2 and #lessons_3 but when i run the code it says undefined method `each' for nil:NilClass, how can i join the number created by calendar to the new variable ? Thanks
Instead of below
<% 3.times do |calendar| %>
<% test = #lessons_calendar %>
<% test.each do |lesson| %>
display html here
<% end %>
<% end %>
Make changes in your controller as well as in view as below
Controller Code
# take new variable
#lessons = []
#lessons << #lessons_1
#lessons << #lessons_2
#lessons << #lessons_3
Now do code in view file as below
<% #lessons.each do |lesson| %>
<% lesson.each do |ls| %>
your code here
<%end%>
<%end%>
Hope this will help you.
<% 3.times do |calender| %>
<%= #lessons_calendar.collect{ |lesson| Write Your Code Here }.join("").html_safe rescue 'No Record' %>
<% end %>
With in the collect iterator you can assign it to instance var if you want to. Thanks

undefined method `value' for #<CategoryItemValue::ActiveRecord_Associations_CollectionProxy:0x007ff9706d24c0>

This is so simple but isnt working. What am I missing?
controlelr
#guide = Guide.friendly.find(params[:guide_id])
#category = #guide.categories.friendly.find params[:id]
#items = #category.category_items
view
<% #items.each do |item| %>
<%= item.category_item_values.value %>
<% end %>
gives the no method error of
undefined method 'value' for #<CategoryItemValue::ActiveRecord_Associations_CollectionProxy:0x007ff9706d24c0>
There is a values column in the category_item_values table so I'm not sure what the problem is.
item.category_item_values is the CollectionProxy instance (one might think of it as of an kinda array.)
Each category_item has [likely, you did not provide sufficiently enough info to guess more precisely] many values. If the assumption above is correct, here you go:
<% #items.each do |item| %>
<% item.category_item_values.each do |value| %>
<%= value %> # or maybe (depending on your model) <%= value.value %>
<% end %>
<% end %>
You will have to loop over each of the category_item_values to get the result as this suggests <CategoryItemValue::ActiveRecord_Associations_CollectionProxy:0x007ff9706d24c0> that your category_item_value is a association.
So you could do something like
<% item.category_item_values.each do |category_item_value| %>
<%= category_item_value.value %>
<% end %>

Iterate over an Active Record Relation in Rails 3.2

I have the following code:
#items = QuestionGroup.search(params[:search]).limit(50)
This returns an ActiveRecord relation. In the view I want to iterate through it so I use:
<% if #items.present? %>
<%= #items.each do |r| %>
<%= div_for r do %>
<div><%= r.subject %></div>
<% end %>
<% end %>
<% end %>
This does print r.subject to the view but it then follows it with the entire relation. e.g.
the pipe
[#<QuestionGroup id: **, subject: "the pipe", created_at: "*******", updated_at: "******"]
Why is this and how can I fix it?
Problem is here:
<%= #items.each do |r| %>
This line of code iterates over each of the relations and due to the '=' you output its content. Change it to:
<% #items.each do |r| %>
and you are good to go!

How to iterate instance variables within instance variables in view?

I'm using Rails 3.2. I have the following code:
# transports_controller.rb
#transports = %w(car bike)
#transports.each do |transport|
instance_variable_set("##{transport.pluralize}",
transport.classify.constantize.all)
end
# transports/index.html.erb
<% #transports.each do |transport| %>
<h1><%= transport.pluralize.titleize %></h1>
<% #transport.pluralize.each do |transport_item| %>
<%= transport_item.name %><br>
<% end %>
<% end %>
The controller code is correct, but the view code is wrong. #transport.pluralize.each cannot be called literally . Expected result is:
<h1>Cars</h1>
Ferrari<br>
Ford<br>
<h1>Bikes</h1>
Kawasaki<br>
Ducati<br>
How do I do this?
Just like you use instance_variable_set, there is an instance_variable_get available.
You don't have to create instance variables for this, just use an array (or a hash):
transport_classes = [Car, Bike]
#transports = transport_classes.map { |transport_class|
[transport_class, transport_class.all]
}
# this returns a nested array:
# [
# [Car, [#<Car id:1>, #<Car id:2>]],
# [Bike, [#<Bike id:1>, #<Bike id:2>]
# ]
In your view:
<% #transports.each do |transport_class, transport_items| %>
<h1><%= transport_class.to_s.pluralize.titleize %></h1>
<% transport_items.each do |transport_item| %>
<%= transport_item.name %><br>
<% end %>
<% end %>

Resources