我正在调试视图中控制器中的一个变量它显示了我期望看到的内容这是一个带有属性的对象
--- !ruby/hash:Parse::Object
alternativeColour: false
collection: 1
colour: 2
favourite: false
gender: 2
image: !ruby/object:Parse::File
parse_filename: dcefd915-eee7-4203-840b-sdsdsd34refdd-image3.jpg
url: http://files.parse.com/erre43-7f16-479f-97e6-343434/dcefd-image3.jpg
price: 23.42
productType: 2
recommended: false
size: 6
title: Bomber Jacket
createdAt: '2014-04-03T20:33:41.020Z'
updatedAt: '2014-06-18T19:03:24.220Z'
objectId: yZkfeeNJPm
在我的控制器我有:
def edit
@garment = Parse::Query.new("Garments").eq("objectId", params[:id]).get.first
end
这需要在另一个页面上点击的项目的objectId,并从parse.com数据库中获取匹配的项目,因此我可以编辑任何我想编辑的内容。
现在我的编辑页面加载良好没有我的形式,当我调试控制器变量的第一段代码,我复制并粘贴在上面它出现了什么。所以我确定我有一个返回对象。
我甚至还测试了一个标题,使用:
<%= debug @garment["title"] %>
这很好。所以现在我想添加我的表单,然后使文本字段的值和选择下拉框反映什么是存储在数据库中的特定项目。问题是我得到一个错误声明:
undefined method `title' for #<Parse::Object:0x007fer0h02y5c0>
下面是我的表单:
<%= form_for :garment, :url => adminpanel_url, :html => {:multipart => true} do |f| %>
<%= f.text_field :title, :class => "formFields", :placeholder => "Title" %>
<%= f.text_field :price, :class => "formFields", :placeholder => "Price" %>
<%= f.select :recommended, [["Yes", true], ["No", false]], :include_blank => "Make Recommended?" %>
<%= f.select :alternativeColour, [["Yes", true], ["No", false]], :include_blank => "Alternative Colour?" %>
<%= f.select :gender, [["Male", 1], ["Female", 2]], :include_blank => "Select Gender" %>
<%= f.select :productType, [["T-Shirt", 1], ["Shirt", 2], ["Hoody", 3], ["Gilet", 4], ["Jacket", 5]], :include_blank => "Select Product Type" %>
<%= f.select :size, [["Size 8", 1], ["Size 10", 2], ["Size 12", 3], ["Size 14", 8], ["Small", 4], ["Medium", 5], ["Large", 6], ["X-Large", 7]], :include_blank => "Select Product Size" %>
<%= f.select :colour, [["Black", 1], ["Blue", 2], ["Green", 3], ["Grey", 4], ["Navy", 5]], :include_blank => "Select Product Colour" %>
<%= f.select :collection, [["Tailor Made", 1], ["Raw & Uncut", 2], ["Custom", 3]], :include_blank => "Select Collecton" %>
<%= f.file_field :image %>
<%= f.file_field :image2 %>
<%= f.file_field :image3 %>
<%= f.file_field :image4 %>
<%= f.file_field :image5 %>
<%= f.file_field :image6 %>
<%= f.submit "Add Item", :id => "addItemButton" %> <br >
<% end %>
与我上面的调试结果,我本以为我的形式会工作得很好,但它没有,我得到这个错误:
undefined method `title' for #<Parse::Object:0x007fff0fedd1e0>
有什么问题吗?我已经做了一些谷歌和看了服务器日志,但似乎找不到什么是错的。只要我删除表单,只留下调试行,错误就消失了。所以这个错误肯定是由我的表单引起的。
这是我的模型,以防你想知道:
class Garment
include ActiveAttr::Model
#include ActiveModel::Validations
extend CarrierWave::Mount
attribute :alternativeColour
attribute :title
attribute :image
attribute :image2
attribute :image3
attribute :image4
attribute :image5
attribute :image6
attribute :price
attribute :recommended
attribute :gender
attribute :productType
attribute :size
attribute :colour
attribute :collection
mount_uploader :image, ImageUploader
非常感谢你的帮助。
感谢您的宝贵时间。
在做我原来的回答之前…尝试更改控制器代码以实例化服装类:
def edit
@garment = Garment.new Parse::Query.new("Garments").eq("objectId", params[:id]).get.first
end
我看了active_attr代码,看起来这将工作,如果, Parse::Query
返回一个哈希(似乎是)。
尝试将此添加到您的Garment
类定义:
def method_missing(method_name, *args)
if self[method_name.to_s].present?
self[method_name.to_s]
else
super
end
end
为了使setter正常工作,你必须追加这个。
这假设您的Parse::Query
调用在某个时刻实例化了Garment
类。这在哪里发生?
将服装改为实际对象,即
<%= form_for @garment, :url => adminpanel_url, :html => {:multipart => true} do |f| %>