Cocoon gem を使用してユーザーをアセットに追加しようとしています。唯一の問題は、アセットのタイプ (ハードウェア/ソフトウェア、ハードウェアが 1 の場合、ソフトウェアは定義されません) に基づいて、アセットに適用できるユーザー関連付けの量を制限するように設定する必要があることです。 )。現時点では、ユーザーの関連付けをアセットに追加し続けることができます。関連付けの量を制限する方法はありますか?
どんな助けでも大歓迎です。前もって感謝します。
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
別の質問で見た解決策を試しましたがUncaught TypeError: number is not a function
、if $("#assets_users .nested-fields").length() is 1
.
私が試した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 関連付けの追加、関連付けの数を制限する方法
生成された 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();
});