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 9326945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T05:12:58+00:00 2026-06-19T05:12:58+00:00

This error has been troubling me for weeks. When I try to build an

  • 0

This error has been troubling me for weeks. When I try to build an Item from a specified User and List, the Item is created, but the association, Wish is not.

If i try to do @item.lists.first.name it returns an error:

undefined method 'name' for nil:NilClass

I’m very new to rails, so I’m sure there is something that I have missed or misunderstood. Any help is thus much appreciated!

I have four models:

class User < ActiveRecord::Base
  has_many :lists, dependent: :destroy
  has_many :wishes, through: :lists
  has_many :items, through: :wishes

class List < ActiveRecord::Base
  belongs_to :user
  has_many :wishes, dependent: :destroy
  has_many :items, through: :wishes

class Wish < ActiveRecord::Base
  belongs_to :list
  belongs_to :item

class Item < ActiveRecord::Base
  has_many :wishes
  has_many :lists, through: :wishes

I want to create a new Item from lists_controller.rb show action:

class ListsController < ApplicationController
  def show
    @user = User.find(params[:user_id])
    @list = @user.lists.find(params[:id])
    @item = @list.items.build if current_user?(@user)
    @items = @list.items
  end

class ItemsController < ApplicationController
    def create
        @list = current_user.lists.find(params[:list_id])
        @item = @list.items.build(params[:item])
    if @item.save
      flash[:success] = "Item created!"
      redirect_to @item
    else
      render 'new'
    end
end

My route file looks like this:

Wishlist::Application.routes.draw do
  resources :users do
    resources :lists, only: [:show, :create, :destroy]
  end
  resources :items, only: [:show, :new, :create, :destroy]

The form lists/show.html.erb:

   <div class="modal-body">
     <%= form_for(@item, html: { multipart: true }) do |f| %>
  <div class="field">
        <%= render 'items/fields', f: f %>
         <%= hidden_field_tag :list_id, @list.id %>
  </div>
   </div>

and items/fields:

<%= render 'shared/error_messages', object: f.object %>

<%= f.label :image %>
<%= f.file_field :image %>

<%= f.label :remote_image_url, "or image URL" %>
<%= f.text_field :remote_image_url %>

<%= f.label :title %>
<%= f.text_field :title %>

<%= f.label :link %>
<%= f.text_field :link %>

Update

From the log:

Processing by ItemsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"pcDVdaDzZz4M17Kwjx8mKw6tTF9MjUvx1woTzaKRWJY=", "item"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fecdd1fd890 @original_filename="profile.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"item[image]\"; filename=\"profile.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/var/folders/6y/j8zfcgmd02x5s439c0np8fjh0000gn/T/RackMultipart20130216-8413-3vzjuj>>, "remote_image_url"=>"", "title"=>"YES", "link"=>"SDFs"}, "list_id"=>"1", "commit"=>"Add"}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."remember_token" = '5AXS8-7-YRyLyGDKYHIZRg' LIMIT 1
  List Load (0.2ms)  SELECT "lists".* FROM "lists" WHERE "lists"."user_id" = 1 AND "lists"."id" = ? LIMIT 1  [["id", "1"]]
   (0.1ms)  begin transaction
  SQL (0.7ms)  INSERT INTO "items" ("created_at", "image", "link", "title", "updated_at") VALUES (?, ?, ?, ?, ?)  [["created_at", Sat, 16 Feb 2013 20:57:10 UTC +00:00], ["image", "profile.jpg"], ["link", "SDFs"], ["title", "YES"], ["updated_at", Sat, 16 Feb 2013 20:57:10 UTC +00:00]]
   (2.7ms)  commit transaction
Redirected to http://localhost:3000/items/1
Completed 302 Found in 830ms (ActiveRecord: 4.0ms)
  • 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-19T05:12:59+00:00Added an answer on June 19, 2026 at 5:12 am

    After lots of googling, and trial and error, I found that I had to build the Wish explicitly. The create action ended up looking like this:

    def create
        @list = current_user.lists.find(params[:list_id])
        @item = Item.create!(params[:item])
        @item.wishes.build(list_id: @list.id)
        if @item.save
          flash[:success] = "Item created!"
          redirect_to @item
        else
          render 'new'
        end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Getting this error when try to add an item to my repositories/context: Collection has
I know this error has been beaten to death, but I cannot seem to
I know this type of error has been raised many times before, but I
A buddy of mine has been getting this error mesage when trying to run
Getting this error with jquery & jquery.form. Site has been live for awhile..upgraded to
I'm getting a java.awt.FontFormatException: Unrecognised Font Format error, and I believe this has been
this code produces 17 error C2995: function template has already been defined; there was
Has anyone met this error? Right now i am trying to do switch from
I am receiving this error: The ViewData item that has the key 'DepartmentId' is
We use rails version 2.3.5 This error has been reported in SO here I

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.