Ruby on Rails
Tuesday, October 21, 2014
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
On 2014-Oct-21, at 17:16 , Rob Biedenharn <rob@agileconsultingllc.com> wrote:
On 2014-Oct-21, at 16:54 , Dominic Monaco <jarvis345@gmail.com> wrote: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 controllerclass LocationsController < ApplicationControllerbefore_action :set_location, only: [:show, :edit, :update, :destroy]# GET /locations# GET /locations.jsondef index@location = Location.newend# GET /locations/1# GET /locations/1.jsondef showend# GET /locations/newdef newend# POST /locations# POST /locations.jsondef 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])endprivate# Never trust parameters from the scary internet, only allow the white list through.def location_paramsparams.require(:location).permit(:address)endendHere 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']}" %>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:With:@shops = @client.spots_by_query('coffee near #{@location(params[:address])}')@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 Cincinnatiso that should get you going again.-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-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/C7D8086A-6B34-4002-8D67-A555010E2F33%40agileconsultingllc.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment