我一直按照Brandon Tilley 关于创建私人消息系统的说明进行操作,并希望修改传递私人消息收件人的方式(从复选框到文本框)。
在我看来,我有这个:
<%= f.label :to %><br />
<%= f.text_field :to, placeholder: "Recip...(separated by commas)" %>
如何接受输入作为将整数数组传递给控制器的文本输入?
额外细节:
全视图:
<h1>New Conversation</h1>
<%= form_for(@conversation) do |f| %>
<div>
<%= f.label :to %><br />
<%= f.text_field :to, placeholder: "Recip...(separated by commas)" %>
</div>
<br />
<%= f.fields_for :conversation do |c| %>
<div>
<%= c.label :subject %>
<%= c.text_field :subject %>
</div>
<%= c.fields_for :messages do |m| %>
<div>
<%= m.label :body %><br />
<%= m.text_area :body %>
</div>
<% end %>
<% end %>
<%= f.submit %>
<% end %>
在控制器内我有这个:
def create
redirect_to users_path unless current_user
@conversation = UserConversation.new(params[:user_conversation])
@conversation.user = current_user
@conversation.conversation.messages.first.user = current_user
...
在模型中我有这个:
accepts_nested_attributes_for :conversation
delegate :subject, :to => :conversation
delegate :users, :to => :conversation
attr_accessor :to
*before_create :create_user_conversations*
private
def create_user_conversations
return if to.blank?
to.each do |recip|
UserConversation.create :user_id => recip, :conversation => conversation
end
end
end
编辑:新模型:
def to
to.map(&:user_id).join(", ") *stack too deep error*
end
def to=(user_ids)
@to = user_ids.split(",").map do |recip|
UserConversation.create :user_id => recip.strip, :conversation => conversation
end