What is the best way to re-use conditional logic in a Rails 4 app? -


my goal display user's profile image. users not have uploaded profile images, i'd display default profile image them.

i can in view this:

    <% if post.user.avatar.presence %>     <%= image_tag post.user.avatar.url %>     <% else %>     <%= link_to user_path(post.user), :class => "default-profile-image" %>     <i class="fa fa-user"></i>     <% end %>     <% end %> 

but seems sub-optimal. if want use logic elsewhere? don't want repeat code in views.

what optimal way recycle code elsewhere in app?

thank you!

partials.

you should create app/views/users/_avatar.html.erb partial, move code in question there, , each time want render avatar, use

<%= render 'users/avatar', user: post.user %> 

Comments