Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8815425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:22:27+00:00 2026-06-14T04:22:27+00:00

I have a user model, and event model, and an asset model. A user

  • 0

I have a user model, and event model, and an asset model.

A user can have multiple events.
An event can have multiple assets (images)

Now what I am trying to do is display one (any) event image on a page. Currently I have the following.

Controller

@user = current_user  
@events = @user.events  

View

<% @events.each do |e| %>
  <li>
  <% if e.assets.nil? %> 
    <%= image_tag("img36.jpg" , :size => "280x230") %>
  <% else %>
  <%=image_tag e.assets.first.path.url %>   
  <% end %>
  <div class="bar">
  <strong class="heading"><%= e.name %></strong>
  <ul class="menu">
    <li><a class="time" title="Time" href="#"><%= e.date %></a></li>
    <li><a class="comments" title="Comments" href="#">53</a></li>
    <li><a class="favourites" title="Favourites" href="#">87</a></li>
    <li><a class="view" title="Views" href="#">242</a></li>
 </ul>
 </div>
 <p><%= e.description %><a href="#">more &raquo;</a></p>
 </li>
<% end %>

“path”is the string field in the assets table that contains the image path. I’m getting the following error right now.

undefined method `path' for nil:NilClass

Any ideas? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:22:28+00:00Added an answer on June 14, 2026 at 4:22 am

    The problem is when there are no assets associated with an event, the value of e.assets will be an empty array, not nil. So what is happening is that e.assets passes the nil? conditional, then you take e.assets.first which is nil (because the array is empty), and then you try calling path on that which obviously doesn’t work.

    To fix the problem just change:

    <% if e.assets.nil? %> 
    

    to:

    <% if e.assets.empty? %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have one model (event) that has multiple polymorphic HABTM associations
I have a model that looks like this: class Invite(models.Model): user = models.ForeignKey(User) event
I have a User model that has many posts. Im trying to render the
I have a dashboard page where the user can enter an asset code or
I have an Event model, which stores an event feed for each user. I
Let's say I have such model class Event(models.Model) users_count = models.IntegerField(default=0) users = models.ManyToManyField(User)
I have a User Model(:name, :password, :email), and Event model(:name, :etc) and Interest model
I have a User Model(:name, :password, :email), and Event model(:name, :etc) and Interest model
I have a model for Event that I would like to have a user
I have a User model, an Event Model, an Event Priority Model and a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.