我有一个帖子资源,该资源属于一个发帖人(一种用户类型),其地址在注册后已成功使用Geocoder gem进行了地址解析。现在,我希望用户能够按距离搜索附近的帖子。我是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
在我的帖子模型中还使用了“:poster进行地理编码”
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句