Google Maps for Rails info window example - gmaps4rails

Maybe it's a case of the Mondays but I'm having a really difficult time with infowindows and the Google Maps for Rails gem. Does anyone know of a tutorial or example?
All I want to do is set up a default infowindow to open when you click on a marker. I've gathered that I need to make a partial and set the options in the map but I just can't seem to bring it all together.
Thanks!

Nevermind, it finally clicked. Here's my code for a basic example, hopefully it will help someone else in the future.
location model
class Location < ActiveRecord::Base
include Rails.application.routes.url_helpers
default_scope order('locations.id ASC')
acts_as_gmappable
attr_accessible :name,
:address,
:city,
:province,
:postal_code,
:country,
:phone,
:ext,
:phone_alt,
:ext_alt,
:latitude,
:longitude
geocoded_by :address
validates_presence_of :name
validates_presence_of :address
validates_presence_of :city
validates_presence_of :province
validates_presence_of :postal_code
validates_presence_of :country
after_validation :geocode, :if => :address_changed?
def gmaps4rails_address
#describe how to retrieve the address from your model, if you use directly a db column, you can dry your code, see wiki
"#{self.address}, #{self.city}, #{self.country}"
end
end
location controller
class LocationsController < ApplicationController
def show
#location = Location.find(params[:id])
#json = #location.to_gmaps4rails do |location, marker|
marker.infowindow render_to_string(:partial => "/layouts/partials/infowindow", :locals => { :location => location})
end
respond_to do |format|
format.html
end
end
end
infowindow partial (haml)
.location-data{id: location.id}
.location-name
= location.name.capitalize
.location-address
= location.address.capitalize
.location-city= location.city.capitalize
.location-province
= location.province.capitalize
.location-postal-code
= location.postal_code
.location-country
= location.country
.location-phone
= location.phone
.location-extension
= location.ext
.location-alt-phone
= location.phone_alt
.location-alt-phone-extension
= location.ext_alt
show view (haml)
#map-column
%h1
Find a retailer near you
= gmaps("markers" => {"data" => #json, "options" => {"link_container" => "map_link_" } })

Related

Limiting how often a user can post on a particular person's profile/wall in Rails

How can I limit a user to only to being able to post once or twice per day on a particular users's wall? I primarily want to do it in order to limit spam. My code for the wall, models, view, and controllers are below. I don't really know how to go about it as I'm new to rails but I know there is something time.now. I'm not exactly sure how to implement such a feature.
Class UsersController < ApplicationController
def show
#user = User.find(params[:id])
#first_name = #user.first_name
#last_name = #user.last_name
#wallpost = WallPost.new(params[:wall_post])
#showwallposts = #user.received_wallposts
end
def create
#wallpost = WallPost.create(params[:wall_post])
end
models
class WallPost < ActiveRecord::Base
attr_accessible :content, :receiver_id, :sender_id
belongs_to :receiver, :class_name => "User", :foreign_key => "receiver_id"
belongs_to :sender, :class_name => "User", :foreign_key => "sender_id"
end
class User < ActiveRecord::Base
has_many :sent_wallposts, :class_name => 'WallPost', :foreign_key => 'sender_id'
has_many :received_wallposts, :class_name =>'WallPost', :foreign_key => 'receiver_id'
in the view
<%= form_for(#wallpost, :url => {:action => 'create'}) do |f| %>
<%= f.hidden_field :receiver_id, :value => #user.id %>
<%= f.hidden_field :sender_id, :value => current_user.id %>
<%= f.text_area :content, :class => 'inputbox' %>
<%= f.submit 'Post', class: 'right btn' %>
<% end %>
You could create a custom validator which assures maximum DAILY_LIMIT posts have been created on that person's wall that day by that user:
class SpamValidator < ActiveModel::Validator
DAILY_LIMIT = 2
def validate(record)
if similar_posts_today(record).count >= DAILY_LIMIT
record.errors[:spam_limit] << 'Too many posts today!'
end
end
def similar_posts_today(record)
WallPost.where(receiver: record.receiver, sender: record.sender)
.where("DATE(created_at) = DATE(:now)", now: Time.now)
end
end
Then add that validation to your WallPost model:
validates_with SpamValidator
Then it will fail with a validation error when trying to create a wall post beyond the limit set in the constant. You need to handle this case in the create action in your controller. A simple (but not optimal in terms of user experience) way of handling this is:
def create
#wallpost = WallPost.new(params[:wall_post])
flash[:error] = "You've reached the daily posting limit on that wall." unless #wallpost.save
redirect_to user_path(#wallpost.receiver)
end
With that, it'll try to save the new wall post, if it is unable to, it'll set flash[:error] to the error message above. You'd need to show this on your show.html.erb page with <%= flash[:error] if flash[:error] %>.

Polymorphic Comments with Ancestry Problems

I am trying to roll together two Railscasts: http://railscasts.com/episodes/262-trees-with-ancestry and http://railscasts.com/episodes/154-polymorphic-association on my app.
My Models:
class Location < ActiveRecord::Base
has_many :comments, :as => :commentable, :dependent => :destroy
end
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end
My Controllers:
class LocationsController < ApplicationController
def show
#location = Location.find(params[:id])
#comments = #location.comments.arrange(:order => :created_at)
respond_to do |format|
format.html # show.html.erb
format.json { render json: #location }
end
end
end
class CommentsController < InheritedResources::Base
def index
#commentable = find_commentable
#comments = #commentable.comments.where(:company_id => session[:company_id])
end
def create
#commentable = find_commentable
#comment = #commentable.comments.build(params[:comment])
#comment.user_id = session[:user_id]
#comment.company_id = session[:company_id]
if #comment.save
flash[:notice] = "Successfully created comment."
redirect_to :id => nil
else
render :action => 'new'
end
end
private
def find_commentable
params.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
end
end
nil
end
end
In my locations show view I have this code:
<%= render #comments %>
<%= render "comments/form" %>
Which outputs properly. I have a _comment.html.erb file that renders each comment etc. and a _form.html.erb file that creates the form for a new comment.
The problem I have is that when I try <%= nested_comments #comments %> I get undefined method 'arrange'.
I did some Googling and the common solution to this was to add subtree before the arrange but that throws and undefined error also. I am guessing the polymorphic association is the problem here but I am at a loss as to how to fix it.
Dumb mistake... forgot to add the ancestry gem and required migration which I thought I had already done. The last place I checked was my model where I eventually discovered my error.

Multiple (n) identical nested forms generated square-times(n*n) when validation fails

User has two addresses shipping(:address_type=0) and billing(:address_type=1)
User form with 2 classic nested forms for each address type are generated square times every submit and failed validation.
Models:
class User < ActiveRecord::Base
has_many :addresses, :dependent => :destroy
accepts_nested_attributes_for :addresses
validates_associated :addresses
end
class Address < ActiveRecord::Base
belongs_to :user
validates :user, :address_type, :first_name, :last_name, :street
end
Controller
class UsersController < ApplicationController
public
def new
#user = User.new
#shipping_address = #user.addresses.build({:address_type => 0})
#billing_address = #user.addresses.build({:address_type => 1})
end
def create
#user = User.new(params[:user])
if #user.save
#fine
else
render => :new
end
end
Uncomplete Form
=form_for #user, :html => { :multipart => true } do |ff|
=ff.fields_for :addresses, #shipping_address do |f|
=f.hidden_field :address_type, :value => 0
=ff.fields_for :addresses, #billing_address do |f|
=f.hidden_field :address_type, :value => 1
=ff.submit
The form should look like this:
=form_for #user, :html => { :multipart => true } do |ff|
=ff.fields_for :addresses do |f|
Nothing else.
Addressess is already a collection, so you should have just one rendering of it.
Also that ":addresses, #shipping_address" makes it to render addresses AND shipping address, even if it's included in #user.addresses.
The addressess built in new action will show there because they are in the addresses collection.
EDIT:
If you need only these two addresses, you can sort it and pass it to fields_for directly:
=form_for #user, :html => { :multipart => true } do |ff|
=ff.fields_for ff.object.addresses.sort{|a,b| a.address_type <=> b.address_type } do |f|
That should do it.
Surprised? I guess not but I was. I found it am I correct? And its stupid and simple.
There is no #shipping_address nor #billing_address when validation fails and rendering the new action (the form) again. But #user has already 2 addresses builded and nested form behave correctly to render each twice for first time failed validation.
def create
#user = User.new(params[:user])
if #user.save
#fine
else
#user.addresses.clear
#user_address = #user.addresses.build({:address_type => 0})
#user_address.attributes = params[:user][:addresses_attributes]["0"]
#billing_address = #user.addresses.build({:address_type => 1})
#billing_address.attributes = params[:user][:addresses_attributes]["1"]
render => :new
end
end

Configuring Rails 3 + Polymorphic Image model + Paperclip and Amazon S3, No errors, but nothing uploading

I suspect the problem lies in the way I am creating the polymorphic image attribute. I am using fields_for in the form.
In this case a user can create a post and add an image using paperclip, storing it with S3.
I am using a polymorphic image model "post_image":
class PostImage < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
#.merge(PAPERCLIP_OPS)
has_attached_file :image, :styles => { :medium => "200x200>", :thumb => "50x50>" },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => "/:style/:id/:filename",
:bucket => "zounds-dev"
validates_attachment_presence :image
validates_attachment_size :image, :less_than => 5.megabytes
end
Post Model:
class Post < ActiveRecord::Base
has_many :post_images, :as => :imageable, :dependent => :destroy
.
.
.
accepts_nested_attributes_for :post_images, :reject_if => lambda { |t| t[:post_image].nil?}, :allow_destroy => true
end
New Post Form:
=form_for( setup_post(#post,current_user), :html => { :multipart => true}) do |f|
%dl
=f.fields_for :post_images do |ff|
=ff.file_field :image
%dt.field=f.label :name
%dd.field=f.text_field :name
%dt.field=f.label :description
%dd.field=f.text_area :description
=f.fields_for :user do |u|
=render "user_fields", :f => u
=f.fields_for :assignments do |ff|
=ff.check_box :_destroy, {:checked => ff.object.persisted?}, '0','1'
=ff.label :_destroy, ff.object.group.name
=ff.hidden_field :group_id
.action=f.submit "Save Post"
The setup_post helper method used in the Post form_for: (the groups stuff isn't relevant here)
def setup_post(post, current_user)
groups = current_user.groups_as_owner + current_user.groups_as_member
(groups - post.groups).each do |group|
post.assignments.build(:group => group)
end
post.assignments.sort_by {|x| x.group.name }
post_image = post.post_images.build
post
end
Post controller:
def new
#user = User.find(params[:user_id])
# #post = #user.posts.build
#post = Post.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #post }
end
end
def create
#user = current_user
#post = #user.posts.build(params[:post])
.
.
.
end
I suspect the problem is that I am using fields_for for the post_image attribute, but I've looked all over and can't figure out what the proper way to implement a polymorphic nested image attribute is.
I also did the s3sh amazon s3 console thing, and although I couldn't upload an image because I couldn't figure out how to pass in the right image path to the open() function, I connected to S3. My s3.yml file is set up correctly as well.
Thanks yall,
Brian
The issue was with the reject_if in accepts_nested_attributes for the Post model
accepts_nested_attributes_for :post_images, :reject_if => lambda { |t| t[:post_image].nil?}, :allow_destroy => true
commenting it out fixed the issue.

Querying for a relationship in ruby on rails and update results via Ajax

I have a ROR app that has many players, and many proposed games. The games display on a feed and a player can decide to hide them from this feed. The hidden function works like this:
in player.rb:
has_many :hides, :foreign_key=> "hider_id",
:dependent => :destroy
has_many :hidees, :through => :hides
def hidden?(hidee)
hides.find_by_hidee_id(hidee)
end
def hide!(hidee)
hides.create!(:hidee_id => hidee.id)
end
def unhide!(hidee)
hides.find_by_hidee_id(hidee).destroy
end
hides_controller.rb
class HidesController < ApplicationController
def create
#game = Game.find(params[:hide][:hidee_id])
current_profile.hide!(#game)
redirect_to :back
end
def destroy
#game = Hide.find(params[:id]).hidee
current_profile.unhide!(#game)
redirect_to :back
end
end
hide.rb
class Hide < ActiveRecord::Base
attr_accessible :hidee_id
belongs_to :hider, :class_name => "Player"
belongs_to :hidee, :class_name => "Game"
validates :hider_id, :presence => true
validates :hidee_id, :presence => true
end
game.rb
has_many :reverse_hides, :foreign_key => "hidee_id",
:class_name => "Hide",
:dependent => :destroy
has_many :hiders, :through => :reverse_hides
routes.rb
resources :games do
member do
post :publish
post :unpublish
get :view
get :hidees, :hiders
end
I'm trying to do two things: 1. Write a function that would allow me to hide a game from the feed if a relationship between hidden relationship between game and player exits, and 2. write a "show hidden" button that would allow me to return all projects that were "hidden" by the player.
So far with part 1. I have the following code in the view, and while this does the trick in terms of setting up the relationships, it does not "hide" the game from the feed--I'm guessing I would need ajax for that??
- if current_profile.hidden?(game)
= form_for current_profile.hides.find_by_hidee_id(game), :html => { :method => :delete } do |f|
= f.submit "Unhide", :title => "Unhide this game."
- else
= form_for current_profile.hides.build(:hidee_id => game.id) do |f|
= f.hidden_field :hidee_id
= f.submit "Hide", :title => "Hide this game"
Thank you so much for viewing this, I know it's quite long, but I would appreciate any help you could offer. Also, thank you for you time.