ZK Zhao
ZK Zhao

Reputation: 21573

Rails: display number of pending request, hide() if there is no number

In my Rails App, I want to display the number of Request.

If there is no pending Request, it shows <a>Request<\a>. If there is pending Request, it shouws its number, like <a>Request(2)<\a>.

Here is what I come up with

<%= link_to "Request (#{@pending_request_number if @pending_request_number > 0})", friendship_requests_friendships_path, class: 'pull-right' %>

But It can only show like Request () when there is no pending @requests. And I also want to configure the css class, which would make this line of code even longer.

How can I do it?

Upvotes: 0

Views: 70

Answers (1)

Viktor Vsk
Viktor Vsk

Reputation: 327

You can always split it to make it simpler:

<% if @pending_request_number.present? %>
  <%= link_to "Request (#{@pending_request_number})", friendship_requests_friendships_path, class: 'pull-right ative-class'  %>
<% else %>
  <%= link_to "Request", friendship_requests_friendships_path, class: 'pull-right'  %>
<% end %>

But when it gets too complicate, you should consider extracting it into Helpers. Don`t try doing everything in one line.

Upvotes: 1

Related Questions