0

I'm trying to create a helper method that will display {user.name} has no submitted posts." on the profile show view of user if they haven't yet submitted any posts and display the number posts they have . currently on my show view i have <%= render @user.posts %> which displays nothing when there are 0 posts submitted.

the partial for post is :

<div class="media">
  <%= render partial: 'votes/voter', locals: { post: post } %>
  <div class="media-body">
    <h4 class="media-heading">
      <%= link_to post.title, topic_post_path(post.topic, post) %>
      <%= render partial: "labels/list", locals: { labels: post.labels } %>
    </h4>
    <small>
      submitted <%= time_ago_in_words(post.created_at) %> ago by <%= post.user.name %> <br>
      <%= post.comments.count %> Comments
    </small>
  </div>
</div>

ive tried :

  def no_post_submitted?(user)
      user.post.count(0)
      "{user.name} has not submitted any posts yet."
  end

on my user show view :

<%= if no_post_submitted?(@user) %>
<%= render @user.posts %>

which im more than sure is wrong but i have no idea how to implement this method .

5 Answers5

3

Where you are using render @user.posts you can just add a simple conditional:

<% if @user.posts.empty? %>
  <p><%= @user.name %> has no submitted posts</p>
<% else %>
  <%= render @user.posts %>
<% end %>

There wouldn't be much point creating a helper for this unless you need to use it in multiple places.

Jon
  • 9,840
  • 2
  • 31
  • 44
1

Render collection returns nil if the collection is empty so you can use the || operator:

<%= render @user.posts || "{@user.name} has not submitted any posts yet." %>

Or if there is more code render another partial:

<%= render @user.posts || render 'no_posts' %>
0

In Ruby methods automatically return the last value so this method:

def no_post_submitted?(user)
  user.post.count(0)
  "{user.name} has not submitted any posts yet."
end

Will always return a string - if you use a string literal in a condition it will be evaluated as true with the warning warning: string literal in condition. Also that is not how you use count - passing 0 will cause it to query on column 0 or just error.

So to fix the method you would do:

def no_post_submitted?(user)
  user.posts.empty?
end

However that conditional is so simple that it does not really warrant a helper method. Instead you would just write:

<%= if user.post.any? %>
  <%= render @user.posts %>
<% else %>
  <%= "{user.name} has not submitted any posts yet." %>
<% end %>
max
  • 76,662
  • 13
  • 84
  • 137
0

There are a couple of problems with your solution. Remember, rails is more about convention over configuration.

Your method no_post_submitted? should actually return true/false since its a method ending with ?. Also it should be named no_posts_submitted? for clarity. It should look something like this:

  def no_post_submitted?(user)
    user.posts.count > 0
  end

Then, there should be another helper method that will print your required message, Something like:

def no_posts_message(user)     
  "{user.name} has not submitted any posts yet."
end

And eventually you can all plug it in like this:

<% if no_posts_submitted?(user) %>
 <%= no_posts_message(user) %>
<% else>
 <%= render @user.posts %>
<% end %>
aliibrahim
  • 1,105
  • 9
  • 18
0

As per the docs:

In the event that the collection is empty, render will return nil, so it should be fairly simple to provide alternative content.

<h1>Products</h1>
<%= render(@products) || "There are no products available." %>

--

So...

  <%= render(@user.posts) || "#{@user.name} has not submitted any posts yet." %>
Richard Peck
  • 73,250
  • 8
  • 84
  • 139