,我發現了以下錯誤: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生成控制器動作
任何解釋?
你能發佈控制器代碼嗎? – andrewpthorp
將它貼在帖子頂部附近。 – Kezzer