Help with small rails app

55 views
Skip to first unread message

Dominic Monaco

unread,
Oct 21, 2014, 4:54:31 PM10/21/14
to rubyonra...@googlegroups.com
Hey guys I'm completely lost at this point I'm trying to make a simple app that takes an input in the form of a string ("City, State") and sends that string to the google_places gem to do spot_by_query("gas station near #{enter value from input}")

Then I need the 1st 10 results and be able to print out their names.  I have become completely lost in my attempts and was looking for some advice. 

Here is my controller
class LocationsController < ApplicationController
  before_action :set_location, only: [:show, :edit, :update, :destroy]

  # GET /locations
  # GET /locations.json
  def index
    @location = Location.new
  end

  # GET /locations/1
  # GET /locations/1.json
  def show

  end

  # GET /locations/new
  def new

  end

  # POST /locations
  # POST /locations.json
  def create
    @client = GooglePlaces::Client.new('################')

    @shops = @client.spots_by_query('coffee near #{@location(params[:address])}')
    @result = JSON.parse(@shops[1])
    # @location.find_closest_shops(@location.params[:address])
  end

  private
    # Never trust parameters from the scary internet, only allow the white list through.
    def location_params
      params.require(:location).permit(:address)
    end
end


Here is the input form
<%= form_for @location do |f| %>
  <%= f.label :address %>
  <%= f.text_field :address, :placeholder => "City State" %>
  <br>
  <%= f.submit "Find Coffee!" %>
<% end %>

Here is the output form (this is where I'm lost)
<%= print "#{@result['name']}" %>


Jason Fleetwood-Boldt

unread,
Oct 21, 2014, 5:15:36 PM10/21/14
to rubyonra...@googlegroups.com
Dominic,

It sounds like you want to write some pseudo-code on the rails console. Do you know how to use the rails console?

Figure out how to use the Rails console and then execute what yo've got in your create method line-by-line

That should let you poke around the results of the GooglePlaces object and see how it works. You should be able to replicate just about anything on the console, and it sounds to me like your grey-areas around Rails itself are getting in the way of you learning how the GooglePlaces object works.

If you have questions that are specific to the Google places gem you are using, I recommend you open an issue or ask on the support forum dedicated to the Google places gem. This list is generally for stuff related to the framework of Rails itself (although we do often point people in the right direction if they ask about common gems).

-Jason





--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/7099011d-e18c-4836-8bed-f68aad67cc6c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

----

Jason Fleetwood-Boldt

All material © Jason Fleetwood-Boldt 2014. Public conversations may be turned into blog posts (original poster information will be made anonymous). Email ja...@datatravels.com with questions/concerns about this.

Rob Biedenharn

unread,
Oct 21, 2014, 5:16:41 PM10/21/14
to rubyonra...@googlegroups.com
Well, after making a few assumptions:
  • you are going to a URL that gets routed to the index action like /locations
  • the form posts to /locations

In your create action, you probably need to replace:
    @shops = @client.spots_by_query('coffee near #{@location(params[:address])}')
With:
    @shops = @client.spots_by_query("coffee near #{location_params[:location][:address])}")
Noting that you won't have an @location set (it will be nil because all instance variables are nil by default).
You need to actually get the params which will look something like: { location: { address: "entered value" } }
You can probably see this in the development.log.
In Ruby, single-quoted strings do not do interpolation. Thus:
here = "Cincinnati"

puts 'coffee near #{here}'
coffee near #{here}

but:
puts "coffee near #{here}"
coffee near Cincinnati

so that should get you going again.

-Rob

Rob Biedenharn

unread,
Oct 21, 2014, 5:20:33 PM10/21/14
to rubyonra...@googlegroups.com
Oh, and:
<%= print "#{@result['name']}" %>

can be simply:
<%= @result['name'] %>

The print output might end up in the log, but certainly won't be in the generated HTML.

And the result of what's inside <%= ... %> will be turned into a String (with .to_s if needed) and replace that bit of the template.

-Rob

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

Dominic Monaco

unread,
Oct 21, 2014, 7:31:59 PM10/21/14
to rubyonra...@googlegroups.com
Ok so I figured out what I did wrong sorta, the way the call was being made didnt work so I did this
  def create
    @shops = []
    @client = GooglePlaces::Client.new('AIzaSyCC5Dyjn5_gGSKZMVJqF6tKOYOaxgUMJ8s')
    @shops = @client.spots_by_query('coffee near ' + @location.address)
  end

now my next (and I think last problem) is in my input form I assumed that was setting @location's address to w/e the string in the input field is but it's saying it's nil??

in my index I'm calling @location = Location.new, then I thought I was setting @location.address to "input value from form", what am I missing?

Dominic Monaco

unread,
Oct 22, 2014, 8:41:03 AM10/22/14
to rubyonra...@googlegroups.com
Fixed it, needed to save it

added @location = Location.create(location_params) to create controller
Reply all
Reply to author
Forward
0 new messages