0

I have models Post and Image. Post has_many images

I have this field

<%= f.file_field :image, required: false %>

How to test adding image that is adding by cocoon gem methods with Capybara attach_file method?

attach_file( "file_field", "#{Rails.root}/spec/assets/example.jpg" )

What should I write instead of file_field? What is the name of this field should be? "image" or :image don`t work.

According to Capybara docs: "The file field can be found via its name, id or label text." But none of the above is working.

tetiross
  • 192
  • 3
  • 17
  • Use `save_and_open_page` to open the page in your browser and the use the web inspector to find the name, id or label. – max Feb 18 '16 at 15:19
  • could you add the html generated of your file field? – svelandiag Feb 18 '16 at 15:21
  • You provide very little info for us to guess what the id/name/label of the field could be. Can you show us a little more to get an impression how your entire form/view code looks? Which form-builder are you using? Does your file-field have (a correct) label? Or show us the generated html of the field. Also it is possible to use xpath expressions instead: http://stackoverflow.com/questions/15165455/how-do-i-attach-a-file-when-the-input-file-has-a-dynamic-id – nathanvda Feb 19 '16 at 09:49
  • Hi if my answer was useful, please consider select it as accepted answer, that's how the community works... – svelandiag Mar 02 '16 at 19:22

2 Answers2

0

Try this code as this link

it "can upload an image" do
  post :picture, avatar: fixture_file_upload('files/spongebob.png', 'image/png')
  response.should be_success
end

Read answers of this question, I think would be helpful.

Community
  • 1
  • 1
svelandiag
  • 4,231
  • 1
  • 36
  • 72
0

Here is decision that I found, and it works for me. You search for all input elements on the page and then just setting the value for each.

elements = page.all(:css, 'input[type="file"]') elements[0].set "#{Rails.root}/spec/assets/example.jpg" elements[1].set "#{Rails.root}/spec/assets/example_2.jpg"

tetiross
  • 192
  • 3
  • 17