如何以Rails形式提交字符串数组 4.2
How to submit a string array in a form Rails 4.2
我有一个带有字符串数组属性 recipients
的模型 message
。这是我的 messages_controller.rb
的样子:
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
_fields.html.erb 看起来像这样:
<%= 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" %>
问题是它像这样保存 recipients
数组:
["recipient_1, recipient_2"]
而不是
["recipient_1", "recipient_2"]
我希望能够检索收件人[0] = recipient_1 而不是收件人[0] = 整个数组。
这可能很简单,但我找不到解决方法。
所有投入表示赞赏。
谢谢
通常对于 permit
你传递一个 :symbols 的集合。这些键必须仅表示标量值。但是数组由空数组表示不同。
试试这个:
def create_params
params.require(:message).permit(:object,
:content,
:attachments,
recipients: [])
end
以及您表单中的字段:
<%= f.text_field_tag "recipients[]", :recipients, multiple: true, class: "form-control" %>
它返回一个字符串,因为输入是一个文本字段。如果你想为此使用文本字段,那么只需期望输入将以逗号分隔并拆分输入:
@message.recipients = params[:message][:recipients].split(',')
# or, if it's wrapped in an array
# @message.recipients = params[:message][:recipients][0].split(',')
我有一个带有字符串数组属性 recipients
的模型 message
。这是我的 messages_controller.rb
的样子:
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
_fields.html.erb 看起来像这样:
<%= 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" %>
问题是它像这样保存 recipients
数组:
["recipient_1, recipient_2"]
而不是
["recipient_1", "recipient_2"]
我希望能够检索收件人[0] = recipient_1 而不是收件人[0] = 整个数组。
这可能很简单,但我找不到解决方法。 所有投入表示赞赏。 谢谢
通常对于 permit
你传递一个 :symbols 的集合。这些键必须仅表示标量值。但是数组由空数组表示不同。
试试这个:
def create_params
params.require(:message).permit(:object,
:content,
:attachments,
recipients: [])
end
以及您表单中的字段:
<%= f.text_field_tag "recipients[]", :recipients, multiple: true, class: "form-control" %>
它返回一个字符串,因为输入是一个文本字段。如果你想为此使用文本字段,那么只需期望输入将以逗号分隔并拆分输入:
@message.recipients = params[:message][:recipients].split(',')
# or, if it's wrapped in an array
# @message.recipients = params[:message][:recipients][0].split(',')