Skip to content

Conversation

juanmanuelramallo
Copy link
Contributor

Summary

Steps to reproduce

  1. Install ActionMailbox in your app
  2. Visit /rails/conductor/action_mailbox/inbound_emails as specified in here https://guides.rubyonrails.org/action_mailbox_basics.html#working-with-action-mailbox-in-development
  3. Click on "New inbound email by form"
  4. Fill the form and submit

Expected

The inbound email is created successfully

Actual

A NoMethodError is raised:

NoMethodError in Rails::Conductor::ActionMailbox::InboundEmailsController#create
undefined method `original_filename' for "":String

Other Information

>> mail_params[:attachments]
=> [""]

This happens because of the hidden field tag the multiple select field uses under the hood. An easy fix for this problem is to exclude the empty string from the collection.

@ghiculescu
Copy link
Member

ghiculescu commented Dec 27, 2021

Thanks for this. Could you please add a test to prevent regressions? It would go here https://github.com/rails/rails/blob/main/actionmailbox/test/controllers/rails/action_mailbox/inbound_emails_controller_test.rb

@juanmanuelramallo
Copy link
Contributor Author

@ghiculescu I added an empty string as part of the attachments collection in the test that exercises attachments, aiming to copy the behavior we see when submitting the form via a browser.

Let me know whether you'd rather have a new test case for this or just updating an existing test case is fine.

Thank you!

@ghiculescu ghiculescu added the ready PRs ready to merge label Dec 27, 2021
@ghiculescu
Copy link
Member

Looks good to me, as long as the test would fail without the fix.

@juanmanuelramallo
Copy link
Contributor Author

Test failed, I was unable to run the test locally (m1 hell) couldn't install the mysql2 gem. I'll try setting up a virtual host and check again.

@ghiculescu ghiculescu removed the ready PRs ready to merge label Dec 27, 2021
@juanmanuelramallo
Copy link
Contributor Author

Minor update:

All attachments are casted to strings whenever I add an empty string to the attachments array.

This is not the case via the browser, when I upload an attachment I can see that the elements inside the attachments array are not casted to strings (this is from the web console in the error page)

>> mail_params[:attachments]
=> ["", #<ActionDispatch::Http::UploadedFile:0x00000001561f1948 @tempfile=#<Tempfile:/var/folders/23/kj43cdvn7t97fjlrd33tfbg80000gn/T/RackMultipart20211227-27798-di69ea.jpeg>, @original_filename="friendlyreminder.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"mail[attachments][]\"; filename=\"friendlyreminder.jpeg\"\r\nContent-Type: image/jpeg\r\n">]

However, when using binding.irb in the test code we can see that all elements are strings:

irb(#<Rails::Conductor::ActionMailbox::InboundEmailsController:0x0000562403e394a0>):001:0> mail_params[:attachments]
=> ["", "#<Rack::Test::UploadedFile:0x0000562403e7f6a8>", "#<Rack::Test::UploadedFile:0x0000562403e7e690>"]

@juanmanuelramallo
Copy link
Contributor Author

@ghiculescu Here's why #44008 (comment) I couldn't make the test work for that particular scenario.

So, for simplicity I ended up moving this test to another scenario that wasn't using attachments at all. Test fails without the change and passes with it.

Let me know what you think, happy to help.

@ghiculescu
Copy link
Member

@juanmanuelramallo could you squash your commits?

@andrew2005
Copy link

Fixes #43928.

1. Install ActionMailbox in your app
2. Visit `/rails/conductor/action_mailbox/inbound_emails` as specified in here https://guides.rubyonrails.org/action_mailbox_basics.html#working-with-action-mailbox-in-development
3. Click on "New inbound email by form"
4. Fill the form and submit

The inbound email is created successfully

A `NoMethodError` is raised:

```
NoMethodError in Rails::Conductor::ActionMailbox::InboundEmailsController#create
undefined method `original_filename' for "":String
```

```
>> mail_params[:attachments]
=> [""]
```

This happens because of the hidden field tag the multiple select field uses under the hood. An easy fix for this problem is to exclude the empty string from the collection.

Add empty string in attachments params in test file

Updated an existing test case to include an empty string in the
attachments collection, mimicking the behavior of the form submission.

Update inbound emails controller test

Moved the empty string for attachments to a different test case where
attachments are not actually sent, just so that we can exercise that
this test fails without this change and passes with it.
@juanmanuelramallo
Copy link
Contributor Author

@ghiculescu rebased with latest main and squashed all commits

@ghiculescu ghiculescu added the ready PRs ready to merge label Jan 14, 2022
@drnic
Copy link
Contributor

drnic commented Mar 13, 2022

Is this PR something that will go into a Rails 7.0.X patch or held off for Rails 7.1 later this year/next year? Sorry to ask rails release question in the ticket.

@juanmanuelramallo
Copy link
Contributor Author

Closing this as it is already solved via #45103

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
actionmailbox ready PRs ready to merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants