Ruby on rails 在rails中分解此代码的最佳方法是什么
我想分解这段代码(if/else) 首先,我会这样做:Ruby on rails 在rails中分解此代码的最佳方法是什么,ruby-on-rails,Ruby On Rails,我想分解这段代码(if/else) 首先,我会这样做: <% @pois.each_with_index do |poi, i| %> <div class="card-item"> <% if poi.poitable.sleep_images.blank? %> <div class="card-sleep-thumb" style="background-image: url(<
<% @pois.each_with_index do |poi, i| %>
<div class="card-item">
<% if poi.poitable.sleep_images.blank? %>
<div class="card-sleep-thumb" style="background-image: url(<%= cl_image_path("comingsoon.jpg", :width=>600, :crop=>"scale") %>);">
<% else %>
<div class="card-sleep-thumb" style="background-image: url(<%= cl_image_path(poi.poitable.sleep_images.first.image, :width=>600, :crop=>"scale") %>);">
<% end %>
在第二种方式中,我尝试了另一种类似这样的想法:
<div class="card-sleep-thumb" style="background-image: url(<%= if poi.poitable.sleep_images.blank? ? cl_image_path("comingsoon.jpg", :width=>600, :crop=>"scale" : cl_image_path(poi.poitable.sleep_images.first.image, :width=>600, :crop=>"scale") %>);" %>
但是,也许有更好的方法,在我的模型中使用一种方法
你怎么能做同样的事情呢?使用一个助手方法
class SomeModelHelper
def some_method_name(poi)
if poi.poitable.sleep_images.blank?
cl_image_path("comingsoon.jpg", :width=>600, :crop=>"scale")
else
cl_image_path(poi.poitable.sleep_images.first.image, :width=>600, :crop=>"scale")
end
end
end
// in the view
<div class="card-sleep-thumb" style="background-image: url(<%= some_method_name(poi).html_safe %>);" %>
class-SomeModelHelper
定义某些方法名称(poi)
如果poi.poitable.sleep\u images.blank?
cl_图像_路径(“comingsoon.jpg”,:宽度=>600,:裁剪=>比例”)
其他的
cl_image_路径(poi.poitable.sleep_images.first.image,:width=>600,:crop=>scale)
结束
结束
结束
//在视图中
当然,您应该使用与poi
类关联的帮助器(例如PoiHelper
,如果该类被称为poi
),并且帮助器方法的名称更具表现力。使用帮助器方法
class SomeModelHelper
def some_method_name(poi)
if poi.poitable.sleep_images.blank?
cl_image_path("comingsoon.jpg", :width=>600, :crop=>"scale")
else
cl_image_path(poi.poitable.sleep_images.first.image, :width=>600, :crop=>"scale")
end
end
end
// in the view
<div class="card-sleep-thumb" style="background-image: url(<%= some_method_name(poi).html_safe %>);" %>
class-SomeModelHelper
定义某些方法名称(poi)
如果poi.poitable.sleep\u images.blank?
cl_图像_路径(“comingsoon.jpg”,:宽度=>600,:裁剪=>比例”)
其他的
cl_image_路径(poi.poitable.sleep_images.first.image,:width=>600,:crop=>scale)
结束
结束
结束
//在视图中
当然,您应该使用与
poi
类关联的帮助器(例如PoiHelper
,如果该类被称为poi
),并且帮助器方法的名称更具表现力。您可以使用|
操作符,它返回第一个“truthy”操作数,您当前使用的不是if
/else
:
cl_image_path(poi.poitable.sleep_images.first&.image || "comingsoon.jpg")
cl\u image\u path
的此参数要么是图像(如果存在),要么是“comingsoon.jpg”
否则
重构的第一步可以如下所示:
<% @pois.each do |poi| %>
<div class="card-item">
<div class="card-sleep-thumb" style="background-image: url(<%= cl_image_path(poi.poitable.sleep_images.first.image || "coming_soon.jpg"), :width=>600, :crop=>"scale") %>);">
</div>
</div>
<% end %>
<%= render @pois %>
您将创建一个新的分部,其中包含给定“Poi”的HTML表示。我不知道类名是什么,但如果它是Poi
,则应将该部分放在app/views/Poi/_Poi.html.erb
,它将包含以下内容:
<div class="card-item">
<div class="card-sleep-thumb" style="background-image: url(<%= cl_image_path(poi.poitable.sleep_images.first.image || "coming_soon.jpg"), :width=>600, :crop=>"scale") %>);">
</div>
</div>
您可以使用
|
运算符,该运算符返回第一个“truthy”操作数,而不是当前使用的if
/else
:
cl_image_path(poi.poitable.sleep_images.first&.image || "comingsoon.jpg")
cl\u image\u path
的此参数要么是图像(如果存在),要么是“comingsoon.jpg”
否则
重构的第一步可以如下所示:
<% @pois.each do |poi| %>
<div class="card-item">
<div class="card-sleep-thumb" style="background-image: url(<%= cl_image_path(poi.poitable.sleep_images.first.image || "coming_soon.jpg"), :width=>600, :crop=>"scale") %>);">
</div>
</div>
<% end %>
<%= render @pois %>
您将创建一个新的分部,其中包含给定“Poi”的HTML表示。我不知道类名是什么,但如果它是Poi
,则应将该部分放在app/views/Poi/_Poi.html.erb
,它将包含以下内容:
<div class="card-item">
<div class="card-sleep-thumb" style="background-image: url(<%= cl_image_path(poi.poitable.sleep_images.first.image || "coming_soon.jpg"), :width=>600, :crop=>"scale") %>);">
</div>
</div>
谢谢Tamer,我认为有更好的方法;)谢谢塔默,我想,还有更好的办法;)