vendredi 27 mars 2015

'nilClass' with geocoder on rails 4

I'm trying to implement geocoder in my rails 4 app, but I'm having problems with initialization. The app properly creates, deletes, edits, etc., but when I try to implement the nearbys method in the show template I get a NoMethodError in Events#show with undefined method `each' for nil:NilClass. The app performs everything perfectly when I run it on rails 3 with attr_accessible, but right when I switch to rails 4 and change to strong parameters I get this error. I know it's something super simple, I just can't see what I'm missing.


Here's my event model:



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


Here's my 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


Here's my show template:



<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://ift.tt/1EeGdmH}" %>

<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 %>


I get the error on line 52 of the show template (for event in @event.nearbys(10)). I appreciate any help at all!


Aucun commentaire:

Enregistrer un commentaire