简体   繁体   English

Ruby on Rails Geocoder宝石。 列出附近搜索返回的令人困惑数据的结果

[英]Ruby on Rails Geocoder Gem. List results of nearby search returning confusing data

I am trying to build an app with Rails that allows users to search for job listings using geographic locations (addresses to be specific). 我正在尝试使用Rails构建一个应用程序,该应用程序允许用户使用地理位置(具体地址)搜索工作清单。 I'm using the 'geocoder' gem. 我正在使用“地理编码器” gem。

gem 'geocoder' gem'geocoder'

I can successfully search using the rails console using Job.near('sydney',10) and it returns a listing from my database in an array. 我可以使用Job.near('sydney',10)在rails控制台中成功搜索,它会以数组形式从数据库中返回列表。

2.2.2 :001 > Job.near('sydney', 10 )
  Job Load (97.5ms)  SELECT jobs.*, 6371.0 * 2 * ASIN(SQRT(POWER(SIN((-33.8688197 - jobs.latitude) * PI() / 180 / 2), 2) + COS(-33.8688197 * PI() / 180) * COS(jobs.latitude * PI() / 180) * POWER(SIN((151.2092955 - jobs.longitude) * PI() / 180 / 2), 2))) AS distance, MOD(CAST((ATAN2( ((jobs.longitude - 151.2092955) / 57.2957795), ((jobs.latitude - -33.8688197) / 57.2957795)) * 57.2957795) + 360 AS decimal), 360) AS bearing FROM "jobs" WHERE (jobs.latitude BETWEEN -33.958751860591875 AND -33.77888753940813 AND jobs.longitude BETWEEN 151.10098469477293 AND 151.31760630522706 AND (6371.0 * 2 * ASIN(SQRT(POWER(SIN((-33.8688197 - jobs.latitude) * PI() / 180 / 2), 2) + COS(-33.8688197 * PI() / 180) * COS(jobs.latitude * PI() / 180) * POWER(SIN((151.2092955 - jobs.longitude) * PI() / 180 / 2), 2)))) BETWEEN 0.0 AND 10)  ORDER BY distance ASC
 => #<ActiveRecord::Relation [#<Job id: 73, title: "Dog walkingn", description: "walk my dog everyday", price: "$25", user_id: "24", timeframe: "daily", created_at: "2016-07-29 05:50:00", updated_at: "2016-07-29 05:50:00", location: nil, job_type_id: "3", street_number: "100", street: "Pitt Street", suburb: "Sydney", state: "NSW", postcode: "2000", country: "Australia", longitude: 151.2092955, latitude: -33.8688197>]>

But when I do this in controller and try and send the data to the view (jobs.html.erb), all I get is : 但是,当我在控制器中执行此操作并尝试将数据发送到视图(jobs.html.erb)时,我得到的只是:

#<Job::ActiveRecord_Relation:0x007fd66475abd8>

here is my controller code: 这是我的控制器代码:

def location 定义位置

if params[:location].present?
  jobs = Job.near(params[:location],
   params[:proximity])

  @jobs = jobs
else
  @noJobs = 'there is nothing nearby'
  @jobs = nil
end

end 结束

and my view only consists of: (jobs.html.erb) 而我的视图仅包含:(jobs.html.erb)

 <%=  @jobs %>       

if i run 如果我跑

<%=@jobs[0].title%>

in the view it just returns 在视图中它只是返回

ActiveRecord::StatementInvalid in Jobs#location

can anyone explain to me what is going on and how i can access the data? 谁能向我解释发生了什么以及如何访问数据? and what the active record relation actually means? 活动记录关系的实际含义是什么?

I'm still pretty new to coding, and this has completely stumped me. 我对编码还是很陌生,这完全让我感到困惑。 I cant find the answer anywhere. 我在任何地方都找不到答案。 Really appreciate any help, Thanks! 非常感谢您的帮助,谢谢!

As you can see, @jobs is a ActiveRecord::Relation object, which is essentially an Array of ActiveRecord objects. 如您所见, @jobs是一个ActiveRecord::Relation对象,它实际上是一个ActiveRecord对象的数组。 To display them in the view, you will have to use some sort of a loop, most likely an each : 要在视图中显示它们,您将必须使用某种循环,最有可能的是each循环:

<% @jobs.each do |job| %>
  <%= job %>
<% end %>

Also, <%= job %> alone might not do the trick. 另外,仅<%= job %>可能无法解决问题。 It will return something like a object with it's id. 它将返回类似于其ID的对象。 You will need to call job.title or some other attribute/method on the object, so you get the actual value in the view: 您将需要在对象上调用job.title或其他属性/方法,以便在视图中获得实际值:

<% @jobs.each do |job| %>
  <%= job.title %>
<% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM