Forum: Ruby on Rails Select from Many to Many

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Buntin, Seth - KATE (Guest)
on 2006-03-02 22:49
(Received via mailing list)
Here is what I have:



add_core_content.rhtml:

<select id="resource_core_content_ids"
name="resource[core_content_ids][]" size="15" style="width: 200px;"
multiple="multiple">

<% @core_contents.each do |c| %>

    <option value="<%= c.id %>" <% if @resource.core_contents.include?
(c) %> selected="selected"<% end %>>

       <%= c.code %>

    </option>

<% end %>

</select>

<br/>



admin_controller.rb:

def add_core_content

    @core_contents = CoreContent.find_all_by_subject(@params["subject"],
:order => 'level ASC')

end



Everything works fine if I don't have:

<% if @resource.core_contents.include? (c) %> selected="selected"<% end
%>



But when that is included I get an error:



You have a nil object when you didn't expect it!
The error occured while evaluating nil.core_contents

Extracted source (around line #3):

1: <select id="resource_core_content_ids"
name="resource[core_content_ids][]" size="15" style="width: 200px;"
multiple="multiple">
2: <% @core_contents.each do |c| %>
3:     <option value="<%= c.id %>" <% if
@resource.core_contents.include? (c) %> selected="selected"<% end %>>
4:        <%= c.code %>
5:     </option>
6: <% end %>



My many-to-many is core_contents_resources any my models are set up
correctly (having the habtm in both models).  I know that I am getting
something with @resource.core_contents because I get information
returned (albeit unreadable,
#<CoreContent:0x3321598>#<CoreContent:0x3321550>#<CoreContent:0x3321508>
#<CoreContent:0x33214c0>)



Thanks,

Seth
This topic is locked and can not be replied to.