Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

252
Visualizações
Rails + Vue: Accessing instance variable

I have a simple view, but Purchase belongs to :client. Therefore to access the purchases I need to provide a client_id. :

$(document).ready(function() {
  var purchases = new Vue({
    el: '#purchases',
    data: {
      purchases: []
    },
    ready: function() {
      var that;
      that = this;
      $.ajax({
        url: 'clients/' + CLIENT_ID + '/purchases.json', // problem
        success: function(res) {
          console.log(res)
          that.purchases = res;
        }
      });
    }
  });
});

How do I access the client_id in the ajax request? I tried <%= @client.id %> but it get the error undefined methodid' for nil:NilClass`.

How do I pass that variable?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The answer to the question is you cant access an instance variable directly in a js function. This is because js is a client side language which only runs in your browser and ruby/rails is a server side language.

The only way you can access the variable in js is by passing it as an html data. For eg as a hidden field in your corresponding html.erb file.

<%= hidden_field_tag('client_id', @client_id) %>

and accessing it as

client_id: $('#client_id').val()

in the js file.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda