Ruby-on-rails 视图中 .html.erb 中的 if else 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17795053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:21:57  来源:igfitidea点击:

If else statements in .html.erb in views

ruby-on-railsrubymodel-view-controller

提问by dsp_099

In rails, I often run into the situation where inside the views I'll do something like

在 Rails 中,我经常遇到这样的情况,在视图中我会做一些类似的事情

<% if @some_condition_previusly_established_in_a_controller %>
 <div class="one">123</div>
<% else %>
 <div class="two">something else</div>
<% end %>

It looks a bit cluttery. Is this an acceptable way of working with views or not?

它看起来有点凌乱。这是一种可以接受的处理视图的方式吗?

采纳答案by tadman

Unless you can think of a way to re-write this as a helper method, you're basically stuck with it looking kind of ugly. That's just how ERB is, as it was intended to be a minimal way of injecting Ruby into an otherwise plain-text template, not as something necessarily streamlined or elegant.

除非你能想出一种方法将它重写为辅助方法,否则你基本上会被它看起来有点难看。这就是 ERB 的方式,因为它旨在作为将 Ruby 注入其他纯文本模板的最小方式,而不是作为必要的流线型或优雅的东西。

The good news is a syntax-highlighting editor will usually make your <% ... %>ERB blocks look visually different from your HTML so that can dramatically improve readability.

好消息是语法高亮编辑器通常会使您的<% ... %>ERB 块在视觉上与 HTML 不同,从而可以显着提高可读性。

It's also why other representations like HAMLhave been created where that syntax is a lot less cluttered:

这也是为什么创建了HAML 之类的其他表示,其中语法不那么混乱:

- if some_condition_previusly_established_in_a_controller
  .one 123
- else
  .two something else

回答by vee

For one or two such conditional logic in your views, I guess its fine but when your code gets bigger and you have multiple if..else..endand looks "cluttery", I think you should look at implementing "Presenter Pattern" which greatly cleans up your views by separating your logic to Presenters.

对于您视图中的一两个这样的条件逻辑,我想这很好,但是当您的代码变大并且您有多个if..else..end并且看起来“杂乱”时,我认为您应该考虑实现“演示者模式”,它通过分离极大地清理了您的视图您对演示者的逻辑。

Here is a great tutorial I followed from Ryan Bates in his Rails Casts series on "Presenter Patterns from scratch". http://railscasts.com/episodes/287-presenters-from-scratch.

这是我在 Ryan Bates 在他的 Rails Casts 系列“从头开始演示模式”中遵循的一个很棒的教程。 http://railscasts.com/episodes/287-presenters-from-scratch

回答by ajthewebdev

Have you tried?

你有没有尝试过?

<% @some_condition_previusly_established_in_a_controller ? <div class="one">123</div> : <div class="two">something else</div> %>

回答by Sunda

If your view contains lots of tags and HTML elements, you can put them into partials and logic into model

如果您的视图包含大量标签和 HTML 元素,您可以将它们放入部分并将逻辑放入模型中

View:

看法:

<%= render :partial => @model.status %>

<%= render :partial => "file/path/#{@model.status}" %> # if your partial is in some different folder

If your status is one, then it would render the file _one.html.erb

如果您的状态为 1,则它将呈现文件 _one.html.erb

If it is two, then it would render the file _two.html.erb automatically.

如果是两个,那么它会自动渲染文件 _two.html.erb。

Model:

模型:

def status
    if @some_condition
      "one"
    else
      "two"
    end
end

回答by Rebitzele

Yes, that is the standard (and yes, it looks cluttery).

是的,这就是标准(是的,它看起来很杂乱)。

If you're looking for a possibly cleaner alternative, check out: Conditional tag wrapping in Rails / ERB

如果您正在寻找可能更清洁的替代方案,请查看:Rails / ERB 中的条件标签包装

回答by Rebitzele

You can always move the logic to the controller and leave the view clean(er).

您始终可以将逻辑移动到控制器并保持视图干净(呃)。

Controller:

控制器:

if @some_condition
  @div_class = :one
  @div_content = 123
else
  @div_class = :two
  @div_content = 'something else'
end

View:

看法:

 <div class="<%= @div_class %>"><%= @div_content %></div>

Or using a helper:

或者使用助手:

 <%= content_tag :div, @div_content, class: @div_class %>