[英]Rails methods in model not accessible in view
I have been fighting a annoying thing all day. 我整天都在打闹。
I have a model class which connects to another with has_many connection. 我有一个模型类,它通过has_many连接连接到另一个。 It works ok, but when I try to access non db methods from the class I get an error that the model does not contain the method.
它可以正常工作,但是当我尝试从类中访问非db方法时,出现一个错误,该模型不包含该方法。 I have put the method into attr_accessible so I would think it should be accessible.
我将方法放入attr_accessible中,因此我认为它应该是可访问的。
Here is the model for the first class: 这是头等舱的模型:
class Recipe < ActiveRecord::Base
validates :name, :presence => true,
:length => { :maximum => 100 }
attr_accessible :ingredient_recipes_attributes
has_many :ingredient_recipes, :foreign_key => "recipe_id"
has_many :ingredients, :through => :ingredient_recipes
accepts_nested_attributes_for :ingredient_recipes, :allow_destroy => true
end
and the second model: 第二种模式:
class IngredientRecipe < ActiveRecord::Base
attr_accessor :ingrNameFromUser
set_primary_key :ingredient_id, :recipe_id
attr_accessible :ingrNameFromUser, :readable_qty, :ingredient_description
belongs_to :recipe, :foreign_key => "recipe_id"
belongs_to :ingredient, :foreign_key => "ingredient_id"
# belongs_to :ndbfoodde, :foreign_key => "ingredient_id"kki"
delegate :description, :to => :ingredient, :prefix => "ingredient", :allow_nil => true
def readable_qty
qty.to_s + " gr."
end
def readable_qty=(qty)
self.qty = qty.to_f + 2
end
end
when I try to access the readable_qty through the view like this: 当我尝试通过这样的视图访问visible_qty时:
<% f.fields_for :ingredient_recipes do |rif| %>
<td>
<%= rif.autocomplete_field :ingrNameFromUser, recipes_autocomplete_ingredient_description_path, :value => @recipe.ingredient_recipes[i].ingrNameFromUser, :width=>1000, :size=>60 %>
</td>
<td>
<%= rif.text_field :readable_qty %>
</td>
I get an error: 我收到一个错误:
undefined method `readable_qty' for #<IngredientRecipe:0x00000103322068>
Extracted source (around line #59):
56: </td>
57:
58: <td>
59: <%= rif.text_field :readable_qty %>
60: </td>
61:
62: <td>
Can anybody see what I'm doing wrong? 有人可以看到我在做什么吗?
At first glance, I'd say that given readable_qty
is a virtual attribute (ie it isn't in the database and has instance methods as setters and getters, you should remove it from the attr_accessible
list. This list defines which attributes stored in the database can be updated by mass assignment. 乍一看,我想说的是,给定的
readable_qty
是一个虚拟属性(即它不在数据库中,并且具有作为setter和getter的实例方法,您应该将其从attr_accessible
列表中删除。此列表定义了哪些属性存储在可以通过批量分配来更新数据库。
But readable_qty
isn't a database field, so it doesn't belong in the list. 但是
readable_qty
不是数据库字段,因此它不属于列表。
Try adding 尝试添加
attr_accessor :readable_qty
to IngredientRecipe. 到IngredientRecipe。
If you're running the server in production mode and you've made this change to the model, then the model will not be reloaded automatically and the new method would not be available. 如果您在生产模式下运行服务器,并且已对模型进行了更改,则该模型将不会自动重新加载,并且新方法将不可用。 You must restart the server for it to take effect.
您必须重新启动服务器才能使其生效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.