简体   繁体   中英

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. 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. I have put the method into attr_accessible so I would think it should be 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:

 <% 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.

But readable_qty isn't a database field, so it doesn't belong in the list.

Try adding

attr_accessor :readable_qty

to 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM