bradpotts
bradpotts

Reputation: 329

Remove Logic from Rails Views

Is there a way I can clean up my views when it comes to an example below like user links? I remember reading in a book remove if and else statements put it into a controller or something. Can't find it at the moment.

<% if current_user %>
    <%= link_to edit_post_path(blog) do %>
        <i class="fas fa-edit"></i> Edit Post
    <% end %>
    <%= link_to blog_post_index do %>
        <i class="fas fa-trash"></i> Delete Post
    <% end %>
<% end %>

Upvotes: 0

Views: 490

Answers (2)

Jerry
Jerry

Reputation: 198

Or make a shared folder and put the code in a template.

Like: app > views > shared > links > _user_link.html

Then call in your views:

= render "shared/links/user_link"

You can also parse extra info like:

= render "shared/links/user_link", f: f

Upvotes: 0

Vishal
Vishal

Reputation: 818

You can use rails view helpers for such code.

See..

https://api.rubyonrails.org/classes/ActionController/Helpers.html

Upvotes: 2

Related Questions