I can add a new "Location" from the console, but can not add from the views

时间:2017-04-24 17:21:33

标签: ruby-on-rails

Don't know what I broke. I can add a new "location" from the console, but from the views it does not work. Does anyone see what I don't see? When a new location is created, the address is entered in and it is auto geolocated and a marker is placed on the map.

Log OutPut:

Started GET "/locations/new?utf8=%E2%9C%93&authenticity_token=QY4efCl8XNUWE7Z8%2FmYlm8w9XIZ1EHDClLALbXGEBteppx2Ved%2BleeKywFOg6uSOAh%2FOK8PtqQjdu69sfedAHQ%3D%3D&location%5Blocation_name%5D=Test+Location+Name&location%5Blocation_description%5D=Test+Description&location%5Blocation_address%5D=2600+Georgia+St.+Vallejo%2C+CA+94591&commit=Create+New+Location" for ::1 at 2017-04-24 12:27:39 -0700 Processing by LocationsController#new as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"QY4efCl8XNUWE7Z8/mYlm8w9XIZ1EHDClLALbXGEBteppx2Ved+leeKywFOg6uSOAh/OK8PtqQjdu69sfedAHQ==", "location"=>{"location_name"=>"Test Location Name", "location_description"=>"Test Description", "location_address"=>"2600 Georgia St. Vallejo, CA 94591"}, "commit"=>"Create New Location"} User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 12], ["LIMIT", 1]] Rendering locations/new.html.erb within layouts/application Rendered locations/new.html.erb within layouts/application (2.0ms) Completed 200 OK in 48ms (Views: 45.2ms | ActiveRecord: 0.3ms)

Location Model:

2.3.3 :002 > Location => Location(id: integer, location_name: string, location_address: string, location_description: string, lat: float, lng: float, created_at: datetime, updated_at: datetime) 2.3.3 :003 >

Thanks,

routes.rb

Rails.application.routes.draw do
  mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
  devise_for :users
  root "homes#index"


#  Location Routes

  get '/locations', to: 'locations#index', as: 'locations'
  get "/locations/new", to: "locations#new", as: "new_location"
  post '/locations', to: 'locations#create'
  get '/locations/map', to: "locations#map", as: "map"
  get "/locations/:id", to: "locations#show", as: "location"
  get "/locations/:id/edit", to: "locations#edit", as: "edit_location"
  patch "/locations/:id", to: "locations#update"
  delete "/locations/:id", to: "locations#destroy"

# Article Routes

  get '/articles/:id/new', to: 'articles#new', as: 'new_article'
  get '/articles/:id', to: 'articles#show', as: 'article'
  get '/articles', to: 'articles#index', as: 'user_articles'
  post '/articles', to: 'articles#create'
  delete '/articles/:id', to: 'articles#destroy', as:'delete_article'
  get '/articles/:id/edit', to: 'articles#edit', as: 'edit_article'
  put '/articles/:id', to: 'articles#update'
  patch '/articles/:id', to: 'articles#update'

  get '/login', to: 'sessions#new'
  get '/logout', to: 'sessions#destroy'

Locations_Controller

class LocationsController < ApplicationController
      before_action :authenticate_user!

    def new
      @location = Location.new
    end

   def create
     location_params = params.require(:location).permit(:location_name, 
     :location_address, :location_description, :lat, :lng)
     location = Location.new(location_params)
   if location.save
      redirect_to location_path(location)
    end
end

/locations/views/new.html.erb

  <form class="form-inline">
          <%= form_for @location do |f| %>
          <div class="form-group">
          <h3><label>  Name of the Location: <%= f.text_field 
:location_name %></label></h3>
          </div>
          <div class="form-group">
            <label>Description/Discount Offered: <%= f.text_field 
:location_description %></label>
          </div>
          <div class="form-group">
            <label>Street Address: <%= f.text_field :location_address 
%></label>
          </div>
            <%= f.submit "Create New Location" %>
            <% end %>
       </form>

location.map.erb

<div class ="container-fluid">
      <h1>map Page</h1>

        <script src="https://maps.googleapis.com/maps/api/js?key=KEY-
REMOVED-FOR-QUESTION-POSTING">
        </script>

    <div id="map" class="responsive center" style="width: 85%; height: 400px;"></div>

    <script type="text/javascript">

       map = new google.maps.Map(document.getElementById('map'), {
        zoom: 10,
        center: new google.maps.LatLng(37.725685, -122.156830),
      mapTypeId: google.maps.MapTypeId.ROADMAP
      });

      var places = <%= @locations.to_json.html_safe %>
      var infowindow = new google.maps.InfoWindow();
      var marker, i;

      for (i = 0; i < places.length; i++) {

        marker = new google.maps.Marker({
          position: new google.maps.LatLng(places[i].lat, places[i].lng),
          map: map
        });

        google.maps.event.addListener(marker, 'click', (function(marker, i) {
          return function() {
            infowindow.setContent(places[i].location_name + "   |   " + places[i].location_address  + places[i].location_description);
            infowindow.open(map, marker);
          }
        })(marker, i));
      }
    </script>

0 个答案:

没有答案