简体   繁体   English

是否可以将Rails的值返回给客户端JavaScript?

[英]Is it possible to return value from Rails to the client-side javascript?

I am working on a move from a Python back end to Ruby on Rails, and some of the legacy client-side js code looks like this: 我正在研究从Python后端迁移到Ruby on Rails的过程,一些旧式客户端js代码如下所示:

$http.post(url, data, config).
    success(function(data, status, headers, config) {
        if(data == 0) {
            $scope.subscribed = 'true'
        } else {
            alert("There is an issue with that email address.")
            return
        }
    }).
        error(function(data, status, headers, config) {
        return
    });

I now have the back-end doing what it is supposed to do here, but unless I use the flash, I'm not sure how to return the success/failure status to the calling function. 我现在让后端执行这里应该做的事情,但是除非使用闪光灯,否则我不确定如何将成功/失败状态返回给调用函数。 I'd like to just return an integer value so the code could continue to work as is. 我只想返回一个整数值,以便代码可以按原样继续工作。 Is that possible? 那可能吗? Or do I need to rewrite the front-end to use the flash hash? 还是需要重写前端才能使用Flash哈希?

EDIT: For the record, this is what the backend code that handles this looks like. 编辑:记录下来,这就是处理此问题的后端代码。

Routes: 路线:

Rails.application.routes.draw do
  root to: 'static_pages#index'

  resources :subscriptions, only: [:create]

  match '/subscribe', to: 'subscriptions#subscribe', via: :post

end

The controller: 控制器:

class SubscriptionsController < ApplicationController
  protect_from_forgery
  returnVal = -1

  def subscribe  
    begin
      # Do some stuff that could cause an exception>
      flash[:success] = "Subscribed successfully"
      logger.debug flash[:success]
      returnVal = 0
    rescue <Some exception>
      flash[:error] = "Error"
    rescue <Another exception>
      flash[:error] = "Error"
    end

    if (!flash[:error].blank?)
      logger.debug flash[:error]
    end

    render 'static_pages/index'
    return returnVal
  end
end

All seems to be working fine except for the "return" part. 除了“返回”部分外,其他一切似乎都工作正常。

In Rails controllers, you don't return the text you want to send to the browser. 在Rails控制器中,您不return要发送到浏览器的文本。 Instead, you render it. 而是render它。

For example: 例如:

render plain: returnVal

(From the Rails Guide on layouts and rendering ) (来自有关布局和渲染的Rails指南

Also, you probably don't need the flash hash. 另外,您可能不需要Flash哈希。 flash is for temporarily storing session data and retrieving it in the next request. flash用于临时存储会话数据并在下一个请求中检索它。 Since you're only making one request here, flash is unnecessary. 由于您仅在此处提出一个请求,因此不需要flash

Or do I need to rewrite the front-end to use the flash hash? 还是需要重写前端才能使用Flash哈希?

The front-end can't use the flash hash. 前端不能使用Flash哈希。 That's only accessible on the server. 那只能在服务器上访问。

I'm not sure how to return the success/failure status to the calling function. 我不确定如何将成功/失败状态返回给调用函数。

Your use of phrases like this makes me wonder if you really understand what's going on here. 您对这样的短语的使用使我想知道您是否真的了解这里发生了什么。 You can't directly call sever-side functions from the client. 您不能直接从客户端调用服务器端功能。 All you can do is make HTTP requests, just like the browser does when it visits a page. 您所能做的就是发出HTTP请求,就像浏览器访问页面时一样。 Your code just tells the browser to download a page, and the Rails backend renders the response inside that page, just like it would with any other webpage you might visit. 您的代码仅告诉浏览器下载页面,Rails后端在页面内呈现响应,就像您可能会访问的任何其他页面一样。

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

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