2016-05-05 6 views
0

私は現在ウェブアプリケーションを構築しており、いくつかの問題が発生しています。 プッシュされている地図の表示記事が必要です(プッシュ== 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=&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>

あなたがそのことについて何か提案を持っているのであれば、あなたは歓迎しています!

答えて

1

この段階のためにあなたが

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 
+0

おかげで多くのことをモデルPost

class Post < ActiveRecord::Base scope :push, ->{ where(push: true).order("push_updated_at DESC") } end 

scopeと、コントローラでを作成する必要があり、それは完璧です! – tutoto

関連する問題