2011-08-02 3 views
2

問題は私が一番下にあります。acts_as_listスコープの問題

モデル:

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

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

ビュー:

<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のドラッグ&ドロップのため:

$("#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' %>' 
    }) 
    } 
}); 

コントローラタグ用:

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 

問題:

エラーがあるタグをドラッグした後:

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' 

私はこのような特定のスキルに属するタグを読み込む場合は、エラーがなくなっていました

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

質問質問:ロードするタグをコントローラに伝える方法すべてのタグではない)私は...

タグコントローラFOUND


SOLUTION:

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

が、これはそれを行うための最善の方法ですか?

答えて

2

The error occurred while evaluating nil.+):

これは(コントローラでソートから)何params['tag'].index(tag.id.to_s) + 1が行うことになっていることを意味し、実際にnil + 1が得られています。

ソリューションが期待どおりに機能する場合は、問題は発生しません。

タグモデルでbelongs_to :skillを入力すると、ヒントとして@skill = Skill.find_by_id(@tag.skill_id)@skill = @tag.skillに短縮することができます。

+1

upvote apprec: – tybro0103