Ruby on Rails:使用默认值,当变量为null或为空时

时间:2021-09-30 15:23:58

I have this code-snippet in html erb.

我在html erb中有这个代码片段。

For some objects the cover_image_url is empty, how do i modify this code block to use a default value ,when that property is null or empty?

对于某些对象,cover_image_url为空,当该属性为null或为空时,如何修改此代码块以使用默认值?

<%@books.each do |book|%>
        $('#bookContainer').append('<div class="conn"><p><img class="floatright" src="<%= h book.cover_image_url%>"><h3><%= h book.title%></h3><h3><%= h book.author%></h3></p></div>');
    <% end %>

2 个解决方案

#1


15  

You could define a cover_image_url method on your book model that will return a default value if there is nothing set in the database (I am assuming that cover_image_url is a column in the book table). Something like this:

如果数据库中没有设置任何内容,我可以在您的图书模型上定义一个cover_image_url方法,该方法将返回默认值(我假设cover_image_url是book表中的一列)。像这样的东西:

class Book < ActiveRecord::Base
  def cover_image_url
    read_attribute(:cover_image_url).presence || "/my_default_link"
  end
end

This will return "/my_default_link" if the attribute is not set, or the value of the attribute if it is set. See section 5.3 on The Rails 3 Way for more info on this stuff. Defining a default value for a model in the model layer may be a little cleaner than doing it in the view layer.

如果未设置属性,则返回“/ my_default_link”,如果设置了属性,则返回属性值。有关这些内容的更多信息,请参阅有关Rails 3 Way的第5.3节。在模型图层中为模型定义默认值可能比在视图图层中进行更清晰。

#2


1  

You can use a local variable inside the loop :

您可以在循环内使用局部变量:

<% url = book.cover_image_url or "/my_default_link" %>

This will take a default link if the first value is nil.

如果第一个值为nil,则将采用默认链接。

To check both null or empty :

要检查null或empty:

<% url = ((book.cover_image_url.nil? or (book.cover_image_url and book.cover_image_url.empty?)) ? "/my_default_link" : book.cover_image_url) %>

Put the whole thing in a helper to make it cleaner.

把整个东西放在帮手里让它变得更干净。

#1


15  

You could define a cover_image_url method on your book model that will return a default value if there is nothing set in the database (I am assuming that cover_image_url is a column in the book table). Something like this:

如果数据库中没有设置任何内容,我可以在您的图书模型上定义一个cover_image_url方法,该方法将返回默认值(我假设cover_image_url是book表中的一列)。像这样的东西:

class Book < ActiveRecord::Base
  def cover_image_url
    read_attribute(:cover_image_url).presence || "/my_default_link"
  end
end

This will return "/my_default_link" if the attribute is not set, or the value of the attribute if it is set. See section 5.3 on The Rails 3 Way for more info on this stuff. Defining a default value for a model in the model layer may be a little cleaner than doing it in the view layer.

如果未设置属性,则返回“/ my_default_link”,如果设置了属性,则返回属性值。有关这些内容的更多信息,请参阅有关Rails 3 Way的第5.3节。在模型图层中为模型定义默认值可能比在视图图层中进行更清晰。

#2


1  

You can use a local variable inside the loop :

您可以在循环内使用局部变量:

<% url = book.cover_image_url or "/my_default_link" %>

This will take a default link if the first value is nil.

如果第一个值为nil,则将采用默认链接。

To check both null or empty :

要检查null或empty:

<% url = ((book.cover_image_url.nil? or (book.cover_image_url and book.cover_image_url.empty?)) ? "/my_default_link" : book.cover_image_url) %>

Put the whole thing in a helper to make it cleaner.

把整个东西放在帮手里让它变得更干净。