这个错误困扰了我好几个星期。当我尝试Item
从指定的User
and构建 an 时List
,Item
会创建 ,但Wish
不会创建关联。
如果我尝试这样做@item.lists.first.name
,则会返回错误:
undefined method 'name' for nil:NilClass
我对 Rails 很陌生,所以我确定有些东西我错过或误解了。非常感谢任何帮助!
我有四个模型:
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
我想Item
从lists_controller.rb
显示操作创建一个新的:
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
我的路由文件如下所示:
Wishlist::Application.routes.draw do
resources :users do
resources :lists, only: [:show, :create, :destroy]
end
resources :items, only: [:show, :new, :create, :destroy]
表格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>
和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 %>
更新
从日志:
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)