开发者

Having trouble sorting using Ruby on rails and acts as list, position won

I have Groups that has_many projects. I also created an order view that is similar to my Show view. The reason for creating an additional view is that I am using the Group#show view for a primary display in my site.

So I have the following in my Groups_Controller

 def order
  @group = Group.find(params[:id])
 end

 def sort 
  @group = Group.find(params[:id]) 
  @group.projects.each do |p|
  p.position = params['group_projects'].index(p.id.to_s) + 1
  p.save
 end
render :nothing => true 
end

Then in my view

<ul id="group_projects">
  <% @group.projects.each do |p|%>  
<li class="project_<%= p.id %>"><%= p.title %> - <%= p.position %></li>
  <% end -%>

 <%= sortable_element 'group_projects', :url => { :action => "sort", :id => @group }, :complete  => visual_effect(:highlight, 'group_projects')%>

I have not messed with my routes, but I wonder if thats part of the problem.

So the sorting works but its not saving the position. The error I am getting is the following:

Processing GroupsController#sort (for 127.0.0.1 at 2010-05-03 11:14:19) [POST]
Parameters: {"authenticity_token"=>"vODeoGHH4osrDeBY+fBI/x+YgEs6SJBO15cF/qLqW5o=", "id"=>"1"}
User Load (47.6ms)   SELECT * FROM "users" WHERE ("users"."id" = 1) LIMIT 1
Group Load (0.5ms)   SELECT * FROM "groups" 
Group Load (0.2ms)   SELECT * FROM "groups" WHERE ("groups"."id" = 1) 
Project Load (1.7ms)   SELECT * FROM "projects" WHERE ("projects".group_id = 1) ORDER BY position

NoMethodError (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.index):
app/cont开发者_Python百科rollers/groups_controller.rb:136:in `sort'
app/controllers/groups_controller.rb:135:in `sort'

Rendered rescues/_trace (106.7ms)
Rendered rescues/_request_and_response (1.2ms)
Rendering rescues/layout (internal_server_error)

Any ideas on how I can solve this problem. I know there's a nil.Object, I just don't see how.


Without having the line numbers to match with the error, it looks like the line

p.position = params['group_projects'].index(p.id.to_s) + 1

is where the problem lies. params['group_projects'] is coming back nil for some reason.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜