表单: RailsNotes
用户: dreamable
创建日期: 2020-11-13 00:16:14 UTC
更新日期: 2020-11-13 01:00:13 UTC
引用:(Table ID 3, Record ID 5)

标题 :
Array & Hash
笔记 :
  • Hash with specified keys
    h.slice(:key1,:key2)
    h.slice(*[:key1,:key2])
  • Hash: convert key to symbols
    h.symbolize_keys
标签:
表单: RailsNotes
用户: dreamable
创建日期: 2020-11-12 08:51:41 UTC
更新日期: 2020-11-12 22:22:24 UTC
引用:(Table ID 3, Record ID 4)

标题 :
insert_all
笔记 :

note
Rails 6 adds insert_all, insert_all!, upsert_all for bulk insertion

  • insert_all(attributes, returning: nil, unique_by: nil)
    This method can be used to insert multiple records with a single SQL INSERT statement. The method simply skips ActiveRecord callbacks or validations. It accepts array of hashes, where every hash determines the attributes for a single record or row.

  • insert_all!(attributes, returning: nil)
    This method works same as that of insert_all but in addition to that it raises ActiveRecord::RecordNotUnique if any record violates a unique index on the table.

  • upsert_all(attributes, returning: nil, unique_by: nil)
    It updates the records if they exist or simply inserts them into database with a single SQL INSERT statement.

NOTE:

  • Can't use reference: i.e. use user_id instead of user
  • Must provide value for all fields, e.g. created_at, updated_at
标签:
表单: RailsNotes
用户: dreamable
创建日期: 2020-11-12 07:10:04 UTC
更新日期: 2020-11-12 23:20:33 UTC
引用:(Table ID 3, Record ID 3)

标题 :
destroy_all vs delete_all
笔记 :
  • https://stackoverflow.com/questions/6698207/delete-all-vs-destroy-all

    • destroy_all: call destroy method of all matched objects. i.e. one SQL for one object
    • delete_all: one SQL delete for all matched results.

      • NOTE:It seems to be update if use reference e.g.

           table.datetime_cells.delete_all 
            DatetimeCell Update All (0.3ms)  UPDATE "datetime_cells" SET "table_id" = $1 WHERE "datetime_cells"."table_id" = $2  [["table_id", nil], ["table_id", 7]]
           datetime_cells.where(table:talbe).delete_all # OK
        
标签:
表单: RailsNotes
用户: dreamable
创建日期: 2020-11-06 23:43:37 UTC
更新日期: 2020-11-07 00:39:36 UTC
引用:(Table ID 3, Record ID 2)

标题 :
Calendar
笔记 :

simple_calendar or DIY

  1. gem
    gem simple_calendar

  2. scss
    include the default stylesheet for the calendar in your app/assets/stylesheets/application.css file

    *= require simple_calendar
    

    highlight start-date in app/assets/stylesheets/simple-calender.scss

    .simple-calendar {
      .start-date {
          background-color: coral
      }
    }
    
  3. views

    <%= month_calendar start_date_param: :date  do |date| %>
      <%= date.day %>
    <% end %>
    
标签:
表单: RailsNotes
用户: dreamable
创建日期: 2020-11-02 23:53:43 UTC
更新日期: 2021-01-25 10:00:21 UTC
引用:(Table ID 3, Record ID 1)

标题 :
wicked_pdf
笔记 :

采用wicked_pdf产生PDF文档

  1. Gemfile

    gem 'wicked_pdf'
    gem 'wkhtmltopdf-binary'
    #gem 'wkhtmltopdf-binary-edge' # fork of wkhtmltopdf-binary,不需要
    
  2. install libxrender1

    • Error, error while loading shared libraries: libXrender.so.1
    • Solution: Need to install sudo apt-get install libxrender1
  3. in config/initializers/mime_types.rb

    Mime::Type.register "application/pdf", :pdf 
    
    • Optional? PDF may be builtin for new version.
  4. define template in app/views/layouts/pdf.html.erb

        <!doctype html>
        <html>
        <head>
            <meta charset='utf-8' />
            <%= javascript_include_tag "https://code.jquery.com/jquery-3.3.1.slim.min.js" %>
            <%= javascript_include_tag "http://code.jquery.com/ui/1.10.3/jquery-ui.min.js" %>
            <%= javascript_include_tag "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" %>
            <%= javascript_include_tag "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" %>
        </head>
        <body onload='number_pages'>
            <div id="header">
            </div>
            <div id="content">
            <%= yield %>
            </div>
        </body>
        </html>
    
    • NOTE: In the official guide, you can include local css/js, but you need to define it and pre-compile it for production. Otherwise, the missing assets could be OK in development but failed in production
        <%= wicked_pdf_stylesheet_link_tag "pdf" -%>
        <%= wicked_pdf_javascript_include_tag "number_pages" %>
    
    • although include jquery & bootstrap cdn in pdf.html.erb, but seems not work.
  5. specify template

    • define default template in config/initlializers/wicked_pdf.rb
       layout: 'pdf.html'
    
    • specify in page view when using it.
  6. Use it in controllers

    in  app/controller/records_controller.rb
      format.pdf do
        render pdf: "records",
        template: "records/index.html.erb"
    #        layout: "pdf.html"
      end
    
    • You could customized layout template here.
  7. If you use assets, pre-compile it for production. in config/initializers/assets.rb. e.g.

    Rails.application.config.assets.precompile += ['blueprint/screen.css', 'pdf.css', 'jquery.ui.datepicker.js', 'pdf.js']
    
    • Not used and tested yet.
  8. Chinese character problem.

    • set UTF8 in layout <meta charset='utf-8' />
    • install font: sudo apt-get install fonts-wqy-zenhei
标签: