简体   繁体   English

在嵌入式 Ruby 中使用 JavaScript 将变量作为参数传递

[英]Using JavaScript in embedded Ruby to pass a variable as a parameter

Short story:短篇故事:

I am trying to pass a variable from JavaScript (specifically, jQuery) as a parameter in an embedded ruby method.我正在尝试将 JavaScript(特别是 jQuery)中的变量作为嵌入式 ruby​​ 方法中的参数传递。

Long story:很长的故事:

It all begins with a function.这一切都始于一个函数。 In summary, I have date picker built via jQuery UI.总之,我通过 jQuery UI 构建了日期选择器。

#my_budget.html.erb
<h1 class="text-center">Your Budget</h1>
<h4 class="text-center">Select Month to View</h4>
<input style="width:200px" name="startDate" id="startDate" class="date-picker form-control" placeholder="Select Month"/>

<br>
<br>

<script type="text/javascript">
  function setDateRange() {
    $('.date-picker').change(function() {
     var date = $(this).val(); 
     return date
    });
  }

  $(document).ready(setDateRange);
</script>

Further down the road, I have embedded ruby attempting to make a call to said variable (date).再往前走,我已经嵌入了 ruby​​ 试图调用所述变量(日期)。 This value is passed via MM/YYYY format.该值通过 MM/YYYY 格式传递。

#my_budget.html.erb
<td class="section-value"><%= number_to_currency(@user.total_income(date)) %></td>

Here's a peek at my model.这是我的模型的一瞥。

#user.rb
# General Income
  def total_income(current_month)
    total_value = Transaction.joins(:user).where("transactions.sub_category" => '11').where("created_at = ?", current_month).sum(:amount)
    total_value
  end

For fairly obvious reasons, this does not work.由于相当明显的原因,这不起作用。

I am a junior developer in Rails and am still learning when to use Ruby versus JS for certain functions.我是 Rails 的初级开发人员,仍在学习何时使用 Ruby 与 JS 来实现某些功能。

What is the best way to go about passing my JS variable into my embedded Ruby?将我的 JS 变量传递到我的嵌入式 Ruby 中的最佳方法是什么? Alternatively, how can I call the input value "on change" via Ruby into my function, so when a user selects "March 2017" in the date picker, Ruby says "Hey, it changed! Let's use it!"或者,我如何通过 Ruby 将输入值“on change”调用到我的函数中,因此当用户在日期选择器中选择“March 2017”时,Ruby 会说“嘿,它改变了!让我们使用它!” and adjusts the value accordingly.并相应地调整值。

Thank you!谢谢!

Simply put, Ruby runs on your server and all erb tags are evaluated by the time it reaches the client.简而言之,Ruby 在您的服务器上运行,所有 erb 标签在到达客户端时都会进行评估。 Ex:前任:

<%= number_to_currency(@user.total_income(date)) %>

If you attempt to change "date" with javascript you will fail because by the time javascript tries to change it, rails and your server already evaluated the method and it will return that value not the Ruby method.如果您尝试使用 javascript 更改“日期”,您将失败,因为当 javascript 尝试更改它时,rails 和您的服务器已经评估了该方法,它将返回该值而不是 Ruby 方法。

Now, you have two simple options here:现在,您有两个简单的选择:

  1. Write a Javascript function that does the same what your Ruby code is suppose to do.编写一个 Javascript 函数,它的作用与您的 Ruby 代码假定的作用相同。
  2. Sent an Ajax call to your server, let Rails process the request and respond with json or javascript to the client.向您的服务器发送 Ajax 调用,让 Rails 处理请求并使用 json 或 javascript 响应客户端。

The first option is probably simpler and will be better for your server since it would not have to take care of extra requests.第一个选项可能更简单,并且对您的服务器更好,因为它不必处理额外的请求。

The second option requires you to create a form and set it to remote: true .第二个选项要求您创建一个表单并将其设置为remote: true In your controller you will have to receive the content of the form, call your @user.total_income method and set the respond_to so that it return json or javascript:在您的控制器中,您必须接收表单的内容,调用您的 @user.total_income 方法并设置respond_to以使其返回 json 或 javascript:

@user_income = @user.total_income(params[:date)
respond_to do |format|
  format.html
  format.js
end

Then you will have to create a javascript file in your views folder and write some javascript with the info you want the user to get.然后,您必须在您的视图文件夹中创建一个 javascript 文件,并使用您希望用户获得的信息编写一些 javascript。 Simplistically, it will look something like this:简单地说,它看起来像这样:

$('#where-to-put-your-income').append('<%= j render @user_income %>');

You can read about how to use Ruby and Javascript here:您可以在此处阅读有关如何使用 Ruby 和 Javascript 的信息:

http://guides.rubyonrails.org/working_with_javascript_in_rails.html http://apidock.com/rails/ActionController/MimeResponds/InstanceMethods/respond_to http://guides.rubyonrails.org/working_with_javascript_in_rails.html http://apidock.com/rails/ActionController/MimeResponds/InstanceMethods/respond_to

Good luck, I'm also still a Junior Rails developer, let's do our best.祝你好运,我还是一名初级 Rails 开发人员,让我们尽力而为。

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

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