Ruby on Rails:在同一页面上使用用户输入进行计算 [英] Ruby on Rails: Using user input for calculations on the same page

查看:25
本文介绍了Ruby on Rails:在同一页面上使用用户输入进行计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我遇到了一个我认为非常简单的问题.我不知道如何访问用户输入……或者我不知道如何在 rails 中声明一个临时变量.

无论如何,最直接的方法是什么:

 

输入组大小:<%= number_field_tag(:group_size)%>选择研究地点:<%= number_field_tag(:site) %>

<% if :site >4%>你好!<%其他%>不!<%结束%>

我想我需要 javascript 才能让它真正工作,但现在我只需要知道如何使用这些变量.

任何帮助将不胜感激.谢谢.

解决方案

与大多数事情一样,解决方案涉及更多:

<小时>

Ajax

与原生应用不同,Rails 依赖于 HTTP 协议

HTTP 适用于请求.您向服务器发送请求以呈现网页;服务器响应请求.这样做的问题是你不能在不向服务器发送请求的情况下使用具有实时"功能的 Rails(即使实时"应用程序也只是保持永久连接打开,充当单个请求)

这意味着如果您想处理实时"数据(不刷新),您必须使用一种技术来代表您发送请求.正如您所指出的,这将是 ajax:

$(".element").on("action", function(){$.ajax({网址:你的/终点/点"数据:{您的:数据}成功:功能(数据){警报(数据)}});});

<小时>

导轨

在 Rails 中处理 ajax 请求与处理 HTTP 请求基本相同:

#config/routes.rb资源:控制器做得到 :your_endpoint结尾#app/controllers/controllers_controller.rb定义 your_endpoint# 在这里执行操作response_to do |格式|格式.htmlformat.js { # 处理 JS/Ajax 请求 }结尾结尾

<小时>

退货

然后您可以使用您的 JS (Ajax) 函数处理返回的数据.这给出了它实时"工作的图像,但实际上将发送 &每次都收到服务器的请求

So, I'm having issues with what I think is a really simple problem. I don't know how to access user input...or perhaps I don't know how to declare a temporary variable in rails.

Anyway, what is the most straight forward way of accomplishing this:

    <div>
        Enter Group Size: <%= number_field_tag(:group_size)%>
        Select Study Site: <%= number_field_tag(:site) %>
    </div>
    <% if :site > 4 %>
            Hello!
    <% else %>
            Nope!
    <% end %> 

I suppose I'll need javascript to actually make it work, but for now I just need to know how to use these variables.

Any help would be greatly appreciated. Thanks.

解决方案

As with most things, the solution is a lot more involved:


Ajax

Unlike native apps, Rails relies on the HTTP protocol

HTTP works on requests. You send requests to a server to render a web page; the server responds to the requests. The problem with this is you cannot use Rails with "live" functionality without sending requests to-and-from the server (even "live" applications just keep a perpetual connection open, acting as a single request)

This means if you want to process "live" data (without refresh), you'll have to use a technology to send a request on your behalf. As you noted, this will be ajax:

$(".element").on("action", function(){
    $.ajax({
        url: "your/end/point"
        data: {your: data}
        success: function(data) {
            alert(data)
        }
    });
});


Rails

To handle an ajax request in Rails, it's basically the same as handling an HTTP request:

#config/routes.rb
resources :controller do
    get :your_endpoint
end

#app/controllers/controllers_controller.rb
def your_endpoint
    # perform actions here
    respond_to do |format|
        format.html
        format.js { # handles JS / Ajax request }
    end
end 


Return

You can then handle the returned data with your JS (Ajax) function. This gives the image that it's working in "real time", but will actually be sending & receiving requests from the server every time

这篇关于Ruby on Rails:在同一页面上使用用户输入进行计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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