我有一个帖子资源,属于在注册时成功使用Geocoder GEM使用地理编码的海报(用户类型)。现在我希望用户能够通过距离搜索它们附近的帖子。对于Rails来说,我对rails相对较新的,任何帮助都会受到极大的赞赏。

我得到以下错误(我真的不知道它的意思是什么,我认为在我的模型中设置关联足够)

Cannot visit ActiveRecord::Relation::ActiveRecord_Relation_Poster
.

index.html.erb

<%= form_tag posts_path, method: :get do %> 
Find postings <%= select_tag "within", options_for_select([["Next door", 0.001],["In your  neighborhood", 0.05], ["Nearby", 0.1]], ["Nearby", 0.1]) %>
<%= submit_tag "Find", :name => nil %>
<% end %>
.

posts_controller.erb

  def index
if params[:within]
  @posts= Post.where(Poster.near(current_user, params[:within].to_f))
else
 @posts=Post.all
 end
end
.

帖子模型

class Post < ActiveRecord::Base
belongs_to :poster
end
.

海报模型

class Poster < User
has_many :posts, dependent: :destroy   
end
.

用户模型

class User < ActiveRecord::Base
 def full_address
[address_one, city, state, zip].compact.join(', ')
 end 

geocoded_by :full_address, :latitude => :lat, :longitude => :long
after_validation :geocode, if: -> (full_address){ full_address.present? or     address_changed? }
end
.

有帮助吗?

解决方案

我刚刚结束了在帖子的创建时传递属性,我希望有一种方法来解决我的原始问题(使数据库较少冗余)

在我的帖子控制器中

def create
 @post = current_user.posts.build(post_params)
 @post.assign_attributes(:lat => current_user.lat, :long => current_user.long)
end

def index
if params[:within]
 @posts= Post.near(current_user, params[:within].to_f)
else
 @posts=Post.all
 end
end
. 我的帖子模型中也使用了“地理编码:海报”>

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top