本文介绍了为什么不能显示餐厅列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试制作一些餐厅列表.我关联了两个表,然后写了这段代码.
I'm trying to make some restaurant list.I associated two tables, and then write this code.
class Restaurant < ActiveRecord::Base
has_many :restaurant_translations
end
class RestaurantTranslation < ActiveRecord::Base
self.table_name = 'restaurant_translations'
end
restaurant_controller.rb
restaurant_controller.rb
class RestaurantController < ApplicationController
def list
@restaurants = Restaurant.all
logger.debug @restaurants
end
end
list.html.slim桌子头tr类型姓名网址流派第一个地址
list.html.slim table thead tr th Type th Name th Url th Genre th Addr
tbody
- @restaurants.each do |restaurant|
tr
td = restaurant.restaurant_type
td = restaurant.restaurant_translations.restaurantname
td = link_to 'here', restaurant.url
td = restaurant.genre
td = restaurant.restaurant_translations.address
但结果如下.未定义方法`restaurantname' #"
But results are belows."undefined method `restaurantname' for #"
我该如何解决这个问题?提前致谢.
How can I do this problem? Thanks in advance.
推荐答案
您的餐厅有多个restaurant_translations".
Your restauration have multiple 'restaurant_translations'.
例如,首先你可以写td = restaurant.restaurant_translations.first.try(:restaurantname)
这篇关于为什么不能显示餐厅列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!