Is it acceptable keep logic in view templates?
Is it acceptable keep logic in view templates 开发者_StackOverflow(in MVC terms and Rails specifically)? Is there any way to avoid something like this?
<% if current_user %>
Welcome, <%= current_user.name %>.
<%= link_to "Sign Out", signout_path %>
<% else %>
<%= link_to "Sign in with Twitter", "/auth/twitter" %>
<% end %>
Or am I inventing a "bicycle" and the upper stuff goes well?
Comes down to preference of course. I would say that the above is pretty acceptable. Simple conditions and loops are pretty standard in a view. I would say the things to avoid would be assigning variables, hitting models for data you haven't already gathered, etc.
If you don't want anything like tha in there, you can always build helpers. This kind of thing has to be somewhere.
edit:
a good rule of thumb is "does this code directly relate to the presentation?" I would say the answer regarding your above case is yes.
精彩评论