+5

Một số thủ thuật nhỏ sử dụng console trong Rails 5

Xây dựng database mẫu

  • Ta tạo một model user :
  def change
    create_table :users do |t|
      t.string :name, null: false
      t.string :phone

      t.timestamps
    end
  end

In ra biểu đồ yml của đối tượng bằng phương thức y:

irb(main):001:0> user = User.first
  User Load (0.5ms)  SELECT  "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ?  [["LIMIT", 1]]
=> #<User id: 1, name: "Nguyen van an", phone: "098765432", created_at: "2017-09-20 14:57:25", updated_at: "2018-01-03 01:07:06">
irb(main):002:0> y user
--- !ruby/object:User
concise_attributes:
- !ruby/object:ActiveRecord::Attribute::FromDatabase
  name: id
  value_before_type_cast: 1
- !ruby/object:ActiveRecord::Attribute::FromDatabase
  name: name
  value_before_type_cast: Nguyen van an
- !ruby/object:ActiveRecord::Attribute::FromDatabase
  name: phone
  value_before_type_cast: '098765432'
- !ruby/object:ActiveRecord::Attribute::FromDatabase
  name: created_at
  value_before_type_cast: '2017-09-20 14:57:25.696837'
- !ruby/object:ActiveRecord::Attribute::FromDatabase
  name: updated_at
  value_before_type_cast: '2018-01-03 01:07:06.575995'
new_record: false
active_record_yaml_version: 2
=> nil

Hiển thị đối tượng với phương thức pp

irb(main):003:0> pp user
#<User:0x0055d1277b37b8
 id: 1,
 name: "Nguyen van an",
 phone: "098765432",
 created_at: Wed, 20 Sep 2017 14:57:25 UTC +00:00,
 updated_at: Wed, 03 Jan 2018 01:07:06 UTC +00:00>
=> #<User id: 1, name: "Nguyen van an", phone: "098765432", created_at: "2017-09-20 14:57:25", updated_at: "2018-01-03 01:07:06">

Bạn có thể truy cập trong console với biến app

  • Truy cập class
irb(main):004:0> app.class
=> ActionDispatch::Integration::Session

  • Gửi request tới trang chủ
irb(main):005:0> app.get '/'
Started GET "/" for 127.0.0.1 at 2018-01-03 08:21:19 +0700
   (0.4ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
Processing by Rails::WelcomeController#index as HTML
  Rendering /home/khacthe/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/templates/rails/welcome/index.html.erb
  Rendered /home/khacthe/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/templates/rails/welcome/index.html.erb (9.1ms)
Completed 200 OK in 89ms (Views: 46.1ms | ActiveRecord: 0.0ms)

=> 200

  • Check flash
irb(main):007:0> app.flash
=> #<ActionDispatch::Flash::FlashHash:0x0055d1269fc6d8 @discard=#<Set: {}>, @flashes={}, @now=nil>

  • Hiển thị cookie
irb(main):008:0> app.cookies
=> #<Rack::Test::CookieJar:0x0055d127a9a990 @default_host="www.example.com", @cookies=[]>
  • Kiểm tra phương thức có sẵn của đối tượng.
irb(main):001:0> app.methods
=> [:main_app, :_main_app, :_routes, :rails_info_properties_path, :rails_info_routes_path, :rails_info_path, :rails_mailers_path, :rails_info_properties_url, :rails_info_routes_url, :rails_info_url, :rails_mailers_url, :status, :path, :host, :host=, :accept, :request, :headers, :reset!, :response, :body, :cookies, :process, :remote_addr, :default_url_options, :default_url_options=, :status_message, :redirect?, :url_options, :controller, :default_url_options?, :host!, :request_count, :https?, :remote_addr=, :accept=, :https!, :request_count=, :url_for, :optimize_routes_generation?, :full_url_for, :route_for, :polymorphic_url, :polymorphic_path, :edit_polymorphic_url, :edit_polymorphic_path, :new_polymorphic_url, :new_polymorphic_path, :session, :flash, :assigns, :redirect_to_url, :fixture_file_upload, :delete, :post, :head, :get, :patch, :put, :follow_redirect!, :html_document, :method_missing, :assert_recognizes, :assert_generates, :assert_routing, :with_routing, :assert_response, :assert_redirected_to, :css_select,...]

  • Gạch dưới ( _ ) là biến toàn cục đặt biệt.
irb(main):002:0>  _.class
=> Array

irb(main):003:0> app._routes.methods
=> [:empty?, :inspect, :call, :prepend, :set, :formatter, :formatter=, :eager_load!, :append, :default_url_options, :default_url_options=, :routes, :default_scope, :default_scope=, :url_helpers, :clear!, :relative_url_root, :mounted_helpers, :env_key, :url_for, :define_mounted_helper, :finalize!, :disable_clear_and_finalize, :disable_clear_and_finalize=, :add_url_helper, :optimize_routes_generation?, :path_for, :named_routes, :router, :resources_path_names, :polymorphic_mappings, :named_routes=, :resources_path_names=, :api_only?, :request_class, :draw, :add_route, :add_polymorphic_mapping, :extra_keys, :generate_extras, :find_script_name, :find_relative_url_root, :recognize_path, :set=, :router=, :to_json, :`, :to_yaml, :to_yaml_properties, :presence, :blank?, :present?, :psych_to_yaml, :as_json, :acts_like?, :to_param, :to_query,...]
  • Reload lại khi có sự thay đổi code.
irb(main):004:0> reload!
Reloading...
=> true

All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí