Helper Method detect post - comment from user

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 .

Answers


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.


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' %>

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 %>

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 %>

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." %>

Need Your Help

svn blame properties with command line svn

svn properties blame

I am trying to get an svn blame out of svn for the svn properties. I expected there would be something like:

Deploy multiple instances of the same application on JBoss

deployment java-ee jboss ejb-3.0 seam

I want to deploy multiple instances of the same Seam/EJB3 application that differ only in their database settings on the same JBoss server instance. That way I want to provide different demo instan...