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.
F779209a7f02c754eda7b505898347f9?d=identicon&s=25 Rob Balfour (robtotheb)
on 2006-06-02 18: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
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2006-06-02 19:11
(Received via mailing list)
Hi Rob,

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

Rob Balfour 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
F779209a7f02c754eda7b505898347f9?d=identicon&s=25 Rob Balfour (robtotheb)
on 2006-06-05 10: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.