Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在rails中分解此代码的最佳方法是什么_Ruby On Rails - Fatal编程技术网

Ruby on rails 在rails中分解此代码的最佳方法是什么

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(<

我想分解这段代码(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(<%= 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,我认为有更好的方法;)谢谢塔默,我想,还有更好的办法;)