Forum: Ruby on Rails I need help with my 1st sortable list!

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.
Rob B. (Guest)
on 2006-06-02 20:24
Ill keep it brief.  Dragging an item is working but dropping and
updating the list gives me this error -

NoMethodError in AdminController#sort

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

#{RAILS_ROOT}/app/controllers/admin_controller.rb:171:in `sort'

# My view -

<ul id="price">
  <% @prices.each do |@price| %>
  <li id="price_<%= @price.id %>">
	<%= @price.service %>  <%= @price.price %>
  </li>
  <% end %>
</ul>

<p id="list-info"></p>

<%= sortable_element 'price',
       :update => 'list-info',
      :url => { :action => "sort", :id => @price },
      :complete => visual_effect(:highlight, 'pricelist')
%>

# Controller -

def sort
  @price = Price.find(params[:price])
  @prices.each do |@price|
    @price.position = params['price'].index(@price.id.to_s) + 1
    @price.save
  end
  render :nothing => true
end

# Model -

class Price < ActiveRecord::Base
  belongs_to :user
  acts_as_list :scope => :user
end
Bill W. (Guest)
on 2006-06-02 21:11
(Received via mailing list)
Hi Rob,

I'm guessing it's late where ever you're writing from ;-)

Rob B. wrote:

> NoMethodError in AdminController#sort
> #{RAILS_ROOT}/app/controllers/admin_controller.rb:171:in `sort'
>
> # My view -

Why are you looking at the view?  Rails gave you the two lines above
telling
you to look in your controller.  One of them even tells you what line
number
in the controller, _and_ what method it's in in case your editor doesn't
show line numbers.

> You have a nil object when you didn't expect it!

> The error occured while evaluating nil.each

Any occurrances of ".each" in the code below?

> def sort
>  @price = Price.find(params[:price])
>  @prices.each do |@price|
>    @price.position = params['price'].index(@price.id.to_s) + 1
>    @price.save
>  end
>  render :nothing => true
> end

You're not assigning anything to @prices before you try to enumerate
it's
elements.

Best regards,
Bill
Rob B. (Guest)
on 2006-06-05 12:55
> You're not assigning anything to @prices before you try to enumerate
> it's
> elements.

I'm not following sorry.  What do I need to assign?
This topic is locked and can not be replied to.