1

我对 Rails 还是很陌生,所以我可能会在这里错过一些非常重要的东西,这很容易修复,或者可能是我还没有研究过的部分。

我正在创建常规形式的巡回演出。该表单有很多字段,但有问题的字段是我的 collection_select。我要做的是在城市页面中显示与该城市相关的旅游。

这是我保存游览后出现的错误。

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

模型/city.rb

class City < ActiveRecord::Base
    has_many :cities
end

模型/tour.rb

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

我对城市和旅游有单独的控制器。虽然我确实将 :city 添加到 params.require(:tour).permit(.

意见/旅游/_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 %>

该表格完美地显示了所有城市,但是用城市更新游览给了我出现的错误。

作为稍后的额外问题,然后我想显示与该城市相关的所有旅行。例如访问 city/1 将显示该城市的游览。

谢谢!

4

2 回答 2

0

你可以试试这个:

<%= f.select :tour, City.all.map{|c| [c.city, c.id] }, {include_blank: true} %>
于 2013-11-20T22:08:27.150 回答
0

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

:city 这里是一个字符串吗?此外,City has_many :cities 关系似乎很奇怪。

另外,如果我理解这里的意图(看起来像巡回演唱会?),那么使用连接表(:id,:user_id,:city_id)并建立has_many_through关系可能更适合多对多关系。

于 2013-11-21T04:54:02.267 回答