I’m getting the following error:
undefined method `id' for #<Array:0x00000101ff0b70>
On this line:
<%= collection_select(:staff, :id, @staff, :id, :name, options ={:prompt => "-Select a staff member"}) %>
Here’s the action method:
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
Now, I know what you’re thinking, have you got a has_many through relationship etc., but actually the set up at the moment (because it’s in its first iterations) is very simple, and in fact mimics that of other types on the same page which work just fine. On the same page I have this:
<%= collection_select(:client, :id, @clients, :id, :name, options ={:prompt => "-Select a client"}) %>
And in fact, the model is precisely the same except with a different name. Both only have an ID and a name field. In fact, even the relationship in the model is the precisely the same. There’s a booking, a client, and a staff member. A client has_many :bookings, and a “staff” has_many :bookings. The only different I can really see is that I’m using inflections.rb to do the following
ActiveSupport::Inflector.inflections do |inflect|
inflect.uncountable "staff"
end
Any ideas why I can’t get this to work?
EDIT
Honestly, I think it has to do with belongs_to :staff in booking.rb model. This is something to do with pluralisation IMO.
EDIT EDIT
To be more specific, the following form works fine:
<%= 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 %>
But the following form does not:
<%= 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 %>
The @staff variable is simply generated in the controller action by doing @staff = Staff.all
Any explanation?
This was a strange issue, something I should’ve spotted earlier.
The
collection_selecthelpers needed to be specified for the booking. For some reason I’d just got muddled up as I’ve done this many-a-time before. Hey presto, it now works. Why the error didn’t complain about the secondcollection_selectI don’t know.If you do encounter the error I specified, it’s probably because you’ve got multiple helpers trying to resolve the same thing. I’ve not seen this posted anywhere else on the interwebs, but it’s fixed.