Ruby 索引页面即使为 nil 也显示,当前出现未定义错误
Ruby index page display even if nil, currently get undefined error
您好,我正在尝试获取 属性 的索引页以显示当前 属性 中的租户。
我已通过执行以下
在我的 show.html 页面上运行它
.wrapper_with_padding
#house.show
%h1= @house.title
%p= number_to_currency(@house.price, :unit => "£")
%p= simple_format(@house.description)
Occupied: #{@house.occupied}
%br/
-if @house.tenant.present?
Tenant: #{@house.tenant.first_name} #{@house.tenant.last_name}
-else
%p= 'No Tenant Assigned'
我如何为我的索引页采用同样的方法,这就是我目前所拥有的,但我收到错误 'tenant' 未定义。
.wrapper_with_padding
#houses.clearfix
- unless @houses.blank?
- @houses.each do |house|
%a{ href: (url_for [house])}
.house
%p.title= house.title
%p.postcode= 'Postcode: ' + (house.postcode)
%p.price= number_to_currency(house.price, :unit => "£") + ' per month'
-if @house.tenant.present?
%p.tenant_id= @house.tenant.first_name
-else
%p No Tenant Assigned
- else
%h2 Add a Property
%p It appears you have not added any property's
%button= link_to "New Property", new_house_path
这是完整的堆栈跟踪
Showing C:/Sites/landlord2/app/views/houses/index.html.haml where line #10 raised:
undefined method `tenant' for nil:NilClass
Rails.root: C:/Sites/landlord2
Application Trace | Framework Trace | Full Trace
app/views/houses/index.html.haml:10:in `block in _app_views_houses_index_html_haml__260246869_75400416'
app/views/houses/index.html.haml:4:in `_app_views_houses_index_html_haml__260246869_75400416'
这应该有效:
.wrapper_with_padding
#houses.clearfix
- unless @houses.blank?
- @houses.each do |house|
%a{ href: (url_for [house])}
.house
%p.title= house.title
%p.postcode= 'Postcode: ' + (house.postcode)
%p.price= number_to_currency(house.price, :unit => "£") + ' per month'
-if house.tenant.present?
%p.tenant_id= house.tenant.first_name
-else
%p No Tenant Assigned
- else
%h2 Add a Property
%p It appears you have not added any property's
%button= link_to "New Property", new_house_path
请注意,虽然我没有修复任何其他错误,但您可能应该阅读一些关于 Haml 规范的内容。
您不必执行 %p= 'No Tenant Assigned'
,因为 %p No Tenant Assigned
有效。
您好,我正在尝试获取 属性 的索引页以显示当前 属性 中的租户。
我已通过执行以下
在我的 show.html 页面上运行它 .wrapper_with_padding
#house.show
%h1= @house.title
%p= number_to_currency(@house.price, :unit => "£")
%p= simple_format(@house.description)
Occupied: #{@house.occupied}
%br/
-if @house.tenant.present?
Tenant: #{@house.tenant.first_name} #{@house.tenant.last_name}
-else
%p= 'No Tenant Assigned'
我如何为我的索引页采用同样的方法,这就是我目前所拥有的,但我收到错误 'tenant' 未定义。
.wrapper_with_padding
#houses.clearfix
- unless @houses.blank?
- @houses.each do |house|
%a{ href: (url_for [house])}
.house
%p.title= house.title
%p.postcode= 'Postcode: ' + (house.postcode)
%p.price= number_to_currency(house.price, :unit => "£") + ' per month'
-if @house.tenant.present?
%p.tenant_id= @house.tenant.first_name
-else
%p No Tenant Assigned
- else
%h2 Add a Property
%p It appears you have not added any property's
%button= link_to "New Property", new_house_path
这是完整的堆栈跟踪
Showing C:/Sites/landlord2/app/views/houses/index.html.haml where line #10 raised:
undefined method `tenant' for nil:NilClass
Rails.root: C:/Sites/landlord2
Application Trace | Framework Trace | Full Trace
app/views/houses/index.html.haml:10:in `block in _app_views_houses_index_html_haml__260246869_75400416'
app/views/houses/index.html.haml:4:in `_app_views_houses_index_html_haml__260246869_75400416'
这应该有效:
.wrapper_with_padding
#houses.clearfix
- unless @houses.blank?
- @houses.each do |house|
%a{ href: (url_for [house])}
.house
%p.title= house.title
%p.postcode= 'Postcode: ' + (house.postcode)
%p.price= number_to_currency(house.price, :unit => "£") + ' per month'
-if house.tenant.present?
%p.tenant_id= house.tenant.first_name
-else
%p No Tenant Assigned
- else
%h2 Add a Property
%p It appears you have not added any property's
%button= link_to "New Property", new_house_path
请注意,虽然我没有修复任何其他错误,但您可能应该阅读一些关于 Haml 规范的内容。
您不必执行 %p= 'No Tenant Assigned'
,因为 %p No Tenant Assigned
有效。