2014-11-03 66 views
0

我正嘗試使用Cocoon gem將用戶添加到資產。唯一的問題是我需要設置它來限制可以應用於資產的用戶關聯的數量,這些關聯可以基於資產類型(硬件/軟件,硬件爲1,軟件不會定義)。目前我可以繼續添加用戶關聯到資產。有沒有辦法限制關聯的數量?Cocoon gem:如何限制關聯數量

任何幫助表示讚賞。先謝謝你。

Asset.show:

- if @asset.users.empty? 
     = simple_form_for([@asset_profile, @asset]) do |f| 
     #assets_users 
      = f.simple_fields_for :assets_users do |assets_user| 
      = render "assets_user_fields", :f => assets_user 
      .links 
      = link_to_add_association "Add Another User", f, :assets_users 
      = f.submit 

_assets_users_fields.html.haml:

.nested-fields 
    = f.input :user_id, collection: @users.order(:last_name), :label => "User" 
    = link_to_remove_association "Remove", f 

我試圖解決我的另一個問題所看到的,但我得到一個錯誤在if $("#assets_users .nested-fields").length() is 1說明Uncaught TypeError: number is not a function

的JavaScript我想:

$ -> 
    check_to_hide_add_link = -> 
    if $("#assets_users .nested-fields").length() is 1 
     $("#assets_users .links a").hide() 
    else 
     $("#assets_users .links a").show() 
    return 
    $("#assets_users").bind "cocoon:after-insert", -> 
    check_to_hide_add_link() 
    return 

    $("#assets_users").bind "cocoon:after-remove", -> 
    check_to_hide_add_link() 
    return 

    check_to_hide_add_link() 
    return 

再次,任何的幫助深表感謝。

編輯:鏈接所指的問題:Cocoon add association, how to limit number of associations

生成的JavaScript:

$(function() { 
    var check_to_hide_add_link; 
    check_to_hide_add_link = function() { 
    if ($("#assets_users .nested-fields").length() === 1) { 
     return $("#assets_users .links a").hide(); 
    } else { 
     return $("#assets_users .links a").show(); 
    } 
    }; 
    $("#assets_users").bind("cocoon:after-insert", function() { 
    return check_to_hide_add_link(); 
    }); 
    $("#assets_users").bind("cocoon:after-remove", function() { 
    return check_to_hide_add_link(); 
    }); 
    return check_to_hide_add_link(); 
}); 
+0

你不要鏈接到你找到答案的問題([我的答案] (http://stackoverflow.com/questions/12934925/cocoon-add-association-how-to-limit-number-of-associations?rq=1)btw),但afaik你的問題是在咖啡翻譯。你能告訴我們生成的JavaScript嗎? – nathanvda 2014-11-04 09:57:52

+0

我對此表示歉意。鏈接被添加,以及生成的JavaScript。 – Briknthewall 2014-11-04 14:51:02

回答

相關問題