简体   繁体   English

如何在JavaScript中从控制器的视图传递变量-Rails 4

[英]how to pass variable from view in controller in javascript - Rails 4

I woud like to read a variable which is introduced by the user and pass it to my controller. 我想读取用户引入的变量并将其传递给我的控制器。 I am very new to ruby and rails. 我对红宝石和Rails很陌生。 The index.html.erb is something like this: index.html.erb是这样的:

<h1>Welcome#index</h1> 
<p>Find me in app/views/welcome/index.html.erb</p> 
<h1>Spline Chart</h1> 


<div class="boxed">
      <strong>Jahreshausverbrauch (kWh):</strong>
      <%= form_tag do %>
          <%= text_field_tag "user_entry_module1", nil, placeholder: "Bsp. 3500" %>
          <%= submit_tag "Berechnen" %>
      <%end%>
</div> 


<div id="container"></div> 

<script > 

  $(document).ready(function() { 
    var options = { 
        chart: { 
            renderTo: 'container', 
            type: 'spline' 
        }, 
        series: [{}] 
    }; 


    $.getJSON('/welcome/get_data/?factor=1', function(data) { 
        options.series[0].data = data; 
        var chart = new Highcharts.Chart(options); 
    }); 
  }); 
</script> 

So instead of ?factor=1 i want to pass user_entry_module1 which is introduced in the form by the client. 因此,我不是传递?factor = 1而是要传递user_entry_module1,它是由客户端以表格形式引入的。

My controller is something like this: 我的控制器是这样的:

class WelcomeController < ApplicationController

  def index



  end  

  def get_data 
    #From json to hash - consumption_profile_generic 
     file_path = Rails.root.join('db','consumption_profile_generic_some_columns.js')  
     file_gen = File.read(file_path) 
     data_hash_gen = JSON.parse(file_gen) 


  # Filtering date and consumption_% ind 2 vectors and then merging them
  num = data_hash_gen.count 
  vectorA = Array.new
  vectorB = Array.new
  i = 0
  while num > i
    vectorA[i] = data_hash_gen[i]["consumption_%"].to_f  
    vectorB[i] = data_hash_gen[i]["date"]
    i += 1
  end

  data = vectorB.zip(vectorA)

    factor = params[:factor].to_i 
    data = data.map{|i| [i[0], i[1] * factor]} 

  render json: data

  end 

If you just want to have the value of text field in your js code you can do it like: 如果您只想在js代码中使用text字段的值,则可以执行以下操作:

<%= text_field_tag "user_entry_module1", nil, placeholder: "Bsp. 3500". id: "user_entry" %> # add jquery selecter id

now in your js code do: 现在在您的js代码中执行以下操作:

   $(document).ready(function() { 
     var myVar;
     var options = { 
       chart: { 
         renderTo: 'container', 
         type: 'spline' 
       }, 
       series: [{}] 
     }; 

     $(document).on('change', '#user_entry', function(){
       myVar = $(this).val();
     }

     $.getJSON('/welcome/get_data/?factor=' + 'myVar', function(data) { 
       options.series[0].data = data; 
       var chart = new Highcharts.Chart(options); 
    }); 
   }); 

If you will get issue in joining your variable with url then covert variable into string then join with url. 如果在将变量与url联接时出现问题,然后将隐式变量转换为字符串,然后与url联接。

Hope I get your problem correctly and this code will help you. 希望我能正确解决您的问题,此代码将为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM