2016-03-26 32 views
0

我希望有人能幫助我。 我有一個名爲「location_location_relationships」的表。如果您創建一個新條目,則可以選擇來自「locaions」表的兩個位置(前導和後繼)。另外,您可以選擇一個旅程。這些來自桌子「旅遊」。地點選擇正在工作。但巡演會產生一些問題。首先,我沒有在模型中一切正常,但所選內容中沒有寫入名稱,而是寫入了「location_location_relationships」表中的ID。但我想要那裏的名字。所以我完成了「旅遊」和「location_location_relationships」的模型。在選擇有現在的旅遊名稱,但如果我按下按鈕「創建」,我得到一個錯誤按摩: 預期,得到的字符串(#1848192)f.collection_select姓名

遊覽(#96769428)它顯示我的錯誤在location_location_relationship_controller.rb: 「DEF創建

@location_location_relationship = LocationLocationRelationship.new(location_location_relationship_params)' 

有沒有人一個想法,我做錯了什麼?

非常感謝您的幫助提前!

這是我location_location_relationships_form.html.erb其中collection_select是:

<%= 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 %> 

我的location_location_relationships的模型:

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 

我的旅遊模式:

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

location_location_relationships控制器:

class LocationLocationRelationshipsController < ApplicationController 
before_action :set_location_location_relationship, only: [:show, :edit, :update, :destroy] 


# GET /location_location_relationships 
# GET /location_location_relationships.json 
def index 
@location_location_relationships = LocationLocationRelationship.all 
end 

# GET /location_location_relationships/1 
# GET /location_location_relationships/1.json 
def show 
end 

# GET /location_location_relationships/new 
def new 
@location_location_relationship = LocationLocationRelationship.new 
end 

# GET /location_location_relationships/1/edit 
def edit 
end 

# POST /location_location_relationships 
# POST /location_location_relationships.json 
def create 
@location_location_relationship = LocationLocationRelationship.new(location_location_relationship_params) 

respond_to do |format| 
    if @location_location_relationship.save 
    format.html { redirect_to @location_location_relationship, notice: 'Die Distanz wurde angelegt.' } 
    format.json { render :show, status: :created, location: @location_location_relationship } 
    else 
    format.html { render :new } 
    format.json { render json: @location_location_relationship.errors, status: :unprocessable_entity } 
    end 
end 
end 

# PATCH/PUT /location_location_relationships/1 
# PATCH/PUT /location_location_relationships/1.json 
def update 
respond_to do |format| 
    if @location_location_relationship.update(location_location_relationship_params) 
    format.html { redirect_to @location_location_relationship, notice: 'Die Distanz wurde aktualisiert.' } 
    format.json { render :show, status: :ok, location: @location_location_relationship } 
    else 
    format.html { render :edit } 
    format.json { render json: @location_location_relationship.errors, status: :unprocessable_entity } 
    end 
end 
end 

# DELETE /location_location_relationships/1 
# DELETE /location_location_relationships/1.json 
def destroy 
@location_location_relationship.destroy 
respond_to do |format| 
    format.html { redirect_to location_location_relationships_url, notice: 'Die Distanz wurde gelöscht.' } 
    format.json { head :no_content } 
end 
end 
private 
# Use callbacks to share common setup or constraints between actions. 
def set_location_location_relationship 
    @location_location_relationship = LocationLocationRelationship.find(params[:id]) 
end 

# Never trust parameters from the scary internet, only allow the white list through. 
def location_location_relationship_params 
    params.require(:location_location_relationship).permit(:predecessor_id, :successor_id, :tour, :distance, :sequence, :binary_variable) 
end 
end 

回答

0

之旅(#96769428)預期,得到了字符串(#1848192)

你應該改變tourtour_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