Javascript:在JavaScript代码中使用模型中的方法 [英] Javascript: Using a method from model in javascript code

查看:100
本文介绍了Javascript:在JavaScript代码中使用模型中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在资产上设置用户,并且遇到了不确定如何将max_users方法从asset.rb传递给我的JS代码的问题.我尝试将max_users方法与Cocoon gem配合使用,以使添加其他"按钮在达到该资产允许的指定max_users时消失.

I am trying to set up users on an asset, and I am running into the problem of not being sure how to pass my max_users method from asset.rb to my JS code. I am trying to use the max_users method with the Cocoon gem to have the "Add Another" button disappears when it reached the specified max_users allowed for that asset.

硬件始终将max_user设置为1,而软件永远不会具有max_user值.

Hardware will always be set with a max_user of 1, while software will never have a max_user value.

Max_users和profile_type一样可以正常工作. JS照原样工作,但与其让它始终停止在1上,不如让它在max_users中注册该值,并以此为基础进行显示/隐藏.

Max_users works as it should, as does profile_type. The JS works as it is, but instead of it always stopping at 1, I need to have it register the value in max_users, and base the show/hide off of that.

任何帮助将不胜感激.并先谢谢您.

Any help will be much appreciated. And thank you in advance.

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()

资产展示:

- 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:

_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

推荐答案

必须检索max_users的值,并将其转换为整数.然后将其插入,以便如果显示的字段数量大于或等于max_users,则表单将停止生成新字段.

Had to retrieve the value of max_users, and convert it into an integer. Then just plugged it in so that if the amount of fields showing up was greater than, or equal to, the max_users, the form would stop generating new fields.

$ ->
  check_to_hide_add_link = ->
    max_users = parseInt($("#asset_max_users").val(), 10)
    if $("#assets_users .nested-fields").length >= max_users
      $("#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()

这篇关于Javascript:在JavaScript代码中使用模型中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆