Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy isn't this portion of HTML rendering and why do I get a undefined method `cardsets' for nil:NilClass in Rails?
    primarykey
    data
    text
    <p>I'm trying to add a flashcards(cardsets) feature in Rails and I'm having some problems implementing this as I'm getting an <code>undefined method</code>cardsets' for nil:NilClass` error </p> <p>I can get the page to render if I change: <code>&lt;% if @user.cardsets.any? %&gt;</code> to <code>&lt;% unless @cardsets.any? %&gt;</code> but the corresponding cardsets are being shown when I do this. Here is the html.erb code that is producing this error:</p> <pre><code>&lt;% provide(:title, "Flashcards") %&gt; &lt;h1&gt;Flashcards&lt;/h1&gt; &lt;div class="row"&gt; &lt;aside class="span3"&gt; &lt;%= render "shared/cardset_form" %&gt; &lt;/aside&gt; &lt;div class="span6"&gt; **&lt;% if @user.cardsets.any? %&gt;** &lt;h3&gt;Flashcard Sets (&lt;%= @user.cardsets.count %&gt;)&lt;/h3&gt; &lt;ol class="cardsets"&gt; &lt;% @user.cardsets.each do |cardset| %&gt; &lt;li&gt; &lt;span class="topic"&gt;&lt;%= link_to cardset.topic, show_cardset_path(cardset) %&gt;&lt;/span&gt; &lt;%= link_to "edit", edit_cardset_path(cardset) %&gt; &lt;%= render partial: "shared/delete_link", locals: {item: cardset} %&gt; &lt;/li&gt; &lt;% end %&gt; &lt;/ol&gt; &lt;% end %&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>I've starred the line that is producing the <code>undefined method</code>cardsets' for nil:NilClass` error. </p> <p>This is what's in my cardsets_controller.rb:</p> <pre><code>class CardsetsController &lt; ApplicationController before_action :signed_in_user, only: [:new, :index, :create, :edit, :destroy, :update] before_action :correct_user, only: [:edit, :update, :destroy] def index @cardsets = Cardset.all end def show @cardset = Cardset.find(params[:id]) end def create @cardset = current_user.cardsets.build(cardset_params) if @cardset.save flash[:success] = "A new set of flashcards have been created!" redirect_to @cardset else render "new" end end def new @cardset = Cardset.new end def edit end def update @cardset = Cardset.find(params[:id]) if @cardset.update_attributes(cardset_params) flash[:success] = "You've updated your flashcards!" redirect_to @cardset else render "edit" end end def destroy @cardset = Cardset.find(params[:id]) @cardset.delete redirect_to cardsets_path end private def cardset_params params.require(:cardset).permit(:topic, :description) end def correct_user @cardset = current_user.cardsets.find_by_id(params[:id]) redirect_to root_url if @cardset.nil? end end </code></pre> <p>Here is the code in my cardset.rb file:</p> <pre><code>class Cardset &lt; ActiveRecord::Base belongs_to :user validates :user_id, presence: true validates :topic, presence: true end </code></pre> <p>Here is the code in my user.rb file:</p> <pre><code>class User &lt; ActiveRecord::Base has_many :cardsets (...a bunch of other code...) end </code></pre> <p>If anyone could provide some help or suggestions on where I may be going wrong I'd greatly appreciate it! </p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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