2013-06-17 5 views
19

Używam generatora scaffold_controller na istniejącym modelu z istniejącymi atrybutami, ale formularze widokowe, które są generowane, nie mają żadnych kontrolek wejściowych dla odpowiednich atrybutów modelu - one to tylko puste formularze. Dlaczego?Generator kontrolek Railsy_kontrolera nie stosuje atrybutów modelu do widoków

np:

rails generate scaffold_controller User --skip --no-test-framework 

gdzie użytkownik ma już name i email atrybuty powinny generować formularze z nazwa i adres e-pól ...

+0

chciałby również wiedzieć, dlaczego – K2xL

Odpowiedz

32

To jest to, co ma robić. Dzwoniąc pod numer scaffold_controller, mówisz generatorowi, aby nie używał modelu. Jeśli chcesz mieć atrybuty formularza w widokach, musisz przekazać je do generatora, tak jak normalne rusztowanie.

rails g scaffold_controller User name email 
+0

dzięki ... pomocny –

+4

To jest do bani jeśli masz jak 20 atrybutów. .. – Philip

+2

Ciągle dużo lepiej niż alternatywa;) –

3

Zgadzam się, że to jest do bani musiał klawiaturze wszystkimi atrybutami się z niebezpieczeństwem nazwy misspellingu lub typu, gdy informacja jest po prostu siedzi tam w modelu. Oto łatka małpy, którą napisałem, aby interpolować nazwy i typy kolumn (przynajmniej w Railsach 4). Umieść ten kod w pliku .rb w # {Rails.root}/config/katalog inicjalizatory:

# patch to scaffold_controller to read model attributes 
# if none specified on command line (and model exists) 
# usage: rails g scaffold_controller <MODEL> 

if ARGV.size > 0 and ARGV[0] == "scaffold_controller" 
    puts "\n\n\n\n" 
    puts "monkey patch attributes at #{Time.now}" 

    Rails::Generators::NamedBase.class_eval do 

     # parse_attributes! converts name:type list into GeneratedAttribute array 
     # must be protected; thor enumerates all public methods as commands 
     # and as I found out will call this and crash otherwise 
     protected 
     def parse_attributes! #:nodoc: 
      # get model columns into col:type format 
      self.attributes = get_model_attributes if not self.attributes or self.attributes.empty? 
      # copied from default in named_base.rb 
      self.attributes = (self.attributes || []).map do |attr| 
      Rails::Generators::GeneratedAttribute.parse(attr) 
      end 
     end 

     # get model columns if no attributes specified on command line 
     # fake it by creating name:type args 
     private 
     def get_model_attributes 
      # fill from model 
      begin 
       mdl = class_name.to_s.constantize 
       # don't edit id, foreign keys (*_id), timestamps (*_at) 
       attrs = mdl.columns.reject do |a| 
        n = a.name 
        n == "id" or n.end_with? "_id" or n.end_with? "_at" 
       end .map do |a| 
        # name:type just like command line 
        a.name+":"+a.cast_type.type.to_s 
       end 
       puts "model_attributes(#{class_name})=#{attrs}" 
       return attrs 
      rescue => ex 
       puts ex 
       puts "problem with model #{class_name}" 
       return nil 
      end 
     end 

    end 

end 
+0

Chciałem tylko dać ci znać, że uratowałeś mi tyłek. Dzięki –

+0

w szynach 5 to zawiedzie komunikatem "niezdefiniowana metoda cast_type". spróbuję zbadać, kiedy mam czas. –

Powiązane problemy