如何使用select_tag

时间:2019-01-17 17:02:55

标签: jquery ruby-on-rails

我已经使用rails select_tag来检索用户列表,像这样。

- @mandatory_signatures.each do |mandatory_signature|
    .form-group.row.mt-4
      .col-sm-5
        %label Choose #{mandatory_signature.signature_title}
        .centered-vertical
          = select_tag '', options_from_collection_for_select(find_users_with_certificate_for(mandatory_signature), :id, :full_name), { include_blank: "Select User", class: 'form-control select2 find-user-certificate', style: 'width: 100%;' }

      .col-sm-5
        %label Choose Certificate
        .centered-vertical
          = select_tag nil, "", { include_blank: "Select Certificate", class: 'form-control select2 certificate-list', style: 'width: 100%;' }

      .col-sm-2.pl-3
        %label Signature
        .form-control.invisible
        %img.rounded-circle{:src => "http://via.placeholder.com/80x80"}

这将返回用户列表,并在选择其中一个用户时,我想调用一个API并列出属于用户的证书。

使用此jquery函数,我可以检索证书列表:

$('.find-user-certificate').change(function() {
    let user_id = $(this).val();

    getUserCertificates(user_id);
});

function getUserCertificates(userId) {
  let hostname = window.location.origin;

  $.ajax({
    url: hostname + '/users/' + userId + '/certificates',
    dataType: 'json',
    method: 'get',
    success: function(data) {
      options = '';
      data.forEach(function(object) {
        options += `<option value=${object.id}>${object.name}</option>`;
      });
      $('.certificate-list').empty();
      $('.certificate-list').append(options);
    }
  });
}

现在的问题是,每当我尝试为用户检索证书时,它都会填充其他下拉列表以及所需的下拉列表。我尝试使用HTML id和class,但未产生正确的结果。

这是下面的屏幕截图中的样子: enter image description here

正确的方法是什么?使下拉列表中的“用户选择”仅填充相应下拉列表的证书列表?

1 个答案:

答案 0 :(得分:0)

您需要以某种方式定义更新的下拉列表。 import PushNotification from "../services/push_notifications"; import axios from 'axios'; async componentDidMount(){ let tokenFromStorage = await zlAsyncStorage.getItem('pushToken'); console.log('token from storage',tokenFromStorage);return; let token = await PushNotification(); AsyncStorage.setItem('pushToken',token); console.log("here"); //Save the token in couch db await axios({ url:"http://192.168.8.148:5984/mycompany", method: 'post', timeout: 1000, headers: { 'Accept-Encoding' : 'gzip, deflate', 'Content-Type':'application/json', 'Authorization':'Basic YTph' }, data: { user: "cheran", tokenReceived : token, }, auth: { username: 'a', password: 'a' }, }).then(function(response){ //console.log(response); }); } 返回页面上的所有证书下拉列表。您可以为此目的使用id(必填)签名。

$('.certificate-list')