简体   繁体   English

Rails:如何在不同的视图中进行小的更改

[英]Rails: How to make small changes in different views

Is it possible to make small changes in different views? 是否可以在不同的视图中进行小的更改?

The same partial is rendered in index.html.erb and show.html.erb as below. 相同的部分在index.html.erbshow.html.erb呈现,如下所示。

index.html.erb index.html.erb

<%= render @schedules %> 

show.html.erb show.html.erb

<%= render @schedules %> 

What I'd like to do is not to display some value in the index.html.erb. 我想做的是不在index.html.erb中显示某些值。 (and display some value in both erb) (并在两个erb中都显示一些值)

For example, I'd like to display start_at and end_at only in show.html.erb and display title in both erb. 例如,我想显示start_atend_at只有在show.html.erb和显示title两个ERB。

_schedule.html.erb _schedule.html.erb

<% schedule.rooms.each_with_index do |a, idx| %>

  <% a.events.each do |e| %>

    <%= l(e.start_at) %>-<%= l(e.end_at) %> # display only show.html.erb
    <%= e.title %>  #display both erb
    ...

  <% end %>

  ...
<% end %>

Althogh I come up with idea which I create two partials, it contradicts the DRY policy. 虽然我提出了创建两个部分的想法,但这与DRY政策背道而驰。

It would be appreciated if you could give me any idea. 如果您能给我任何想法,将不胜感激。

You can use controller.action_name . 您可以使用controller.action_name

<% if controller.action_name == 'show' %>
  <%= l(e.start_at) %>-<%= l(e.end_at) %> # display only show.html.erb
<% end %>

The params hash also contains the action_name. params哈希还包含action_name。

Can check current action and current controller on page. 可以在页面上检查当前动作和当前控制器。 So we can call single partial from different actions and can customize as per action name or action and controller name. 因此,我们可以调用来自不同动作的单个部分,并可以根据动作名称或动作和控制器名称进行自定义。

eg. 例如。

<% schedule.rooms.each_with_index do |a, idx| %>

  <% a.events.each do |e| %>
    <% if @current_controller == "events" and @current_action == "show" %>
      <%= l(e.start_at) %>-<%= l(e.end_at) %> # display only show.html.erb
    <% end %>
    <%= e.title %>  #display both erb
    ...

  <% end %>

  ...
<% end %>

Also need to update Application Controller. 还需要更新Application Controller。

class ApplicationController < ActionController::Base
  before_filter :instantiate_controller_and_action_names

  def instantiate_controller_and_action_names
    @current_controller = controller_name
    @current_action = action_name
  end

end

action_name is enough and do the trick but personally I don't like this. action_name就足够了,并且可以解决问题,但我个人不喜欢这样。 I'd do two separate partials. 我会做两个单独的部分。

You could use CSS to hide/show the content based on context. 您可以使用CSS根据上下文隐藏/显示内容。

In practice, I have found this a good way to reuse partials that have small differences. 在实践中,我发现这是重用差异很小的部分的好方法。 Especially when those differences don't cost anything to compute ie printing a date 尤其是当这些差异不花任何时间来计算时,即打印日期

  • You can cache the partials without worrying about where they are rendered 您可以缓存部分,而不必担心它们的渲染位置
  • Reduce conditional logic 减少条件逻辑
  • Remove duplication 删除重复

<% if controller.action_name == 'show' %> is fine for a simple use case. 对于一个简单的用例, <% if controller.action_name == 'show' %>是可以的。 If/When you come to have multiple places where the partial needs to be rendered, it will become unwieldy. 如果/当您有多个需要渲染局部的地方时,它将变得笨拙。 The CSS solution would only require another wrapper <div class="schedules--whatever"> and the related CSS style. CSS解决方案仅需要另一个包装<div class="schedules--whatever">和相关的CSS样式。

show.html.erb show.html.erb

<div class="schedules--show">
  <%= render @schedules %> 
</div>

index.html.erb index.html.erb

<div class="schedules--index">
  <%= render @schedules %> 
</div>

_schedule.html.erb _schedule.html.erb

<% schedule.rooms.each_with_index do |a, idx| %>
  <% a.events.each do |e| %>
    <div class="event__date">
      <%= l(e.start_at) %>-<%= l(e.end_at) %>
    </div>
    <%= e.title %>
    ...

  <% end %>
  ...
<% end %>

schedules.css schedules.css

.schedules--show .event__date {
  display: block;
}

.schedules--index .event__date {
  display: none;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM