1

I'm still pretty new to Rails so I could be missing some pretty major here and that's easy to fix or maybe a piece that I just haven't studied yet.

I'm creating a tour in regular form. The form has many fields but the one in question and where I'm having issues is with my collection_select. What I'm trying to do is display the tours associated to that city within the cities pages.

Here's the error that I'm getting which appears after I save the tour.

City(#70179438153960) expected, got String(#70179401165880)

models/city.rb

class City < ActiveRecord::Base
    has_many :cities
end

models/tour.rb

class Tour < ActiveRecord::Base
  belongs_to :user
  belongs_to :city
end

I have separate controllers for cities and tours. Pretty basic scaffolding although I did add :city to the params.require(:tour).permit(.

views/tours/_form.html.erb

  <%= simple_form_for(@tour) do |f| %>

    <div class="inputs">
        <%= f.input :company, label: "Company", input_html: { class: 'form-control' } %>
...
       <%= collection_select :tour, :city, City.all, :id, :city, {:include_blank => true } %>
    </div>

...
  <% end %>

The form displays all the cities perfectly fine but updating the tour with a city gives me that error I showed up.

As an extra question for later, I'd then like to display all the tours associated to the city. For example visiting city/1 would shows the tours for that city.

Thanks!

Michael
  • 589
  • 2
  • 11
  • 26

2 Answers2

0

You can try this:

<%= f.select :tour, City.all.map{|c| [c.city, c.id] }, {include_blank: true} %>
Daniël Zwijnenburg
  • 2,550
  • 2
  • 21
  • 28
  • Hm I'm now getting undefined method `tour' for # – Michael Nov 20 '13 at 22:19
  • I'm guessing that :tour should be replaced with :city but that leads me back to the same issue as before with expected, got String. – Michael Nov 21 '13 at 02:53
  • If I change it to <%= collection_select :city, :city, City.all, :id, :city, {:include_blank => true } %>, it does not save which I'm guessing is a params issue but I can't figure it out. – Michael Nov 21 '13 at 03:09
  • You can fix the params issue when you use f.select :tour. What is it that you want to save? A string or an association? e.g. what does your model look like? – Daniël Zwijnenburg Nov 21 '13 at 08:05
  • Just wrote above in another comment on what I'm trying to do. I'm trying to associate a tour to a city so that later I can display all the tours within that city. Appreciate the help btw. – Michael Nov 21 '13 at 16:53
  • Can you post your schema? It would eliminate the need to guess datatypes. – BWStearns Nov 22 '13 at 14:41
0

<%= collection_select :tour, :city, City.all, :id, :city, {:include_blank => true } %>

Is :city here a string? Also the City has_many :cities relationship seems odd.

Also, if I am understanding the intent here (seems like concert tours?) then a many to many relationship might be more appropriate using a join table (:id, :user_id, :city_id) and making a has_many_through relationship.

BWStearns
  • 2,567
  • 2
  • 19
  • 33
  • Yes, :city is a string. Hm, I think what I meant to write there is has_many :tours. So a tour is associated to a city. Then the city will display all the tours for that city. – Michael Nov 21 '13 at 16:46