1
votes

I'm trying to create a partial for my entity requirement which has an attribute of name. Requirements are associated to Projects. I am trying to render this partial for Requirements on the Project show page. I get the following error on load.

NoMethodError in Projects#show Showing .....xus/app/views/requirements/_requirement.html.erb where line #5 raised:

undefined method `name' for nil:NilClass

In my partial, _requirement.html.erb

<%= div_for requirement do %>
  <h4>
    <%= requirement.name  %>
  </h4>
  <%= requirement.requirement_type.name %>
  <%= requirement.requirement_priority.name %>
  <%= simple_format requirement.detail %>
<% end %>

The relevant section of my Project show template

<h3>Project Requirements</h3>
<div id="requirements_display">
  <%= render :partial => "requirements/requirement", :locals => {:requirement => @requirement} %>
</div>

<div id="requirements_module">
<h3>Add New Requirement</h3>
<%= render :file => "requirements/new" %>
</div>

I also tried rendering the partial using

<h3>Project Requirements</h3>
<div id="requirements_display">
  <%= render @project.requirements %>
</div>

But not look there, either. Any insight is greatly appreciated. Thanks all!

1
The error is complaining that requirement.requirement_type is nil, and yet you're trying to call .name on it. What is requirement_type? From your description, it sounds like you meant to put just requirement.requirement_type instead? - MrTheWalrus
Hi, thanks for the reply. requirement_type is another entity/model, basically a 'category'. Every requirement is assigned a requirement_type. When I run it without the .name method, it prints out "#<RequirementType:0x102fdc110> #<RequirementPriority:0x102fd78b8>" onto the template. I've also tested the relationship in the console and the relationships are in fact intact. For some reason the .name method isn't catching. - justinraczak
What does your Projects#show look like ? - prasvin
I've had many problems trying to pass parameters into partials. A couple of trouble shooting ideas: Can you successfully use "@requirement.name" in your partial? Does your partial code work if you move it back up into the main view and convert it to using "@requirement"? Are you sure "@requirement" is not coming in from your controller as nil? - Don Leatham
Thank you for all of the responses. Check below, I uncovered the problem. I had previously created a @requirement in the Show method of my Project which was creating an empty requirement with no reference. After removing that everything linked up. Thanks again! - justinraczak

1 Answers

0
votes

I resolved this issue! Some legacy code in my controller was creating an empty @requirement which was bugging things up!