LightBox
LightBox

Reputation: 3425

How to Pass an array from form to controller?

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

Upvotes: 0

Views: 468

Answers (2)

LightBox
LightBox

Reputation: 3425

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

Upvotes: 0

Viktor Tr&#243;n
Viktor Tr&#243;n

Reputation: 8894

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(',') %>

Upvotes: 1

Related Questions