From 384e75b4c29016db815f8cc273a97fb50a6dc493 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Mon, 21 Mar 2016 18:13:03 -0500 Subject: [PATCH] Add Attach A File With Capybara as a rails til --- README.md | 3 ++- rails/attach-a-file-with-capybara.md | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 rails/attach-a-file-with-capybara.md diff --git a/README.md b/README.md index 48f8c90..e449d19 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really warrant a full blog post. These are mostly things I learn by pairing with smart people at [Hashrocket](http://hashrocket.com/). -_370 TILs and counting..._ +_371 TILs and counting..._ --- @@ -223,6 +223,7 @@ _370 TILs and counting..._ ### Rails - [All or Nothing Database Transactions](rails/all-or-nothing-database-transactions.md) +- [Attach A File With Capybara](rails/attach-a-file-with-capybara.md) - [Attribute Getter without the Recursion](rails/attribute-getter-without-the-recursion.md) - [Attribute Was](rails/attribute-was.md) - [Autosave False On ActiveRecord Associations](rails/autosave-false-on-activerecord-associations.md) diff --git a/rails/attach-a-file-with-capybara.md b/rails/attach-a-file-with-capybara.md new file mode 100644 index 0000000..5d0cd8c --- /dev/null +++ b/rails/attach-a-file-with-capybara.md @@ -0,0 +1,28 @@ +# Attach A File With Capybara + +There are two ways to attach a file with +[Capybara](https://github.com/jnicklas/capybara). The more conventional way +is with the +[`attach_file`](http://www.rubydoc.info/github/jnicklas/capybara/Capybara%2FNode%2FActions%3Aattach_file) +method. + +Assuming there is a form with a file input similar to the following: + +```html + + +``` + +```ruby +attach_file('data-file', 'path/to/file.csv') +``` + +The first argument to `attach_file` is a locator which refers to the `name` +attribute on the `input` tag. + +If for some reason there is no `name` attribute on the `input` tag, the file +can be attached with a standard `find` and `set`. + +```ruby +find('form input[type="file"]').set('path/to/file.csv') +```