开发者

acts_as_list problem with scope

The problem I have is at the bottom.

Models:

class Skill
  has_many :tags  
  acts_as_list :column => 'sequence'
end

class Tag
  belongs_to :skill 
  acts_as_list :column => 'sequence', :scope => :skill
end

View:

<table id="skills">

  <% @skills.each do |s| %>
    <tr id="skill_<%= s.id %>">

      <td>
        <%= s.name %>
      </td>

      <td>
        <ul id="tags">
        <% s.tags.each do |t| %>
          <li id="tag_<%= t.id %>">
            <%= t.name %>
          </li>
        <% end %>
        </ul>
      </td>

    </tr>
  <% end %>

</table>

jQuery for drag and drop开发者_JS百科:

$( "#skills" ).sortable({          
  axis: 'y',
  dropOnEmpty: false,
  handle: '.handle',
  cursor: 'move',
  items: 'tr',
  opacity: 0.4,
  scroll: true,
  update: function(){
    $.ajax({
      type: 'post',
      data: $('#skills').sortable('serialize') + "&authenticity_token=" + "<%= form_authenticity_token %>",
      dataType: 'script',
      complete: function(request){
        $('#skills').effect('highlight');
      },
      url: '<%= url_for :action => 'sort', :controller => 'skills' %>'
    })
  }
});

$( "#tags" ).sortable({          
  axis: 'y',
  dropOnEmpty: false,
  handle: '.handle',
  cursor: 'move',
  items: 'li',
  opacity: 0.4,
  scroll: true,
  update: function(){
    $.ajax({
      type: 'post',
      data: $('#tags').sortable('serialize') + "&authenticity_token=" + "<%= form_authenticity_token %>",
      dataType: 'script',
      complete: function(request){
        $('#tags').effect('highlight');
      },
      url: '<%= url_for :action => 'sort', :controller => 'tags' %>'
    })
  }
});

Controller for Tags:

def sort
  @tags = Tag.all
  @tags.each do |tag|
    tag.sequence = params['tag'].index(tag.id.to_s) + 1
    tag.save
  end
  render :nothing => true
end

PROBLEM:

After dragging the tags there is an error:

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.+):
  app/controllers/tags_controller.rb:12:in `block in sort'
  app/controllers/tags_controller.rb:11:in `each'
  app/controllers/tags_controller.rb:11:in `sort'

I found the error is gone if I load tags belonging to a specific Skill like this:

def sort
  @tags = Skill.find(1).tags

Question - how to tell the controller which tags to load (but not all tags)?


A SOLUTION I FOUND...

Tags controller:

def sort
  @tag = Tag.find(params[:tag]).first
  @skill = Skill.find_by_id(@tag.skill_id)
  @tags = @skill.tags

Is this the best way to do it?


The error occurred while evaluating nil.+):

This means that whatever params['tag'].index(tag.id.to_s) + 1 (from sort in controller) is supposed to do is actually resulting in nil + 1.

If your solution performs as expected, then I don't see a problem with it.

As a tip, @skill = Skill.find_by_id(@tag.skill_id) could be shortened to @skill = @tag.skill if you do belongs_to :skill in your Tag model.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜