SORU
2 ŞUBAT 2011, ÇARŞAMBA


Model sınıf adı denetleyicisi sınıf adına göre Nasıl?raylar:

class HouseBuyersController < ...
  def my_method
    # How could I get here the relevant model name, i.e. "HouseBuyer" ?
  end
end

CEVAP
21 Temmuz 2011, PERŞEMBE


Bu edecektir:

class HouseBuyersController < ApplicationController

  def index
    @model_name = controller_name.classify
  end

end

Bu genellikle denetleyici eylemleri özetleme gereklidir:

class HouseBuyersController < ApplicationController

  def index
    # Equivalent of @house_buyers = HouseBuyer.find(:all)
    objects = controller_name.classify.constantize.find(:all)
    instance_variable_set("@#{controller_name}", objects)
  end

end

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • BDGcustoms

    BDGcustoms

    24 NİSAN 2011
  • Garrett Müller

    Garrett Mül

    26 HAZİRAN 2009
  • The Computer Chronicles

    The Computer

    7 Kasım 2012