에서 모델의 메소드 사용 자산에 사용자를 설정하려고하는데 메소드를 asset.rb
에서 내 JS 코드로 전달하는 방법을 잘 모르는 문제가 있습니다. Cocoon gem과 함께 max_users 메소드를 사용하여 해당 자산에 대해 지정된 max_users에 도달하면 "Add Another"버튼이 사라집니다.자바 스크립트 : 자바 스크립트 코드
하드웨어는 항상 max_user가 1로 설정되지만 소프트웨어는 max_user 값을 갖지 않습니다.
Profile_type과 마찬가지로 Max_users가 정상적으로 작동합니다. JS는 그대로 작동하지만, 항상 1로 멈추는 대신, max_users에 값을 등록하고 그 표시/숨기기를 기반으로해야합니다.
도움이 될 것입니다. 그리고 미리 감사드립니다.
JS :
$ ->
check_to_hide_add_link = ->
if $("#assets_users .nested-fields").length is 1
$("#assets_users .links a").hide()
else
$("#assets_users .links a").show()
$("#assets_users").bind "cocoon:after-insert", ->
check_to_hide_add_link()
$("#assets_users").bind "cocoon:after-remove", ->
check_to_hide_add_link()
check_to_hide_add_link()
Asset.show :
- if @asset.users.empty?
= simple_form_for([@asset_profile, @asset]) do |f|
= f.input :max_users, as: :hidden
#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_user_fields :
.nested-fields
= f.input :user_id, collection: @users.order(:last_name), :label => "User"
= link_to_remove_association "Remove", f
Asset.rb :
def max_users
if self.asset_profile.profile_type == "Hardware"
1
end
end
.
.
.
def length_of_users
if user_ids.count > max_users
errors.add(:users, "You can only add a maximum of #{max_users} users")
end
end