为什么我的 rails 应用程序在推送到 Heroku: "ActionView::Template::Error (undefined method `toilet_available' for



我正在 Rails 5 中构建一个商店定位器应用程序,该应用程序使用 Open Street Map API 和传单轨道 gem。在开发环境中,一切都按预期工作,但是当我推送到 Heroku 时,我收到"很抱歉,但出了点问题"消息。我创建了一个功能,您可以在其中根据商店是否有厕所对商店进行排序,而这个接缝就是问题所在。Heroku 日志显示以下内容:

2018-09-12T22:02:39.751563+00:00 app[web.1]: [30a84d17-6a5a-4b3d-b21f-ee08babfd123] Completed 500 Internal Server Error in 624ms (ActiveRecord: 119.5ms)
2018-09-12T22:02:39.706503+00:00 app[web.1]: [30a84d17-6a5a-4b3d-b21f-ee08babfd123]   Toilet Load (29.5ms)  SELECT "toilets".* FROM "toilets"
2018-09-12T22:02:39.772904+00:00 app[web.1]: [30a84d17-6a5a-4b3d-b21f-ee08babfd123]
2018-09-12T22:02:39.773276+00:00 app[web.1]: [30a84d17-6a5a-4b3d-b21f-ee08babfd123]     29:                                             <li><%= link_to "Log In", new_user_session_path %> </li>
2018-09-12T22:02:39.772915+00:00 app[web.1]: [30a84d17-6a5a-4b3d-b21f-ee08babfd123] ActionView::Template::Error (undefined method `toilet_available' for nil:NilClass):
2018-09-12T22:02:39.773280+00:00 app[web.1]: [30a84d17-6a5a-4b3d-b21f-ee08babfd123]     30:                                             <li><%= link_to "Add Spaeti", new_user_registration_path %> </li>
2018-09-12T22:02:39.773283+00:00 app[web.1]: [30a84d17-6a5a-4b3d-b21f-ee08babfd123]     31:                                     <% end %>
2018-09-12T22:02:39.773285+00:00 app[web.1]: [30a84d17-6a5a-4b3d-b21f-ee08babfd123]     32:                                     <li><%= link_to "Spaetis with toilets", stores_path(toilet: Toilet.all[0].toilet_available) %></li>
2018-09-12T22:02:39.773288+00:00 app[web.1]: [30a84d17-6a5a-4b3d-b21f-ee08babfd123]     33:                             </ul>
2018-09-12T22:02:39.773290+00:00 app[web.1]: [30a84d17-6a5a-4b3d-b21f-ee08babfd123]     34:                     </div>
2018-09-12T22:02:39.773292+00:00 app[web.1]: [30a84d17-6a5a-4b3d-b21f-ee08babfd123]     35:             </nav>
2018-09-12T22:02:39.773336+00:00 app[web.1]: [30a84d17-6a5a-4b3d-b21f-ee08babfd123]
2018-09-12T22:02:39.773423+00:00 app[web.1]: [30a84d17-6a5a-4b3d-b21f-ee08babfd123] app/views/layouts/application.html.erb:32:in `_app_views_layouts_application_html_erb__2166090761148733231_41993540'

当我从视图中删除此行时,应用程序加载正常:<li><%= link_to "Spaetis with toilets", stores_path(toilet: Toilet.all[0].toilet_available) %></li>

可能导致此错误的原因是什么?我已经扯头发好几天了...

店铺模式:

class Store < ApplicationRecord
belongs_to :user
belongs_to :toilet
validates :name, presence: true
validates :address, presence: true
validates :address_line2, presence: true
validates :address_line3, presence: true
validates :toilet, presence: true
# validates :latitude, presence: true
# validates :longitude, presence: true
validates :beer_cost, format: { with: /Ad+(?:.d{0,2})?z/ }, numericality: { greater_than: 0, less_than: 10 }
validates :latitude, :presence => {message: "Not a valid location, please check name address & country fields" }
def full_address
[address, address_line2, address_line3].compact.join(', ')
end
geocoded_by :full_address
before_validation :geocode,
:if => lambda{ |obj| obj.address_changed? }
def capitalize_name
name.capitalize
end
def capitalize_address
address.capitalize
end
end

马桶型号:

class Toilet < ApplicationRecord
has_many :stores
end

Schema.rb:

ActiveRecord::Schema.define(version: 20180814220216) do
create_table "stores", force: :cascade do |t|
t.string "name"
t.string "address"
t.float "beer_cost"
t.text "facilities"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.float "latitude"
t.float "longitude"
t.boolean "toilet"
t.string "address_line2"
t.integer "user_id"
t.integer "toilet_id"
t.string "address_line3"
end
create_table "toilets", force: :cascade do |t|
t.string "toilet_available"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
end

Github存储库:https://github.com/catsinspacesuits/Spaeti-App

对不起,我误解了你想做什么。我认为,即使你不再得到错误,你仍然没有得到你想要得到的答案。您正在使用一种非常非标准的方式来拥有布尔列。在 rails 控制台上执行以下操作:

t = Toilet.first
t.toilet_available

并查看返回值。无论"是"还是"否",您都会得到一个值。所以如果这样:

Toilet.all[0].toilet_available

应该意味着:"获得所有厕所并在可用的情况下采取第一个 = 是的"这不是正在发生的事情。定义为具有"是"或"否"的String类型的数据库列不是布尔值,如果不定义解释它的方法,则不会像布尔值一样工作:

def has_toilet
self.toilet_available == "yes" ? true : false
end

我认为您Toilet.all[0].toilet_available得到的是"获取所有厕所并获取第一个结果并返回第 toilet_available 列中的文本,无论该文本是true还是false"。但是,如果你的意图是"给我第一个可用性true厕所",那么你的代码是错误的。

下面是一个简单的测试,用于您拥有大量记录的开发数据库。在 Rails 控制台中执行以下操作:

puts Toilet.all[0]
puts Toilet.all.first
puts Toilet.where(toilet_available: 'yes').limit(1)
puts Toilet.find_by(toilet_available: 'yes')

它们可能不会都返回相同的结果。恕我直言,.all[0]模式是从数据库中获取某些东西的糟糕方式,因为它非常模棱两可。

还可以在控制台中执行此操作:

Toilet.all.each do |t|
puts "toilet_available is #{t.toilet_available} and is #{t.toilet_available ? true : false}
end

您应该看到类似以下内容:

toilet_available is yes and is true
toilet_available is no and is true

关键是,在 Rails 中,在文本列中具有文本值会导致它是"true"。获取 false 的唯一方法是将值nil。如果toilet_available只有两种状态,那么布尔列类型将更合适,并且是其他开发人员倾向于期望的。如果值可以是三种状态,如"是"、"否"、"未知",则可以将列与状态机一起使用。

相关内容

最新更新