f.collection_select的名字

时间:2016-03-26 09:17:57

标签: ruby-on-rails select collections

我希望有人可以帮助我。 我有一个名为" location_location_relationships"的表。如果您创建一个新条目,您可以选择两个位置(前任和后继),这两个位置来自" locaions"表。此外,您可以选择一个旅游。这些来自桌子"旅游"。位置选择正在进行中。但是这些旅行会带来一些问题。首先,我在模型中没有任何东西可以工作,但选择的不是名字,而是" location_location_relationships"表。但我想要那里的名字。所以我完成了"旅行"的模型。和" location_location_relationships"。在选择中,现在有游览的名称,但如果我按下按钮"创建"我得到错误按摩: 游览(#96769428)预期,得到字符串(#1848192)

它显示了location_location_relationship_controller.rb中的错误:   ' def create

name

有谁知道我做错了什么?

非常感谢您的帮助!

这是我的location_location_relationships_form.html.erb,其中collection_select位于:

@location_location_relationship = LocationLocationRelationship.new(location_location_relationship_params)'

我的location_location_relationships模型:

<%= form_for(@location_location_relationship) do |f| %>
<% if @location_location_relationship.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@location_location_relationship.errors.count, "error") %> prohibited this location_location_relationship from being saved:</h2>

  <ul>
  <% @location_location_relationship.errors.full_messages.each do |message| %>
    <li><%= message %></li>
  <% end %>
  </ul>
</div>
<% end %>

<div class="field">
  <%= f.label 'Von Ort' %><br />
  <%= f.collection_select :predecessor_id, Location.all, :id, :name %>
</div>

<div class="field">
  <%= f.label 'Zu Ort' %><br />
  <%= f.collection_select :successor_id, Location.all, :id, :name %>
</div>
<div class="field">
<%= f.label 'Tour' %><br>
<%= f.collection_select :tour, Tour.all, :id, :name_of_tour, prompt: true %>
</div>
<div class="field">
<%= f.label 'Distanz' %><br>
<%= f.text_field :distance, type: "number", min:0 %>
</div>

<div class="actions">
<%= f.submit %>
</div>
<% end %>

我的旅游模特:

class LocationLocationRelationship < ActiveRecord::Base
belongs_to :successor, class_name: "Location"
belongs_to :predecessor, class_name: "Location"
validates :successor_id, presence: true
validates :predecessor_id, presence: true

belongs_to :tour

validates :tour, presence: true
validates :distance, :numericality => true
#  validates :sequence, :numericality => {:only_integer => true}
#  validates :binary_variable, :numericality => {:only_integer => true}
end

location_location_relationships控制器:

class Tour < ActiveRecord::Base
#validates :tour, presence: true
has_many :location_location_relationships
def name_of_tour
    name
end
end

1 个答案:

答案 0 :(得分:0)

  游览(#96769428)预期,得到字符串(#1848192)

您应该将tour更改为tour_id

<div class="field">
<%= f.label 'Tour' %><br>
<%= f.collection_select :tour_id, Tour.all, :id, :name_of_tour, prompt: true %>
</div>

还有location_location_relationship_params

def location_location_relationship_params
  params.require(:location_location_relationship).permit(:predecessor_id, :successor_id, :tour_id, :distance, :sequence, :binary_variable)
end