2011-12-10 21 views
0

,我發現了以下錯誤:Underfined方法 「ID」 爲collection_select

undefined method `id' for #<Array:0x00000101ff0b70> 

在此行中:

<%= collection_select(:staff, :id, @staff, :id, :name, options ={:prompt => "-Select a staff member"}) %> 

下面是操作方法:

def new 
    @treatments = Treatment.all 
    @clients = Client.all 
    @staff = Staff.all 

    @booking = Booking.new 

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

現在,我知道你在想什麼,你是否通過關係等得到了一個關係,但實際上目前的設置(因爲它在第一個迭代)非常簡單,事實上它可以模仿在同一頁面上工作得很好的其他類型。在同一頁上我有這個:

<%= collection_select(:client, :id, @clients, :id, :name, options ={:prompt => "-Select a client"}) %> 

而事實上,除了不同的名稱模型是完全相同的。兩者都只有一個ID和一個名稱字段。事實上,即使模型中的關係也是完全一樣的。有預訂,客戶和工作人員。客戶has_many:預訂,「staff」has_many:預訂。唯一不同的我真的可以看到的是,我使用的是inflections.rb做以下

ActiveSupport::Inflector.inflections do |inflect| 
    inflect.uncountable "staff" 
end 

任何想法,爲什麼我不能得到這個工作?

編輯

老實說,我認爲它在booking.rb模型belongs_to :staff做。這與IMO的多元化有關。

編輯編輯

更具體地講,下面的表格正常工作:

<%= form_for(Booking.new) do |f| %> 
     <div class="field"> 
      <%= f.label :treatment %><br /> 
      <%= collection_select(:treatment, :id, Treatment.all, :id, :name, options ={:prompt => "-Select a treatment"}) %> 
      </div> 
      <div class="field"> 
      <%= f.label :client %><br /> 
      <%= collection_select(:client, :id, Client.all, :id, :name, options ={:prompt => "-Select a client"}) %> 
      </div> 
      <div class="field"> 
      <%= f.label :staff %><br /> 
      <%= collection_select(:staff, :id, Staff.all, :id, :name, options ={:prompt => "-Select a staff member"}) %> 
      </div> 
      <div class="actions"> 
      <%= f.submit %> 
      </div> 
<% end %> 

但以下形式並不:

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

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

    <div class="field"> 
    <%= f.label :treatment %><br /> 
    <%= collection_select(:treatment, :id, @treatments, :id, :name, options ={:prompt => "-Select a treatment"}) %> 
    </div> 
    <div class="field"> 
    <%= f.label :client %><br /> 
    <%= collection_select(:client, :id, @clients, :id, :name, options ={:prompt => "-Select a client"}) %> 
    </div> 
    <div class="field"> 
    <%= f.label :staff %><br /> 
    <%= collection_select(:staff, :id, @staff, :id, :name, options ={:prompt => "-Select a staff member"}) %> 
    </div> 
    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

的@staff變量只是通過執行@staff = Staff.all生成控制器動作

任何解釋?

+2

你能發佈控制器代碼嗎? – andrewpthorp

+0

將它貼在帖子頂部附近。 – Kezzer

回答

0

這是一個奇怪的問題,我應該早點發現。

<div class="field"> 
    <%= f.label :treatment %><br /> 
    <%= collection_select(:booking, :treatment_id, @treatments, :id, :name, options ={:prompt => "-Select a treatment-"}) %> 
    </div> 
    <div class="field"> 
    <%= f.label :client %><br /> 
    <%= collection_select(:booking, :client_id, @clients, :id, :name, options ={:prompt => "-Select a client-"}) %> 
    </div> 
    <div class="field"> 
    <%= f.label :staff %><br /> 
    <%= collection_select(:booking, :staff_id, @staff, :id, :name, options ={:prompt => "-Select a staff member-"}) %> 
    </div> 

collection_select幫助者需要指定預訂。出於某種原因,我只是因爲我以前做過很多次這樣的事情而糊塗起來。嘿presto,它現在可以工作。爲什麼錯誤沒有抱怨我不知道第二個collection_select

如果您確實遇到我指定的錯誤,可能是因爲您有多個幫助者試圖解決相同的問題。我沒有在互聯網上的其他任何地方看到過這個帖子,但它已修復。