I';当RubyonRails中确实存在一个未定义的方法时,我得到一个形式为_的未定义方法
我正在从视图中的控制器调试一个变量,它向我显示了我希望看到的具有属性的对象:I';当RubyonRails中确实存在一个未定义的方法时,我得到一个形式为_的未定义方法,ruby,ruby-on-rails-3,ruby-on-rails-4,form-for,activemodel,Ruby,Ruby On Rails 3,Ruby On Rails 4,Form For,Activemodel,我正在从视图中的控制器调试一个变量,它向我显示了我希望看到的具有属性的对象: --- !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:
--- !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
<%= debug @garment["title"] %>
<%= 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 %>
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
这将获取在其他页面上单击的项目的objectId,并从parse.com数据库中获取匹配的项目,以便我可以编辑任何我想要的内容
现在,我的编辑页面在没有表单的情况下可以正常加载,当我调试控制器变量时,我复制并粘贴的第一段代码就会出现。所以我确定我有一个返回的对象
我甚至还使用:
def edit
@garment = Parse::Query.new("Garments").eq("objectId", params[:id]).get.first
end
<%= debug @garment["title"] %>
<%= 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 %>
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
非常感谢你的帮助
谢谢您的时间。更改:将衣服更改为实际对象,即
<%= form_for @garment, :url => adminpanel_url, :html => {:multipart => true} do |f| %>
adminpanel_url,:html=>{:multipart=>true}do | f |%>
在做我的原始答案之前。。。尝试更改控制器代码以实例化garment类:
def edit
@garment = Garment.new Parse::Query.new("Garments").eq("objectId", params[:id]).get.first
end
我查看了活动的_attr代码,如果Parse::Query
返回了一个散列(看起来是这样的),那么它看起来会起作用
旧答案
尝试将其添加到您的服装类别定义中:
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
类。哪里会发生这种情况?
您在这里不是调用方法,而是调用散列的键。你确定那不是你的问题吗?您在garment
变量中得到的是散列,而不是对象,rails表单助手正在尝试调用对象方法或者它可能不是一个实际的散列,但它是一个行为类似于散列的对象。调用@garment.title
有效吗?或者您只能通过@garment['title']
获得它?但这就是我从parse.com调用对象属性的方式。看这里,我没有其他方法来读取对象属性。错误来自表单。即使删除调试代码,错误仍然存在。Parse::Query
在什么时候实例化Garment
类?您的错误提到了#
,但没有提到您定义的实际Garment
类,是否正在使用该类?@nzifnab我使用的是parse.com中的数据库,而不是定义的数据库。基本上这是一个无表应用程序。不使用活动记录。form\u form@garment
和form\u for:garment
是完全相同的,如果您有一个名为@garment
的实例变量可用。如果您使用符号,它会查找该名称的实例变量_name@LondonGuy对于该错误,您可以将extend-ActiveModel::Naming
添加到对象的定义中。我认为这就是编辑操作中发生的情况。毕竟我得到了一个返回的对象。我如何使用上面的代码。我是否按原样粘贴到模型中?我不知道,您的对象转储是说它们是Parse::object
类,而不是Garment
类。您是否需要为类命名Parse::Garment
?或者手动实例化它还是什么?在您实际处理Garment
类之前,我不确定此代码是否有效。(例如,如果您调用Rails.logger.debug(@garment.class.to_s)
我怀疑它不会说garment
或antying)。@LondonGuy我已经更新了我的答案,试着像我提到的那样手动实例化garment
类,看看它是否可以访问@garment.title
之类的内容。如果可以的话,您不需要添加我在这里首先缺少的方法。