This is what worked for me in a Rails 4:
# JSON data as first argument, then parameters
patch :update, { some: 'data' }.to_json, id: id, format: :json
Modern browsers natively suppport file pickers that allow the user to choose multiple files at once. To activate this feature, set the multiple
attribute:
<input type="file" name="images[]" multiple />
Or in a Rails view:
<%= file_field_tag "images[]", multiple: true %>
This works in IE10+.
Make sure that the field name ends in []
so your server-side code will parse the incoming files into an array. Obviously this naming convention is not compatible with default Rails nested attribute setters, so you'll need to write a form ...
A collection of useful filters for AngularJS, e.g. for fuzzy string searching, displaying numbers as percentages an more.
Usage:
class Document < ActiveRecord::Base
scope :any_tags, -> (tags){ where('tags && ARRAY[?]', tags) }
scope :all_tags, -> (tags){ where('tags @> ARRAY[?]', tags) }
end
Document.create(title: "PostgreSQL", tags: ["pg","rails"])
Document.any_tags('pg')
Document.all_tags(['pg', 'rails'])
Migration:
class CreateDocuments < ActiveRecord::Migration
def change
create_table :documents do |t|
t.string :title
t.string :tags, array: true, default: []
t.timestamps
end
add_index :documents, :ta...
tl;dr: Use with_index
find_each
with indexIf you do not provide a block to find_each, it will return an Enumerator for chaining with other methods:
Person.find_each.with_index do |person, index|
person.award_trophy(index + 1)
end
map
with indexSimilarly, you may need an index when using other methods, like map
, flat_map
, detect
(when you need the index for detection), or similar. Here is an example for map
:
people...
A different take on what we're doing with ActiveType. Since it lives under the rails
organization it might be part of Rails 5?
Nice tutorial about packaging Ruby bindings to your API in a Ruby gem, with tests using VCR casettes.
In Thunderbird, you can set custom font faces and sizes for reading plain-text e-mails. However, Thunderbird sometimes "randomly" does not respect your choices.
This is actually not a bug, but a rather weird feature: Fonts are defined per encoding of e-mails.
Some e-mails will be considered Unicode, some Western (ISO 8859-1), and some maybe yet another encoding.
The advanced font settings dialog by default just opens on "Western". Choose a different encoding from the "Fonts for" dropdown menu and you'll see that your custom font sett...
You need to copy an entire website? Use wget
like this:
wget -kr http://host.tld/
This will fetch all content (-r
for recursive) and rewrite links inside the documents to make them suitable for local viewing (-k
).
If you're using the :header_html
option in PDFKit (or the corresponding --header-html
option in wkhtmltopdf
), and the header remains invisible, you need to add this to your header HTML:
<!doctype html>
The same applies to footers via footer_html
I'm sorry.
To avoid n+1 queries, you want to eager-load associated records if you know you need to access them later on.
The Rails docs say:
Eager loading is supported with polymorphic associations.
This is true, but has some caveats.
Consider the following models:
class Image < ActiveRecord::Base
end
class Video < ActiveRecord::Base
end
class PageVersion < ActiveRecord::Base
belongs_to :primary_medium, polymorphic: true # may be Image or Video
end
class Page < ActiveReco...
So you want to organize your I18n using multiple .yml
files but your Rails 4.1 application simply won't use any extra files in development? Spring is to blame.
Halt spring by running:
spring stop
The next time you spawn a bin/rails console
or similar, your new translations will work.
You will need to repeat the above every time you create a new .yml
file or rename existing ones.
:(
Great introduction to React.js and the ideas behind it.
There are different ways to run rake:
bin/rake
to use them.Here is a solution that gives you a plain rake
command which uses a binstubbed bin/rake
if available and falls back to bundle exec rake
if necessar...
When your system is not running on English, you may sometimes want to run some applications and not use your system locale.
Use cases are scripts that parse output, or just using the (possibly) more common English labels or error messages. Here is how to do that.
I will use the date
command and print the current weekday, just for the sake of an example.
Most often, setting LC_ALL
for your command should be enough. The following was run on a system using a German locale.
$ date +%...
In Rails, you can very easily send emails with HTML and plaintext bodies.
However, if you're trying to debug those using your normal email account, you might be out of luck: For some reason, Exchange servers will simply throw away the plaintext part of your mail, and just save the html part.
Use reorder
to replace an existing order clause with a new expression.
The flip-flop operator is a hotly contested feature of Ruby. It's still struggling to find an idiomatic use case, except for a few very rarely needed things. It's not something you'll likely reach for on a daily, weekly or even monthly basis. The only thing you really need to know about it is what it does, and that's only in case you encounter it in someone else's code. Many even go as far to say not to use the flip-flop operator, that it only adds confusion.
My brain just melted.
We will be installing rbenv and ruby-build from our own fork, not from the Ubuntu sources.
Install rbenv:
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
For Bash:
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
For ZSH:
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(rbenv init -)"' >> ~/.zshrc
Now reinitialize ...
# config/initializers/sidekiq.rb
# Perform Sidekiq jobs immediately in development,
# so you don't have to run a separate process.
# You'll also benefit from code reloading.
if Rails.env.development?
require 'sidekiq/testing'
Sidekiq::Testing.inline!
end
Though nowhere to be found in the official docs, this works just fine.
describe Facebook::Post do
it_behaves_like :time_series
end
shared_examples_for :time_series do
# shared example code
end
For server-to-server requests to the Facebook Graph API you can skip requesting an Oauth token, an instead use the combination of app_id|app_secret
as your access token. This token will never expire, and should suffice for retrieving basic information from the Graph API.
http://graph.facebook.com/endpoint?key=value&access_token=app_id|app_secret
Since you don't make requests for a certain user, the Graph API might respond with an error in case you're requesting a resource that requires authenticating as a human...
Remove your old LibreOffice:
sudo apt-get remove libreoffice*
You probably also want to get rid of the dead PPAs you might have installed for LibreOffice 4.x:
Installing a new LibreOffice
-------------...
By default git diff
highlights whole lines as changes.
To diff on a word-by-word basis you can say:
git diff --color-words
To diff on a character-by-character basis you can say:
git diff --color-words=.