[英]AJAX Jquery in rails
I am trying to pass data from view to Controller, but data not passing so what is wrong in my app ? 我正在尝试将数据从视图传递到Controller,但是数据没有传递,那么我的应用程序出了什么问题?
in view: 鉴于:
var url = 'searchs/index';
var url ='搜索/索引';
$( "#h" ).click(function() {
$(“ #h”).click(function(){
$.ajax({ type: "GET", url: url, data: {xx: 'q'} });
});
});
Controller: 控制器:
class SearchsController < ApplicationController
类SearchsController <ApplicationController
def index
定义指数
@x=User.new(:name=> params[:xx])
@ x = User.new(:name => params [:xx])
@x.save
@ x.save
end
结束
end
结束
You should first follow the convention of Rails RESTful routing and use the index
endpoint as a way to list all "searchs." 您应该首先遵循Rails RESTful路由的约定,并使用
index
端点作为列出所有“搜索”的方式。 I don't get why you are doing UsersController#create
stuff in SearchsController#index
. 我不明白为什么要在
SearchsController#index
中执行UsersController#create
东西。
Anyway, once you straighten that out, let's say you have some controller endpoint called SearchsController#custom
(you shouldn't call it that, but I cant think of anything better) that you map to searchs_custom_path
. 无论如何,一旦解决了这个问题,就可以说您有一个映射到
searchs_custom_path
控制器终结点,称为SearchsController#custom
(您不应该这样称呼,但我想不到更好的方法了)。
Then do something like this in your template: 然后在您的模板中执行以下操作:
<%= hidden_field_tag "searchsCustomPath", searchs_custom_path %>
Then in your JavaScript do this: 然后在您的JavaScript中执行以下操作:
$( "#h" ).click(function() {
$.ajax({
type: "GET",
url: $("#searchsCustomPath").val(),
data: {xx: 'q'}
});
});
This way you don't hardcode the URL and violate DRY while still making the right Rails-generated URL available to JQuery. 这样,您就不会对URL进行硬编码并违反DRY,同时仍使正确的Rails生成的URL可用于JQuery。
Replace this 取代这个
var url = 'searchs/index'; var url ='搜索/索引';
With this 有了这个
var url = '<%= searchs_path %>'; var url ='<%= searchs_path%>';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.