0

I have been following Brandon Tilley's instructions on creating a private message system and want to modify the way the recipient of a private message is passed (from check-box to text-box).

In the view I have this:

<%= f.label :to %><br />
<%= f.text_field :to, placeholder: "Recip...(separated by commas)" %>

How can I accept input as a text input that passes an array of integers to the controller?

Extra Details:

Full View:

<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 %>

Within the controller I have this:

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
...

Within the model I have this:

  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

Edit: New Model:

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

2 Answers 2

1

rails helpers are not set up to handle arbitrary array input auto-magically.

you can use multiple checkboxes or parse your text input which is a commaa separated list of user names. In your model

def to=(string)
  @to = User.where(:name => string.split(',')).select(:id).map(&:id)
end

for a nicer user experience you can use tagsinput jquery plugin and/or autocomplete.

also note that if you use a form to modify this same object, you need to regenerate the comma separated string as :value option for the text_field input to correctly prepopulate your edit form.

<%= text_field_tag "user_conversation[to]", (@conversation.to || []).join(',') %>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply. I tried your code with no luck so took the same idea with another method as noted in the edited question. No errors occurred but the user_ids are blank. Can you explain your code?
your solution is the wrong combo. you either use text_field with comma separated values and use conversion in the to accessors or if you use checkbox on "user_conversation[to][]", then you need no conversion for to. In both cases though, make sure you allow mass assignment for the to param.
0

In the view:

<%= f.label :to %><br />
<%= f.text_field :to, placeholder: "Recip...(separated by commas)" %>

In the model:

attr_accessible :to
attr_accessor :to

before_create :create_user_conversations

private

to.split(",").each do |recip|
  UserConversation.create :user_id => recip, :conversation => conversation
end

Comments

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.