> For the complete documentation index, see [llms.txt](https://ploegert.gitbook.io/til/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ploegert.gitbook.io/til/programmy/rails/attach-a-file-with-capybara.md).

# 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
<label for='data-file'>Data File</label>
<input type='file' name='data-file' />
```

```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')
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ploegert.gitbook.io/til/programmy/rails/attach-a-file-with-capybara.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
