Rails,意外的nil错误

时间:2015-09-19 20:10:39

标签: ruby-on-rails ruby ruby-on-rails-4 each

我对rails非常陌生,而且我正在开发一个应用程序,它根据它们是否处于活动状态,是否有足够的库存以及产品的pickup_location是否接近current_user,在索引视图中显示“产品”。位置”。

以下设置工作了几天,突然开始抛出无方法错误。我已经被困了好几天了。我真的很感激任何有关如何解决这个问题的建议以及可能导致此错误突然出现的问题。

index.html.erb

<% if current_or_guest_user.location.any? && @offerings.any? %>
  <div class="current-location">
    <h2>Offerings Near <%= current_or_guest_user.location.last.locationString %></h2>
  </div>
  <div class="container">
    <div class="row">
      <% @offerings.each do |offering| %>
        <%= render "offering_row", offering: offering, order_item: @order_item %>
      <% end %>
    </div>
  </div>
<% end %>

/offerings.controller.rb

def index

  if current_or_guest_user.location.any?
    @cooks = PickupLocation.near(current_or_guest_user.location.last.coordinates).collect { |location| location.user }

    offerings = []

    @cooks.each do |cook|
      cook.offerings.active.inventory.each do |offering|
        offerings.push(offering)
      end
    end

    @offerings = offerings
  end

  @order_item = current_order.order_items.new
end

错误输出

Started GET "/" for ::1 at 2015-09-19 12:51:54 -0700
Processing by OfferingsController#index as HTML
User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 63]]
Location Exists (0.2ms)  SELECT  1 AS one FROM "locations" WHERE "locations"."user_id" = $1 LIMIT 1  [["user_id", 63]]
Location Load (0.2ms)  SELECT  "locations".* FROM "locations" WHERE "locations"."user_id" = $1  ORDER BY "locations"."id" DESC LIMIT 1  [["user_id", 63]]
PickupLocation Load (1.2ms)  SELECT pickup_locations.*, 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((33.4484911 - pickup_locations.latitude) * PI() / 180 / 2), 2) + COS(33.4484911 * PI() / 180) * COS(pickup_locations.latitude * PI() / 180) * POWER(SIN((-112.0744277 - pickup_locations.longitude) * PI() / 180 / 2), 2))) AS distance, MOD(CAST((ATAN2( ((pickup_locations.longitude - -112.0744277) / 57.2957795), ((pickup_locations.latitude - 33.4484911) / 57.2957795)) * 57.2957795) + 360 AS decimal), 360) AS bearing FROM "pickup_locations" WHERE (pickup_locations.latitude BETWEEN 33.1590275337783 AND 33.7379546662217 AND pickup_locations.longitude BETWEEN -112.42134755027531 AND -111.72750784972469 AND (3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((33.4484911 - pickup_locations.latitude) * PI() / 180 / 2), 2) + COS(33.4484911 * PI() / 180) * COS(pickup_locations.latitude * PI() / 180) * POWER(SIN((-112.0744277 - pickup_locations.longitude) * PI() / 180 / 2), 2)))) BETWEEN 0.0 AND 20)  ORDER BY distance ASC
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 63]]


CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 63]]
User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 120]]
User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 130]]
User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 136]]
CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 136]]
User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 142]]
User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 144]]
User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 106]]
User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 146]]
CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 106]]
Offering Load (0.3ms)  SELECT "offerings".* FROM "offerings" WHERE "offerings"."user_id" = $1 AND "offerings"."active" = 't' AND (quantity > 0)  [["user_id", 63]]
CACHE (0.0ms)  SELECT "offerings".* FROM "offerings" WHERE "offerings"."user_id" = $1 AND "offerings"."active" = 't' AND (quantity > 0)  [["user_id", 63]]
Offering Load (0.3ms)  SELECT "offerings".* FROM "offerings" WHERE "offerings"."user_id" = $1 AND "offerings"."active" = 't' AND (quantity > 0)  [["user_id", 120]]
Offering Load (0.2ms)  SELECT "offerings".* FROM "offerings" WHERE "offerings"."user_id" = $1 AND "offerings"."active" = 't' AND (quantity > 0)  [["user_id", 130]]
Offering Load (0.3ms)  SELECT "offerings".* FROM "offerings" WHERE "offerings"."user_id" = $1 AND "offerings"."active" = 't' AND (quantity > 0)  [["user_id", 136]]
CACHE (0.0ms)  SELECT "offerings".* FROM "offerings" WHERE "offerings"."user_id" = $1 AND "offerings"."active" = 't' AND (quantity > 0)  [["user_id", 136]]
Completed 500 Internal Server Error in 34ms (ActiveRecord: 3.9ms)

NoMethodError (undefined method `offerings' for nil:NilClass):
  app/controllers/offerings_controller.rb:34:in `block in index'
  app/controllers/offerings_controller.rb:33:in `each'
  app/controllers/offerings_controller.rb:33:in `index'

0 个答案:

没有答案