张贴到地图显示的限制
Restriction for post to the map display
我目前正在构建一个web应用程序,遇到了一些问题。我想要地图显示被推送的帖子(所以推送==true)。为此,我尝试了很多条件,但都不起作用。我觉得我离目标不远了,但现在,地图上没有参考点。
立柱控制器:
class PostsController < ApplicationController
before_action :authenticate_user!
before_action :set_post, only: [:show, :edit, :update, :destroy]
before_action :owned_post, only: [:edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
@posts = Post.all.order("push_updated_at DESC")
if @posts.push == true
@hash = Gmaps4rails.build_markers(@posts) do |post, marker|
marker.lat post.latitude
marker.lng post.longitude
end
end
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
@post = current_user.posts.build
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
@post = current_user.posts.build(post_params)
respond_to do |format|
if @post.save
@post.update(push: false)
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
@post = Post.find(params[:id])
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:user_id, :title, :description, :image, :push, :push_updated_at, ingredients_attributes: [:id, :name, :_destroy])
end
def owned_post
unless current_user == @post.user
flash[:alert] = "That post doesn't belong to you!"
redirect_to root_path
end
end
end
视图/帖子/索引:
<div class="title text-center">
<h1>Alors ? On mange quoi ?</h1>
</div>
<br>
<%= render 'map' %>
<%= render 'post' %>
&视图/帖子/地图:
<script src="//maps.google.com/maps/api/js?v=3.18&sensor=false&client=&key=&libraries=geometry&language=&hl=®ion="></script>
<script src="//google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.14/src/markerclusterer_packed.js"></script>
<script src='//google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.9/src/infobox_packed.js' type='text/javascript'></script> <!-- only if you need custom infoboxes -->
<div style='width: 800px;'>
<div id="map" style='width: 800px; height: 400px;'></div>
<script type="text/javascript">
handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
markers = handler.addMarkers(<%=raw @hash.to_json %>);
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
});
</script>
</div>
</div>
所以,如果你对此有任何建议,欢迎你!!
在此阶段,您应该在型号Post
上创建一个scope
class Post < ActiveRecord::Base
scope :push, ->{ where(push: true).order("push_updated_at DESC") }
end
然后在控制器中
def index
@posts = Post.push #this will fetch the post with push true and sort it
@hash = Gmaps4rails.build_markers(@posts) do |post, marker|
#rest of your code
end
相关文章:
- 地图API没有't显示地图
- 单击按钮以突出显示地图标记 (Gmaps) - 如何
- 谷歌地图API没有;t显示地图,如果中心由javascript函数填充
- 将PHP集成到Javascript中,使用Google API显示地图标记
- IE中的谷歌地图API v3不显示地图或标记
- 数据映射不是't显示地图
- 谷歌地图隐藏并显示地图
- 在谷歌地图中显示地图标记内容的事件
- 映射:单击标记,显示地图旁边的内容
- 谷歌地图没有显示地图上的所有目的地标记,只有第一个和最后一个
- 如何在另一页上显示地图并全屏查看
- 谷歌地图 - 通过使用php检索纬度和纵向来显示地图和标记
- 单击时显示地图,然后滚动到地图
- 在 JavaScript 循环中显示地图标记的 JSON 解析数据
- 如何更新和显示地图对象
- 必应地图显示地图的左上角的北美
- 谷歌地图功能,但不显示地图
- 如何在选择html元素时高亮显示地图标记
- 获取用户's附近没有显示地图与谷歌地图api v3
- 显示地图的“切换街景地图的问题”