Ruby on Rails:没有视图的操作 [英] Ruby on Rails: Action without View

查看:42
本文介绍了Ruby on Rails:没有视图的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我认为非常简单的问题.我来自 PhP 背景,过去一直这样做,所以我可能看错了.

I have what I think is a very simple problem. I'm coming from a PhP background, and used to do this all the time, so I may be looking at this the wrong way.

我正在尝试在 RoR 中创建一个 ajax 处理程序.当用户单击按钮时,javascript 会触发 POST,并使用 jQuery ajax 函数的success:"参数向用户提供反馈.

I am trying to create an ajax handler in RoR. When the user clicks a button, javascript fires off a POST, and gives the user feedback using the "success:" parameter of jQuery's ajax function.

问题是,RoR 正在尝试为 ajax 处理程序加载视图,当我真的只需要控制器中的几行代码来完成数据库工作,并回显出将由用户的 javascript 解释的状态代码时.

The problem is, RoR is trying to load a view for the ajax handler, when I really just need a few lines in the controller to do the database work, and echo out a status code that will be interpreted by the user's javascript.

这只是一个mailchimp订阅保持页面,所以我只使用'home'控制器.

This is all just a mailchimp subscribe holding page, so I am only using the 'home' controller.

我的路线;

map.root :controller => 'home'
map.connect '/mcsubscribe', :controller => 'home', :action => 'mcsubscribe'

我的家庭控制器;

class HomeController < ApplicationController
  def index
    # no content
  end
  def mcsubscribe
    print params[:email]
  end
end

还有我的 javascript 测试,只是为了让您了解发生了什么;

And my testing javascript, just so you understand what's going on;

function mcSubscribe() {
    var email = jQuery("#signup_input_email").val();
    jQuery.ajax({
        type: "POST",
        url: "http://domain.com/mcsubscribe",
        data: "email=" + email,
        cache: false,
        success: function(result) {
            alert(result);
        }
    });
}

我认为这会是一个常见的问题,但我已经搜索了很多,只能找到重定向的建议,因为用户永远不会访问/mcsubscribe 页面,这似乎不合适.

I thought this would be a common problem, but I've googled around and only managed to find suggestions to redirect, as the user will never visit the /mcsubscribe page, that doesn't seem appropriate.

推荐答案

使用

print params[:email]

只会将该值打印到应用程序日志中,而不会打印到响应中.

will just print that value to the application logs, not into the response.

你想要这个:

render :text => params[:email]

这篇关于Ruby on Rails:没有视图的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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