我想为我的
Item
模型,然后将其保存在
create
我的方法
ItemsController
处理POST请求。但似乎什么都没发生。
# app/models/item.rb
class Item < ApplicationRecord
belongs_to :user
belongs_to :list
...
attr_accessor :list_id, :user_id # this should allow me to assign to fields directly
end
# app/controllers/items_controller.rb
# POST /items or /items.json
def create
@item = Item.new(item_params) # Original generated code
@item.list_id = 1 # I am assigning a value here
@item.user_id = session[:user_id] # and here
logger.debug session[:user_id] # => 1
logger.debug @item # => #<Item id: nil, name: "sad", quantity: 1, user_id: nil, list_id: nil, created_at: nil, updated_at: nil>
...
end
为什么
@item.user_id
和
@item.list_id
仍显示为
nil
任务刚结束?