rails 小代码合集 view controller model

阅读更多
Rails Create an image with link using the image helper
  <%= link_to image_tag("rails.png", alt: "Rails"), 'http://rubyonrails.org' %>01.gemfile


#long_block_rhs.rb
	def self.logger
		@logger ||= begin
  		(defined?(Rails) && Rails.logger) ||
  		(defined?(RAILS_DEFAULT_LOGGER) && RAILS_DEFAULT_LOGGER) ||
  		default_logger
		end
	end
  
#simpler_rhs.rb
  def self.logger
    @logger ||= (rails_logger || default_logger)
  end
 
  def self.rails_logger
    (defined?(Rails) && Rails.logger) || (defined?(RAILS_DEFAULT_LOGGER) && RAILS_DEFAULT_LOGGER)
  end


Adding additional folders to autoload in your Rails app (Rails 4) 
development.rb
config.autoload_paths += [
"#{Rails.root}/app/contexts",
"#{Rails.root}/app/observers",
"#{Rails.root}/app/application",
"#{Rails.root}/app/workers",
]


<%= debug(session) if Rails.env.development? %>
<%= debug(params) if Rails.env.development? %>
Rails.application.config.paths["log"].first
Rails.env.staging?
config = YAML.load(File.read(Rails.root.join("config","config.yml")))[Rails.env].symbolize_keys
Rails.env => "development"

Rails.cache.write('test-counter', 1)
Rails.cache.read('test-counter')

	Rails.application.config.database_configuration[Rails.env]["database"]#	Rails: current database name
	
	Rails.logger.info "Hello, world!" #initializer.rb#可以借此观察 加载顺序	
  
	<%= Rails::VERSION::STRING %> #Rails: Rails version



initializer.rb
ActiveSupport::Notifications.subscribe "sql.active_record" do |name, start, finish, id, payload|
Rails.logger.debug "=============================================="
Rails.logger.debug "SQL: #{payload[:sql]}"
Rails.logger.debug "=============================================="
Rails.logger.debug caller.join("\n")
Rails.logger.debug "=============================================="
end

Clear Rails cache store and do it fast (without loading the whole Rails environment)

  cache.rake
  # bundle exec rake cache:clear
  namespace :cache do
  desc "Clear Rails.cache"
  task :clear do
  Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store(Rails.configuration.cache_store)
  Rails.cache.clear
  puts "Successfully cleared Rails.cache!"
  end
  end


Use Guard to restart Rails development server when important things change
guard-rails.rb
def rails
  system %{sh -c '[[ -f tmp/pids/development.pid ]] && kill $(cat tmp/pids/development.pid)'}
  system %{rails s -d}
end
 
guard 'shell' do
  watch(%r{config/(.*)}) { rails }
  watch(%r{lib/(.*)}) { rails }
end
rails


devise
https://gist.github.com/oma/1698995
_admin_menu.html.erb

你可能感兴趣的:(rails)