Forum: Ruby on Rails collection_select problem

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.
0fdce938a28a604fd5613a68b0d78fcb?d=identicon&s=25 Jose vicente Ribera pellicer (joripel)
on 2008-11-26 18:04
Hi, first and foremost sorry if my english is not the best.

I want to choose between somo products in a view.

I have tickets and i wanna add tickets_products.

1)ruby script/generate scaffold ticket_product name:string
2) rake db: migrate
3) Models-> ticket belongs to ticketproducts and ticketproducts  has
many tickets.
4) I use Sqlite Manager (firefox pluggin) and I added two ticket
products with this soft in tickets_products table.

5)I wrote this in new.html.erb (ticket view):

<% form_for(@ticket) do |f| %>


  <table class="product" >
    <tr>
    <th><%= _('Product') %></th>
    <th><%= _('Breakdown') %></th>
    <th><%= _('State') %></th>
    </tr>
    <tr>
    <td><%= f.collection_select :ticket_product_id, @ticket_products,
:id, :name, :prompt => _("Select product") %></td>
    <td><%= f.collection_select :ticket_breakdown_id,
@ticket_breakdowns, :id, :title, :prompt => _("Select breakdown") %>
</td>
    <td><%= f.collection_select :ticket_state_id, @ticket_states, :id,
:title, :prompt => _("Select state") %></td>
    </tr>
  </table>


The error is:

 NoMethodError in Tickets#new

Showing tickets/new.html.erb where line #14 raised:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.map

Extracted source (around line #14):

11:     <th><%= 'Estado del producto' %></th>
12:     </tr>
13:     <tr>
14:     <td><%= f.collection_select :ticket_product_id,
@ticket_products, :id, :name, :prompt=> "Seleccione producto" %></td>
15:     <td><%= f.text_field :ticket_breakdown_id %> </td>
16:     <td><


--------------------------------
i don´t ubderstand it, because I have two elements in ticket_products


Somebody can help me please??
0fdce938a28a604fd5613a68b0d78fcb?d=identicon&s=25 Jose vicente Ribera pellicer (joripel)
on 2008-11-26 18:06
Sorry,don´t take care this code:
<td><%= f.collection_select :ticket_breakdown_id,
> @ticket_breakdowns, :id, :title, :prompt => _("Select breakdown") %>
> </td>
>     <td><%= f.collection_select :ticket_state_id, @ticket_states, :id,
> :title, :prompt => _("Select state") %></td>
>     </tr>

It will be implemented soon as possible but not yet.
0fdce938a28a604fd5613a68b0d78fcb?d=identicon&s=25 Jose vicente Ribera pellicer (joripel)
on 2008-11-26 19:42
Solved.....it is normal....:(

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.map

tickets_controller.rb
def new
    @ticket = Ticket.new
    @ticket_products= TicketProduct.find(:all, :order =>'name')  ## this
line finds the tickets product!!!

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @ticket }
    end
  end


Thanks a lot!!!
This topic is locked and can not be replied to.