Ruby on rails Rails 3:何处查询问题
我正在学习Rails3中的“where”,我想知道为什么我的代码会给我一个NoMethod错误Ruby on rails Rails 3:何处查询问题,ruby-on-rails,ruby-on-rails-3,activerecord,Ruby On Rails,Ruby On Rails 3,Activerecord,我正在学习Rails3中的“where”,我想知道为什么我的代码会给我一个NoMethod错误 def create #get form values from 'new' page @post = Post.new(params[:post]) #search for the inventory item with the sku code the user typed in the form @item = Inventory.where(:sku_co
def create
#get form values from 'new' page
@post = Post.new(params[:post])
#search for the inventory item with the sku code the user typed in the form
@item = Inventory.where(:sku_code => @post.sku_code)
#set post variable to a variable from the row with the sku code
@post.detail = @item.detail
@post.save
end
通过搜索SKU,我希望检索相同id中的其他变量,并将它们设置为我的@post变量。有人能帮我吗?假设SKU代码是唯一的,你必须这样做
@post = Post.new(params[:post])
@post.detail = Inventory.where(:sku_code => @post.sku_code).first.try(:detail)
first
将从数据库中获取第一条(可能是唯一的)记录<如果返回的库存
不是零,则code>try将尝试获取详细信息
检查以了解有关
尝试方法的更多信息。感谢您的回复。我检查了参考资料,它们是正确的。它给了我一个模板丢失的错误:缺少模板发布/创建,应用程序/创建。有什么想法吗?通常你想在创建后重定向,例如转到ShowAction。或者,如果无法保存帖子,则通过呈现新内容再次显示表单。也看到