tutoto tutoto - 7 months ago 21
Ruby Question

Restriction for post to the map display

i'm currently building a web app, and I encounters some problems.
I want the map display posts that are pushed (so push == true). For that, i'd try a lot of conditions, but it's never working. I feel I am not far from the goal, but for now, the map no reference points.

posts 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





views/posts/index :



<div class="title text-center">
<h1>Alors ? On mange quoi ?</h1>
</div>

<br>


<%= render 'map' %>

<%= render 'post' %>





& views/posts/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>





So if you have any suggestions about that, you're welcome !!

Answer

For this stage you should create a scope on model post

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

and then in 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
Comments