Skip to content

Instantly share code, notes, and snippets.

@JamesYang76
Last active February 19, 2020 23:12
Show Gist options
  • Save JamesYang76/a54b69400ff33eaf97e1c747c831df3a to your computer and use it in GitHub Desktop.
Save JamesYang76/a54b69400ff33eaf97e1c747c831df3a to your computer and use it in GitHub Desktop.

change mode

$ rails s -e production
or 
$ RAILS_ENV=production rails s
or
$ export RAILS_ENV=production
$ printevn | grep RAILS_ENV
$ rails s

console reload

If you have already instantiated any objects, their attributes would not be updated - including newly introduced validations. However, if you create a new object, its attributes (and also validations) will reflect the reloaded code.

$ rails console
> reload!

create app

# rails intall
gem install rails -v 6.0.2
rails _6.0.2_ new appname

# without the default testing suite:
rails new appname -T

# skip turbolinks:
rails new appname --skip-turbolinks 

# database
rails new appname --database=postgresql

# help
rails new -h

credentials

config/credentials.yml.enc is an encrypted file which store the credentials. As this is a encrypted file, we can safely commit it to our version control systems.

config/master.key contains RAILS_MASTER_KEY which is used to decrypt the config/credentials.yml.enc. We should not commit this file to version control

$ EDITOR="atom --wait" rails credentials:edit

for multi environment credentials
$ EDITOR="atom --wait" rails credentials:edit --environment development

secret_key_base

The secret key base is required by Rails. If you want to generate a new secret key base run,

$ bin/rails secret

and add that to your credentials by running bin/rails credentials:edit.

Turbolinks

Turbolinks makes following links in your web application faster. Instead of letting the browser recompile the JavaScript and CSS between each page change, it keeps the current page instance alive and replaces only the body (or parts of) and the title in the head. Think CGI vs persistent process.\

document.addEventListener("turbolinks:load", function() {
  // ...
})

Reference

Routes

CRUD

  • Browsers natively only support HTTP GET and POST methods, we have to do something a bit tricky to use HTTP POST to fake PUT/PATCH/DELETE methods.
  • On the server side, Rails is able to examine the _method value in the params to restore the semantics of the HTTP request.
<form method="post" action="/posts/<%= post.id %>" style='display: inline'>
  <input name= "_method" type="hidden" value="delete">
  <input type="submit" value="Delete" />
</form>

resource vs resources

resource does not have an index method, as it is supposed to be only for a single resource

/workshop
resource: new, create, show, edit, update, destroy

/workshops/:id
resources: index, new, create, show, edit, update, destroy

error

moduleActionView 
 class Base
    cattr_accessor :field_error_proc
    @@field_error_proc = Proc.new{ |html_tag, instance|
      "<div class=\"field_with_errors\">#{html_tag}</div>".html_safe
    }
 end
 ...
 
# we can customize it 
ActionView::Base.field_error_proc = proc do |html_tag, instance|
  include FormHelper
  FormHelper.error_message(html_tag, instance)
end

module FormHelper
  include ActionView::Context
  include ActionView::Helpers::TextHelper
  include ActionView::Helpers::TagHelper

  def error_message(html_tag, instance)
    output = content_tag(:div, class: "form__field--error") do
      concat html_tag.html_safe # rubocop:disable Rails/OutputSafety
      unless html_tag.match?(/^<label/)
        concat content_tag(:label, instance.error_message.any? ? instance.error_message.first : "",
                           class: ["message", "form__error"], for: instance.send(:tag_id))
      end
    end
    return output if output.html_safe?
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment