Sharing view code across actions with the same function

If you put your helpers in application_helper.rb, those methods are
available from all views.

You can also create shared partials that are accessible like this:

render :partial => 'shared/edit'

You need to create a shared directory in the views directory. So
you'd have:


'shared' could be anything string, like 'common', etc. 'shared' is
what I've seen as a recommended name.


You have to put the condition somewhere. What about putting it inside
the partial? Then your layout will read clean.

Interesting read. I like the idea of using blocks instead of 'if'
statements, it makes your code more declarative. It boils down to is
another layer of abstraction that moves the contract from being
between the view and the model/controller, to being between the view
and the helper module. That is a useful abstraction, as the helper
module will tend to only change when the view does. It also provides
a single point of impact if the under lying condition logic must be
changed. Thanks for the link, I will be adopting this style on future

But a condition is still a condition, and conditionally yielding to a
block to hide an 'if' statement is just a fancy, albeit pretty,
condition. The purpose of hiding the condition in a helper is to make
the API more stable. When it comes to APIs of your own creation,
that's a really useful thing as they tend to change more frequently
than core APIs. But you should be able to count on some things being
their now and in the future, especially when it comes to the
foundational libraries your application is built on. When testing
those types of conditions, I wouldn't worry about hiding an 'if'
statement inside a helper unless it was for the sake of readability.
On the subject of a layout, the following seems like readable, non-
fragile code to me:

<% case controller.action_name -%>
<% when 'edit' -%>
<%= render :partial => 'shared/edit' %>
<% when 'show' -%>
<%= render :partial => 'shared/show' %>
<% end -%>

Thinking about it further, I think I would put all of that in a
helper. That would make my layout look like this:

<%= shared_action_partial %>

In application_helper.rb I'd have:

def shared_action_partial
  render :partial => 'shared/' + controller.action_name
  #ignore error if partial does not exist
  #re-throw error if it is some other type of error

This would seem to be very much in the Rails spirit, convention over
configuration. It adds another convention. If you have a shared/
<action> partial, it will use it, otherwise nothing happens.

Again, thanks for the link.