' nilClass'与轨道上的地理编码器4

时间:2015-03-28 04:54:47

标签: ruby-on-rails ruby ruby-on-rails-3 rails-geocoder

我试图在我的rails 4应用中实现geocoder,但我在初始化方面遇到了问题。该应用正确创建,删除,编辑等,但当我尝试在nearbys模板中实施show方法时,我得到NoMethodError in Events#show undefined method `each' for nil:NilClass。当我使用attr_accessible在rails 3上运行时,应用程序可以完美地执行所有操作,但是当我切换到rails 4并更改为强参数时,我收到此错误。我知道这是一件非常简单的事情,我只是看不到我错过的东西。

这是我的event型号:

class Event < ActiveRecord::Base
  belongs_to :user
  validates_presence_of :user
  geocoded_by :address
  after_validation :geocode, :if => :address_changed?
end

这是我的events_controller

class EventsController < ApplicationController
  before_action :authenticate_user!
  load_and_authorize_resource

  def index
    if params[:search].present?
      @events = Event.near(params[:search], 50, :order => :distance)
    else
      @events = Event.all
    end
  end

  def show
  end

  def new
  end

  def edit
  end

  def create
    @event = Event.new(event_params)
    @event.user_id = current_user.id

    respond_to do |format|
      if @event.save
        format.html { redirect_to @event, notice: 'Event was successfully created.' }
        format.json { render :show, status: :created, address: @event }
      else
        format.html { render :new }
        format.json { render json: @event.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @event.update(event_params)
        format.html { redirect_to @event, notice: 'Event was successfully updated.' }
        format.json { render :show, status: :ok, address: @event }
      else
        format.html { render :edit }
        format.json { render json: @event.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @event.destroy
    respond_to do |format|
      format.html { redirect_to events_url, notice: 'Event was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
  def event_params
    params.require(:event).permit(:name, :event_date, :start_time, :end_time, :address, :latitude, :longitude, :description, :user_id)
  end
end

这是我的show模板:

<div id="header"></div>

<p>
  <b>Name:</b>
  <%= @event.name %>
</p>

<p>
  <b>Event date:</b>
  <%= @event.try(:event_date).strftime("%a, %B %d") %>
</p>

<p>
  <b>Start time:</b>
  <%= @event.try(:start_time).strftime("%l:%M%p") %>
</p>

<p>
  <b>End time:</b>
  <%= @event.try(:end_time).strftime("%l:%M%p") %>
</p>

<p>
  <b>Address:</b>
  <%= @event.address %>
</p>

<p>
  <b>Latitude:</b>
  <%= @event.latitude %>
</p>

<p>
  <b>Longitude:</b>
  <%= @event.longitude %>
</p>

<p>
  <b>Description:</b>
  <%= @event.description %>
</p>

<p>
  <b>Organizer:</b>
  <%= @event.user.name %>
</p>

<%= image_tag "http://maps.google.com/maps/api/staticmap?size=450x300&sensor=false&zoom=16&markers=#{@event.latitude}%2C#{@event.longitude}" %>

<h3>Nearby events</h3>
<ul>
  <% for event in @event.nearbys(10) %>
      <li><%= link_to event.address, event %> (<%= event.distance.round(2) %> miles)</li>
  <% end %>
</ul>

<%= link_to 'Edit', edit_event_path(@event) %> |
<%= link_to 'Back', events_path %>

我在show模板(for event in @event.nearbys(10))的第52行收到错误。 我很感激任何帮助!

1 个答案:

答案 0 :(得分:0)

问题在于我的seeds.rb文件。 create操作正确地对:latitude:longitude字段进行了地理编码,但在首次播种数据库时,我没有为这些字段输入值。