expected, got String HABTM

148 Views Asked by At

This form:

<%= form_tag({:controller => "smart_lists", :action => "create"}, class: 'form-inline', :method => "POST") do %>
<%= label_tag :name %>
<%= text_field_tag :name %><br><br>
  <% @people.each do |person| %>
    <%= check_box_tag 'people_id[]', id: person.id %><%= label_tag person.name %><br>
  <% end %>
<%= submit_tag "Create", class: 'btn' %>

Sends its check box list to a controller here:

def create
  @smart_list = SmartList.new(params[:smart_list])
  @smart_list.name = params[:name]
  @smart_list.people = params[:people_id]
 etc....

And I end up with this in my logs:

"name"=>"This is not working : (", "people_id"=>["{:id=>64}", "{:id=>8}", "{:id=>1}"]

And this in my view:

Person(#70133507313700) expected, got String(#70133469090180)

So, I guess my question is - Is there a way to break the that stuff out of those strings? Or can I send them through the form in a better way? Or catch them a better way in the controller?

Thanks for your help - Joey

1

There are 1 best solutions below

0
On

I ended up changing the check_box_tag in the form to:

<td><%= check_box_tag 'people[]', person.id %></td>

Which gave me something like this in my params:

"people"=>["67", "11", "10", "23", "3", "1"],    

And then in the controller I looped through the :people params and took out the ids:

if params[:people]
  params[:people].each do |p|
    @smart_list.people << Person.where(id: p)
  end
end 

I feel like this is pretty ugly - but it works. If anyone has a better way to form the view or controller I would still like to know a better way to do this. - Thanks