Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.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&;的重复使用的代码块;CSS?_Css_Ruby On Rails_Ruby_Dry_Helper - Fatal编程技术网

我应该如何处理包含ruby&;的重复使用的代码块;CSS?

我应该如何处理包含ruby&;的重复使用的代码块;CSS?,css,ruby-on-rails,ruby,dry,helper,Css,Ruby On Rails,Ruby,Dry,Helper,很长一段时间以来,我忽略了RubyonRails的枯燥原理。我在不同的视图文件中反复使用相同的代码块: <div class="challenge-accomplished-date-banner"> <% if @correct_user %> <%= challenge.notes.count.to_s.rjust(2, "0") %> <% else %> <%= challenge.notes.publish.

很长一段时间以来,我忽略了RubyonRails的枯燥原理。我在不同的视图文件中反复使用相同的代码块:

<div class="challenge-accomplished-date-banner">
  <% if @correct_user %>
    <%= challenge.notes.count.to_s.rjust(2, "0") %>
  <% else %>
    <%= challenge.notes.publish.count.to_s.rjust(2, "0") %>
  <% end %>
</div>
<% if challenge.categorization == "adventure" %>
  <%= link_to categorization_path(categorization: :adventure) do %>
    <span class="glyphicon glyphicon-picture", id="challenge-flag"></span>
  <% end %>
<% elsif challenge.categorization == "health" %>
  <%= link_to categorization_path(categorization: :health) do %>   
    <span class="glyphicon glyphicon-heart", id="challenge-flag"></span>
  <% end %>            
<% elsif challenge.categorization == "work" %>
  <%= link_to categorization_path(categorization: :work) do %>    
    <span class="glyphicon glyphicon-briefcase", id="challenge-flag"></span>
  <% end %>
<% elsif challenge.categorization == "gift" %>
  <%= link_to categorization_path(categorization: :gift) do %>    
    <span class="glyphicon glyphicon-tree-deciduous", id="challenge-flag"></span>
  <% end %>
<% else %>
  <%= link_to categorization_path(categorization: :wacky) do %>    
    <span class="glyphicon glyphicon-glass", id="challenge-flag"></span>
  <% end %>
<% end %>
<% if challenge.duels.present? && challenge.duels.last.duelers.order(id: :asc).last.accept =! false %>
  <span class="glyphicon glyphicon-tower", id="challenge-flag"></span>
<% elsif challenge.conceal == true %>
  <span class="glyphicon glyphicon-eye-close", id="challenge-flag"></span>
<% else %>
  <span class="glyphicon glyphicon-eye-open", id="challenge-flag"></span>      
<% end %>

但它不适用于CSS?有没有一种方法允许在助手中使用CSS?或者我把这段代码放在模型中的其他地方,但是我遇到了同样的CSS问题?谢谢

您可以在助手上定义以下方法:

def challenge_link(category)
  icon = case category
  when 'adventure' then 'picture'
  when 'health' then 'heart'
  when 'work' then 'briefcase'
  when 'gift' then 'tree-deciduous'
  else
    'glass'
  end

  link_to(categorization_path(categorization: category.to_sym)) do
    content_tag(:span, "", class: "glyphicon glyphicon-#{ icon }", id: "challenge-flag")
  end
end
然后从视图中调用它,如下所示:

<%= challenge_link('adventure') %>

可以在辅助对象上定义以下方法:

def challenge_link(category)
  icon = case category
  when 'adventure' then 'picture'
  when 'health' then 'heart'
  when 'work' then 'briefcase'
  when 'gift' then 'tree-deciduous'
  else
    'glass'
  end

  link_to(categorization_path(categorization: category.to_sym)) do
    content_tag(:span, "", class: "glyphicon glyphicon-#{ icon }", id: "challenge-flag")
  end
end
然后从视图中调用它,如下所示:

<%= challenge_link('adventure') %>

创建一个
CATEGORIES
常量

CATEGORIES = {
  adventure: 'glyphicon-picture',
  health:    'glyphicon-heart',
  work:      'glyphicon-briefcase',
  gift:      'glyphicon-tree-deciduous'
}

CATEGORIES.default = 'glyphicon-glass'
创建分部

\u flag.html.erb

<span class="glyphicon <%= class_name %>", id="challenge-flag"></span>
<%= link_to categorization_path(categorization: category) do %>    
  <%= render 'flag', class_name: class_name %>
<% end %>

\u link.html.erb

<span class="glyphicon <%= class_name %>", id="challenge-flag"></span>
<%= link_to categorization_path(categorization: category) do %>    
  <%= render 'flag', class_name: class_name %>
<% end %>

并使用它们

<% category = challenge.categorization %>
<% class_name = CATEGORIES[category.to_sym] %>

<%= render 'link', class_name: class_name, category: category %>

创建一个
类别
常量

CATEGORIES = {
  adventure: 'glyphicon-picture',
  health:    'glyphicon-heart',
  work:      'glyphicon-briefcase',
  gift:      'glyphicon-tree-deciduous'
}

CATEGORIES.default = 'glyphicon-glass'
创建分部

\u flag.html.erb

<span class="glyphicon <%= class_name %>", id="challenge-flag"></span>
<%= link_to categorization_path(categorization: category) do %>    
  <%= render 'flag', class_name: class_name %>
<% end %>

\u link.html.erb

<span class="glyphicon <%= class_name %>", id="challenge-flag"></span>
<%= link_to categorization_path(categorization: category) do %>    
  <%= render 'flag', class_name: class_name %>
<% end %>

并使用它们

<% category = challenge.categorization %>
<% class_name = CATEGORIES[category.to_sym] %>

<%= render 'link', class_name: class_name, category: category %>


CSS在哪里?我在这里看不到任何内容。
我认为
类也将构成CSS@AmadanNo,
.challenge completed date banner{……}
(在
.CSS
文件或
标记的主体中)构成CSS。HTML元素上的Class属性仍然只是HTML的文本属性。助手中的class属性有什么问题?“它不工作”是什么意思?如果它是一个来自CSS文件的方法,这不就是CSS吗?如果将代码复制并粘贴到一个应用程序中,你会看到格式都被丢弃了,因为我猜helper不识别CSS@AmadanThanks,但你真的不应该:这会被检测为投票操纵,有点违反规则:)CSS在哪里?我在这里看不到任何内容。
我认为
类也将构成CSS@AmadanNo,
.challenge completed date banner{……}
(在
.CSS
文件或
标记的主体中)构成CSS。HTML元素上的Class属性仍然只是HTML的文本属性。助手中的class属性有什么问题?“它不工作”是什么意思?如果它是一个来自CSS文件的方法,这不就是CSS吗?如果将代码复制并粘贴到应用程序_helper.rb中,你会看到格式都被丢弃了,因为我猜helper不识别CSS@AmadanThanks,但你真的不应该:它会被检测为选票操纵,这有点违反规则:)当我可以将代码块放在部分_banner.html.erb中时,我为什么要这样做?你的备选方案会让网站更快吗?还是一种最佳实践?我还是个初学者,所以我很好奇为什么一种方法比另一种更好。谢谢1.干代码2。已删除条件
if-elsif-else-end
3。少行代码4。易于理解(具备ruby的基本知识)好吧,说得好:)我正在运行一个
未初始化的常量ActionView::CompiledTemplates::CATEGORIES
错误您可以在使用前一个代码的地方使用该代码当我可以将代码块放在部分_banner.html.erb中时,为什么我要这样做?你的备选方案会让网站更快吗?还是一种最佳实践?我还是个初学者,所以我很好奇为什么一种方法比另一种更好。谢谢1.干代码2。已删除条件
if-elsif-else-end
3。少行代码4。易于理解(具备ruby的基本知识)好吧,说得好:)我遇到了一个
未初始化的常量ActionView::CompiledTemplates::CATEGORIES
错误您可以在使用前一个代码的地方使用代码我建议选择一个助手(正如这个答案所示)而不是呈现模板,由于模板的渲染速度相对较慢,在本例中没有任何实际好处,
link_to
content_tag
提供了您所需的内容。我建议您选择一个助手(正如此答案所示)而不是渲染模板,因为模板的渲染速度相对较慢,在本例中没有任何实际好处,
链接到
内容标签
提供您所需的内容。