Note that there are some explanatory texts on larger screens.

plurals
  1. POundefined method `model_name' for NilClass:Class with Partials and Form for
    text
    copied!<p>I am having a weird issue where my rails application seemingly isn't reading the local variable passed to the partial, but <strong><em>only</em></strong> inside of <code>form_for</code>.</p> <p>I have a set up a button where a user can bookmark an object, let's call it <code>zoo</code>.</p> <p>The code looks like this:</p> <p>Zoo Listing View:</p> <pre><code>&lt;li&gt;&lt;%= render 'directory/bookmarks', zoo: zoo %&gt;&lt;/i&gt;&lt;/li&gt; </code></pre> <p>Then the directory/bookmarks partial:</p> <pre><code>&lt;% if user_signed_in? %&gt; &lt;% if current_user.has_zoo_bookmarked_already? %&gt; &lt;%= form_for(current_user.bookmarks.find_by_zoo_id(zoo.id), html: { method: :delete }, remote: true) do |f| %&gt; &lt;%= button_tag do %&gt; &lt;button class="btn btn-lg btn-block btn-primary"&gt;remove bookmark &lt;% end %&gt; &lt;% end %&gt; &lt;% else %&gt; &lt;%= form_for(current_user.bookmarks.build(zoo_id: zoo.id)) do |f| %&gt; &lt;%= f.hidden_field :zoo_id, value: zoo.id %&gt; &lt;%= button_tag do %&gt; &lt;button class="btn btn-lg btn-block btn-primary"&gt;bookmark &lt;% end %&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;% else %&gt; &lt;% end %&gt; </code></pre> <p>Anyway, the problem is the <code>zoo.id</code> on the 3rd line is not being evaluated. I get this error.</p> <p><strong>undefined method `model_name' for NilClass:Class</strong></p> <p>The weird part is that locals are being read on the page.</p> <p>If I place this code snippet <code>&lt;%= zoo.id %&gt;</code> anywhere on the page, I get the id. Then, the <code>find_by_zoo_id</code> is being evaluated also...if I put <code>255</code> a hardcoded number, it works. Or if I set it to a variable, <code>@id = 255</code> and then pass in the <code>@id</code>, it works. </p> <p>The zoo.id only does not work inside of the form_for for some reason. If I set it to an instance variable on the page, <code>@id = zoo.id</code>, that doesn't work either. </p> <p>Any ideas? I'm sure it is something minor.</p> <p>EDITED:</p> <p>The relationship is has_many :through. Forgot to mention, I use this code for a different association in the same application and it works fine.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload