gpt4 book ai didi

javascript - 发布到 map 显示的限制

转载 作者:行者123 更新时间:2023-12-03 07:01:23 31 4
gpt4 key购买 nike

我目前正在构建一个网络应用程序,遇到了一些问题。我想要推送的 map 显示帖子(因此推送== true)。为此,我尝试了很多条件,但从来没有奏效。我觉得我离目标不远了,但目前, map 上没有引用点。

帖子 Controller :

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' %>

& View /帖子/ map :

<script src="//maps.google.com/maps/api/js?v=3.18&sensor=false&client=&key=&libraries=geometry&language=&hl=&region="></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

然后在 Controller 中

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

关于javascript - 发布到 map 显示的限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37049754/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com