SamuraiBlue SamuraiBlue - 6 months ago 8
Ruby Question

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.erb

<%= render @schedules %>


show.html.erb

<%= render @schedules %>


What I'd like to do is not to display some value in the index.html.erb. (and display some value in both erb)

For example, I'd like to display
start_at
and
end_at
only in
show.html.erb
and display
title
in both 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.

It would be appreciated if you could give me any idea.

Answer

You can use 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.