Add marker to center location with gmaps4rails - ruby-on-rails

I have an application where I'm showing a business listing and then displaying various jobs related to that business. The job locations are working great but I'd like to add a marker for the business which should be in the center which I can then customise.
Here is my controller/business_controller
def show
#business = Business.find(params[:id])
#distance = #business.service_area
#jobs = Job.near(#business.location, #distance, {:order => :distance, :units => :km})
#json = #jobs.to_gmaps4rails
#mapoptions = {
"map_options" => {"center_latitude" => #business.latitude,
"center_longitude" => #business.longitude,
"auto_zoom" => true}
}
respond_to do |format|
format.html
end
end
In my view views/businesses/show
<p><%= gmaps(:map_options => #mapoptions,"markers" => { "data" => #json }) %></p>
How can I create a marker for the business location?

I'd say:
markers_array = JSON.parse #jobs.to_gmaps4rails
markers_array << {lat: #business.latitude, lng: #business.longitude}
#json = markers_array.to_json

Related

Place a link within a infowindow using gmaps4rails

I am using gmaps4rails and now when I click on a marker appears information from the database, I need a putting a link inside the marker. How do I?
Model:
def gmaps4rails_address
city
end
def gmaps4rails_infowindow
"<h4>#{city}</h4>"
end
Controller:
def index
#postos = Posto.all
#markers = Posto.all.to_gmaps4rails
#json = Posto.all.to_gmaps4rails do |posto, marker|
marker.json "\"id\": #{posto.id}"
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: #postos }
end
end
I don't recommend you use the gmaps4rails_infowindow method: view details shouldn't be given at the model layer.
You should rather configure the infowindow in the controller, using a partial:
#json = Posto.all.to_gmaps4rails do |posto, marker|
marker.infowindow render_to_string(:partial => "/path_to/your_template", :locals => { needed_locales })
end
Details are in the gem's wiki. (you could even use js templates but it's not the question and it's explained in the wiki as well)
Here are two ways.
Directly in the controller:
hashes = Gmaps4rails.build_markers(collection) do |item, marker|
marker.infowindow(ActionController::Base.helpers.link_to(item.name ||= 'Name?',preplan_path(item)).html_safe)
marker.title item.name
marker.picture({
# :url => "/assets/building_icon.png",
:url => "/assets/text.png",
:width => 32,
:height => 32
})
marker.lat item.latitude
marker.lng item.longitude
end
And using a partial from the controller:
hashes = Gmaps4rails.build_markers(collection) do |item, marker|
marker.infowindow render_to_string(:partial => "/structures/info_window", :locals => { :structure => item})
And the partial can be whatever you like:
# view/structures/_info_window.html.haml
= link_to structure.name, [structure.preplan, structure]
- unless structure.longitude.nil?
%br
= link_to "Drive to?", "https://maps.google.com/maps?daddr=#{structure.latitude},#{structure.longitude}", target: "_blank"

Rails GET not translating slugged id with extra parameters

I have a model which is linking to the show method:
<%= link_to "View Others", :controller => "browse",
:action => "show", :id => #id, :d => "25" %>
Clicking the link gives:
http://localhost:3000/browse/santa-cruz?d=25
Rails gives the error though:
No route matches {:controller=>"browse", :action=>"show", :id=>nil, :d=>"25"}
If I take the extra parameter off everything works.
<%= link_to "View Others", :controller => "browse",
:action => "show", :id => #id %>
Goes to http://localhost:3000/browse/santa-cruz
This page loads and I am getting the correct params[:id]
Any ideas?
Pasted bellow is the show for my controller
def show
if params[:d].nil? then
# Show list of addresses in city.
addresses = Address.where(:slug => params[:id])
profile = []
addresses.each do |ad|
profile << ad.profile
end
unless profile.blank?
#profile = Kaminari.paginate_array(profile).page(params[:page]).per(5)
#title = "Profiles Near " + addresses.first.city
#id = params[:id]
else
redirect_to :controller => 'pages', :action => 'notlaunched', :zip => params[:id]
end
else # :d exists
# show all within :d miles.
addresses = Address.where(:slug => params[:id])
nearby = Address.near("#{addresses.first.fulladdress}", params[:d], :order => :distance)
profiles = nearby.map{ |ad| ad.profile }.uniq
end
end
Here's the Index:
def index
cities = []
states = []
Address.find_each do |ad|
cities << { :city => ad.city, :slug => ad.slug } # slug becomes the :id of show
states << ad.state
end
#cities = cities.uniq
#states = states.uniq
#title = "Browse Cities"
end

Filtering values of index page on values defined in drop down box.rails 3

I am a newbie with rails and I am trying to fliter my index page on values selected by drop down box on index page
For Eg .In my index page I am having a drop down box showing employee names if user selects a value from drop down list the values of index page should filter with that employee name.
Note- Te Employee name is a cross reference field
My Controller Look like
def index
#complaints = Complaint.paginate(:page => params[:page], :per_page => 10)
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #complaints }
end
end
My Index View Looks like
<%= select("employee", "employee_id", Employee.all.collect {|p| [ p.fullname, p.id ] }, { :include_blank => true }) %>
I have tried to answer with whatever I can understand from your question and
I am asssuming u dont want filtering through an ajax call and your complaint table consists of a column named employee_id.
In your index_view add
<%= form_tag 'controllers_index_path' , :method => "get", :id=> 'filter_employees_form' do %>
<p>
<%= select_tag 'employee_id', options_for_select(Employee.all.collect {|p| [p.fullname, p.id ] }, :selected => params[:employee_id]), :prompt => 'Select', :id => 'filter_employees' %>
</p>
<% end %>
Add the following code in the javascript file or add it at the end of your index page.
$(document).ready(function(){
$('#filter_employees').change(function(){
$('#filter_employees_form').submit();
})
})
In controller.rb
def index
#complaints = Complaint.get_complaints(params).paginate(:page => params[:page], :per_page => 10)
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #complaints }
end
end
In complaint.rb(model)
def self.get_complaints(params)
conditions = ['']
conditions = ['complaints.employee_id = ?', params[:employee_id]] if params[:employee_id]
self.where(conditions)
end
Hope this is what you are looking for.

How to get gateway response from model into controller - Ruby on Rails

My application uses activemerchant to process payments. I'm using Eway as my payment gateway. I'm storing credit card details with Eway to keep them out of my application database.
I'm using a method store which returns a response with a customer billing id that I can use at a later time to process the order.
http://rdoc.info/github/Shopify/active_merchant/master/ActiveMerchant/Billing/EwayManagedGateway
My main issue is how do I get the response value into my controller so I can save it to the member model.
I've created a simple ruby file to test this all works and it does. I just need to convert this code to work inside my rails app.
require "rubygems"
gem 'activemerchant', '1.15.0'
require 'activemerchant'
ActiveMerchant::Billing::Base.mode = :production
gateway = ActiveMerchant::Billing::EwayManagedGateway.new(
:login => '12345678',
:username => 'mylogin#example.com',
:password => 'mypassword'
)
credit_card = ActiveMerchant::Billing::CreditCard.new(
:type => "visa",
:number => "4444333322221111",
:verification_value => "123",
:month => "11",
:year => "2011",
:first_name => "John",
:last_name => "Citizen"
)
options = {
:order_id => '1230123',
:ip => "127.0.0.1",
:email => 'john.citizen#example.com',
:billing_address => { :title => "Mr.",
:address1 => '123 Sample Street',
:city => 'Sampleville',
:state => 'NSW',
:country => 'AU',
:zip => '2000'
},
:description => 'purchased items'
}
if credit_card.valid?
response = gateway.store(credit_card, options)
if response.success?
puts "Credit Card Stored. #{response.message}"
customer = response.params['CreateCustomerResult']
puts "Customer Id: #{customer}"
else
puts "Error: #{response.message}"
end
else
puts "Error, credit card is not valid. #{credit_card.errors.full_messages.join('. ')}"
end
Here is the relevant code in my order model.
serialize :params
cattr_accessor :gateway
def response=(response)
self.success = response.success?
self.message = response.message
self.params = response.params
self.billing_id = response.params['CreateCustomerResult']
rescue ActiveMerchant::ActiveMerchantError => e
self.success = false
self.message = e.message
self.params = {}
self.billing_id = nil
end
def store
response = Order.gateway.store(credit_card, options)
end
Here is my order controller create code.
def create
#member = current_member
#order_deal = Deal.find(params[:deal_id])
#order = #order_deal.orders.build(params[:order])
#order.member_id = current_member.id
#order.ip_address = request.remote_ip
#deal = #order_deal
if #order.save
if #order.store
render :action => "success"
flash[:notice] = "Successfully processed your order."
else
render :action => "new"
end
else
render :action => 'new'
end
end
So essentially I want to get the
response.params['CreateCustomerResult']
and add it to my member model under
member.billing_id = response.params['CreateCustomerResult]
How can I do this?

geocode my location and display with google-maps-for-rails gem

i want to geocode my location and display with google-maps-for-rails gem
view:
= gmaps({ "map_options" => { "auto_adjust" => true } })
= form_tag geocode_geo_locations_path, :method => :get, :remote => true do
= text_field_tag 'address'
= submit_tag 'search'
in my controller i have:
def geocode
#location = Gmaps4rails.geocode(params[:address])
if #location
#location_hash = #location.map { |loc| {:longitude => loc[:lng], :latitude => loc[:lat], :address => loc[:matched_address]} }.first
end
end
and in the view geocode.js.erb
Gmaps4Rails.replace_markers(<%=raw #location_hash.to_json %>);
it work so far, that i get a marker on the map, however the map doesnt get auto_adjusted .
how can i accomplish that??
Ok... Indeed you just noticed a bug :)
Corrected now in 0.8.2.
Thanks!

Resources