1. 程式人生 > 實用技巧 >[ROR] 必知必會知識點[持續總結中...]

[ROR] 必知必會知識點[持續總結中...]

一、Ruby語言本身

  • require、load和include。
    require:形式為require 'active_support/concern'。 請求一個模組,需要不帶副檔名的檔名,程式中多次requrie這個模組不會多次裝載,ruby會使用記憶體中的快取;
    load:形式為require 'active_support/concern.rb'。請求裝載一個模組,需要帶副檔名,每次執行到load,不管以前是否裝載過,ruby都會再次讀取此模組;
    include:形式為:include ActiveSupport::Concern。mixin一個模組,使用的是模組的名稱而不是檔名,其實和前兩個沒啥關係,很多時候需要一起使用;

  • 檢視一個類的方法。
    methods、public_methods、private_methods、instance_methods、public_instance_methods
    irb(main):025:0> Test2.instance_methods
    => [:m1, :to_json, :blank?, :as_json, :acts_like?, :deep_dup, :present?, :duplicable?, :with_options, :html_safe?, :in?, :presence_in, :dclone, :`, :to_yaml, :to_param, :to_query, :instance_values,
    :instance_variable_names, :presence, :pretty_print_cycle, :pretty_print_inspect, :pretty_print, :pretty_print_instance_variables, :require_dependency, :unloadable, :require_or_load,
    :load_dependency, :try, :try!, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :instance_variable_get, :instance_variables, :instance_variable_set,
    :protected_methods, :private_methods, :public_send, :method, :public_method, :singleton_method, :class_eval, :pretty_inspect, :define_singleton_method, :extend, :suppress_warnings, :to_enum,
    :enum_for, :<=>, :===, :=~, :!~, :eql?, :respond_to?, :gem, :freeze, :inspect, :object_id, :send, :to_s, :display, :class, :nil?, :hash, :dup, :singleton_class, :clone, :then, :itself,
    :yield_self, :untaint, :taint, :tainted?, :trust, :untrust, :untrusted?, :singleton_methods, :frozen?, :methods, :public_methods, :equal?, :!, :==, :instance_exec, :!=, :instance_eval,
    :__id__, :__send__]

      

    # 為了便於檢視,可以排一下序
    irb(main):034:0> Test2.instance_methods.sort
    => [:!, :!=, :!~, :<=>, :==, :===, :=~, :__id__, :__send__, :`, :acts_like?, :as_json, :blank?, :class, :class_eval, :clone, :dclone, :deep_dup, :define_singleton_method, :display, :dup, 
    :duplicable?, :enum_for, :eql?, :equal?, :extend, :freeze, :frozen?, :gem, :hash, :html_safe?, :in?, :inspect, :instance_eval, :instance_exec, :instance_of?, :instance_values,
    :instance_variable_defined?, :instance_variable_get, :instance_variable_names, :instance_variable_set, :instance_variables, :is_a?, :itself, :kind_of?, :load_dependency, :m1, :method,
    :methods, :nil?, :object_id, :presence, :presence_in, :present?, :pretty_inspect, :pretty_print, :pretty_print_cycle, :pretty_print_inspect, :pretty_print_instance_variables, :private_methods,
    :protected_methods, :public_method, :public_methods, :public_send, :remove_instance_variable, :require_dependency, :require_or_load, :respond_to?, :send, :singleton_class, :singleton_method,
    :singleton_methods, :suppress_warnings, :taint, :tainted?, :tap, :then, :to_enum, :to_json, :to_param, :to_query, :to_s, :to_yaml, :trust, :try, :try!, :unloadable, :untaint, :untrust,
    :untrusted?, :with_options, :yield_self] irb(main):035:0>

     

二、UI檢視(ActionView)相關

  • AJAX訪問錯誤引發錯誤“ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken)” , 在controller中新增下面命令,跳過校驗
    skip_before_action :verify_authenticity_token

三、ActiveRecord

  • 命令列生成migration時,複雜資料型別(decimal)的語法 :
    # decimal的單引號時必要的
    rails g model Abc 'price:decimal{5,2}' supplier:references{polymorphic}

  • Model中自定義屬性(資料庫表中不存在),需要序列化為json的需要覆蓋as_json
     # 擴充套件json物件,新增pinyin和jianpin屬性
      def as_json data    
        if self.respond_to? 'name'
          ps = { pinyin: self.pinyin_name, jianpin: self.jianpin }
          super.merge ps
        end
      end

  • ActiveRecord物件 的修改痕跡追蹤,記錄在此

四、系統相關

  • 建立臨時檔案時,如何指定副檔名。rails建立臨時檔案很方便,直接Tempfile::newnew(basename="", tmpdir=nil, mode: 0, **options),但是如何給定一個副檔名呢?原來只需要在第一項引數位置給出一個數組即可,看程式碼

    irb(main):001:0> tf = Tempfile::new
    => #<Tempfile:/tmp/20201120-38479-dg7h2v>
    irb(main):002:0> tf.path
    => "/tmp/20201120-38479-dg7h2v"
    irb(main):003:0> tf = Tempfile::new('abc')
    => #<Tempfile:/tmp/abc20201120-38479-abc6ui>
    irb(main):004:0> tf.path
    => "/tmp/abc20201120-38479-abc6ui"
    irb(main):006:0> tf = Tempfile::new(['abc','txt'])
    => #<Tempfile:/tmp/abc20201120-38479-2prba5txt>
    irb(main):007:0> tf.path
    => "/tmp/abc20201120-38479-2prba5txt"
    irb(main):008:0> tf = Tempfile::new(['abc','.txt'])
    => #<Tempfile:/tmp/abc20201120-38479-n6a5j6.txt>
    irb(main):009:0>

    irb(main):020:0> tf = Tempfile.new ['','.jpg']
    => #<Tempfile:/tmp/20201120-38479-qkitzp.jpg>

五、程式釋出

  • config/enviroment/production.rb, 靜態檔案服務開啟
    config.public_file_server.enabled = true