4

I have a model message with a string array attribute recipients. Here's what my messages_controller.rb looks like:

def new
  @message = Message.new
  @message.attachments = params[:attachments]
end
def create
  @message = Message.new(create_params)
  @message.user_id = current_user.id
  @message.attachments = params[:message][:attachments]
  @message.recipients = params[:message][:recipients]
  save_message or render 'new'
end
private
  def create_params
    params.require(:message).permit(:object, :content,
                                  :recipients,
                                  :attachments)
  end

And the _fields.html.erb looks like this:

<%= render 'shared/error_messages', object: f.object %>
<%= f.label :object %>
<%= f.text_field :object, class: "form-control" %>
<%= f.label :recipients %>
<%= f.text_field :recipients, multiple: true, class: "form-control" %>
<%= f.label :attachments %><br>
<%= f.file_field :attachments, multiple: true, class: 'form-control' %><br />
<%= f.label :content %>
<%= f.text_area :content, class: "form-control" %>

The problem is that it saves the recipients array like this:

["recipient_1, recipient_2"]

instead of

["recipient_1", "recipient_2"]

I want to be able to retrieve recipient[0] = recipient_1 and not recipient[0] = whole array.

It's probably something simple, but I can't find the fix. All inputs appreciated. Thanks

2 Answers 2

3

It is returning a single string because the input is a text field. If you want to utilize a text field for this then just expect that the input will be comma-separated and split the input:

@message.recipients = params[:message][:recipients].split(',')

# or, if it's wrapped in an array
# @message.recipients = params[:message][:recipients][0].split(',')
Sign up to request clarification or add additional context in comments.

Comments

1

Usually for permit you pass it a collection of :symbols. These keys must represent scalar values only. But arrays are represented differently by an empty array.

Try this:

def create_params
    params.require(:message).permit(:object,
                                    :content,
                                    :attachments,
                                    recipients: [])
end

And the field in your form:

<%= f.text_field_tag "recipients[]", :recipients, multiple: true, class: "form-control" %>

3 Comments

thanks for the reply. I had to wrap it around a block {recipients: []} and it still gave me [["recipient_1, recipient_2"]]
This tricky, but you actually have to put array declarations at the end of your permit parameters ! So in the above piece of code the :attachments is messing things up, because it is after the recipients declaration. Or maybe they fixed that in newest Rails version ?
@CyrilDD Thanks for the catch, I didn't know that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.