2012-05-08 8 views
2

我有一個對象叫做Job,它屬於另一個對象 ,這個對象被稱爲client,有許多關係。Rails 3.2 - 嘗試編輯新的數據庫條目時收到一個奇怪的錯誤

這是我的工作模式

class Job < ActiveRecord::Base 
    belongs_to :client 
end 

這裏是我的客戶模型

class Client < ActiveRecord::Base 
    has_many :jobs 
end 

一份新的工作,我只是想創建一個客戶端過程中分配它。

但是,當我嘗試創建新工作時。在我看來,所有即時消息都是作業的ID而不是名稱,創建的模型的內部也是空的。

當我嘗試編輯作業並再次保存時,我收到以下錯誤。

Client(#2157214400) expected, got String(#2151988620) 

Application Trace | Framework Trace | Full Trace 
app/controllers/jobs_controller.rb:61:in `block in update' 
app/controllers/jobs_controller.rb:60:in `update' 

我想這可能是因爲我的控制器是錯誤的在某些方面,但是這是我的第一個應用程序,所以我不是很確定在哪裏看。

這是我的控制器。

類JobsController < ApplicationController中

def index 
     @job = Job.all 

     respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @job } 
     end 
    end 

    def show 
     @job = Job.find(params[:id]) 

     respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @job } 
     end 
    end 

    def new 
     @job = Job.new(params[:id]) 

     respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @job } 
     end 
    end 

    def edit 
     @job = Job.find(params[:id]) 
    end 

    def create 
     @job = Job.new(params[:jobs]) 

     respond_to do |format| 
     if @job.save 
      format.html { redirect_to @job, notice: 'job was successfully created.' } 
      format.json { render json: @job, status: :created, location: @job } 
     else 
      format.html { render action: "new" } 
      format.json { render json: @job.errors, status: :unprocessable_entity } 
     end 
     end 
    end 

    def update 
     @job = Job.find(params[:id]) 

     respond_to do |format| 
     if @job.update_attributes(params[:job]) 
      format.html { redirect_to @job, notice: 'job was successfully updated.' } 
      format.json { head :no_content } 
     else 
      format.html { render action: "edit" } 
      format.json { render json: @job.errors, status: :unprocessable_entity } 
     end 
     end 
    end 

    def destroy 
     @job = Job.find(params[:id]) 
     @job.destroy 

     respond_to do |format| 
     format.html { redirect_to :jobs } 
     format.json { head :no_content } 
     end 
    end 
    end 

任何指針或在正確的方向上的點頭,將不勝感激。

+0

顯示目前美國對編輯工作的形式。 – jdoe

+0

窗體很長,但在這裏發佈.. https://gist.github.com/2638538 – Keva161

回答

0

的問題是因爲ActiveRecord的預期客戶對象的一個​​實例,它有方法的客戶端=(因爲belogs_to協會) 當從請求綁定AR對象應該使用的,而不是客戶端的client_id參數

可以讀約http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-belongs_to

如果客戶端數量是短,你可以使用select與CLIENT_ID姓名 例http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html

所以你可以做這樣的事情

select("job", "client_id", Client.all.collect {|c| [ c.name, c.id ] }, {:include_blank => 'None'}) 

代替

f.text_field :client, :class => 'text_field' 
+0

謝謝..我需要在哪裏指定? – Keva161

+0

你應該改變表單模板,更新回答 – Fivell

+0

我按照你在我的觀點中所說的那樣輸入了那個文本,但所有即時消息都是那個文本,現在是一個輸入框。 – Keva161

相關問題