专辑中的Rails NoMethodError#show

时间:2016-06-26 19:05:10

标签: ruby-on-rails ruby nomethoderror

如果相册目前没有描述,我正在尝试显示指向新描述的链接,但我一直收到NoMethodError:

<% if @album.description.size = 0 %>
    <div class="new-description"><%= link_to 'Add Description', "/albums/#{x.id}/edit" %></div>
<% else %>
    <div class="description"><%= @album.description %></div>
<% end %>

3 个答案:

答案 0 :(得分:0)

你试过了吗?

<% if @album.description.blank? %>
    <div class="new-description"><%= link_to 'Add Description', edit_album_path(@album) %></div>
<% else %>
    <div class="description"><%= @album.description %></div>
<% end %>

答案 1 :(得分:0)

如果描述应该是一个字符串,这也应该有用。

<% if not @album.description.nil? %>
    <div class="new-description"><%= link_to 'Add Description', "/albums/#{x.id}/edit" %></div>
<% else %>
    <div class="description"><%= @album.description %></div>
<% end %>

答案 2 :(得分:0)

尝试使用Object#tryhttps://github.com/rails/rails/blob/be589a8b01500a4b52a86248458bf57597e568e8/activesupport/lib/active_support/core_ext/object/try.rb

<% if @album.try(:description) %>
  <div class="description"><%= @album.description %></div>
<% else %>
  <div class="new-description"><%= link_to 'Add Description', "/albums/#{x.id}/edit" %></div>
<% end %>