如何使用ajax将数据发送到ruby on rails中的控制器 [英] How to use ajax send data to controller in ruby on rails

查看:39
本文介绍了如何使用ajax将数据发送到ruby on rails中的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 ROR 的新开发人员.我使用带有事件更改的 ajax 将数据从 show.html.erb 中的 select_tag 发送到 def show ... end 中的控制器.我尝试发送数据,但我的脚本不起作用.下面是我的脚本:

在 product_details/show.html.erb 中

<%= form_tag(product_detail_path, :method => "post") do %><%= select_tag "categories", options_from_collection_for_select(Category.all, :id, :category_name),:id =>'cbo'%><%结束%>高清秀@getcbo = params[:cbo_id]结尾

我认为 url: "/product_details/show" 可能是错误的.那么我如何使用 url 将数据发送到 def show ... 在控制器中结束?.请帮我 !

解决方案

很多事情需要调整

<小时>

MVC

首先,让我解释一下 ajax 调用 应该如何在 Rails 中工作

因为 Rails 基于 ,但如果你没有太多,让我解释一下.

Ajax 基本上只是一种在浏览器中创建伪请求"的方法.通过伪请求",我的意思是它本质上会从正常"应用程序的典型流程中发送另一个请求:

因为 HTTP 是无状态的"(即它不保留任何设置/状态"),有如果没有网络套接字之类的东西,就无法提供实时"连接.

Ajax 为您提供发送 &接收数据在您的典型加载过程完成后,使您看起来实时"接收数据.实际上,您只是向您的应用发送了一个伪请求(永远记住 Ajax 不是魔术)

<小时>

代码

您的代码需要在几个地方更改

--

首先,您需要让您的 javascript 不引人注目(即从您的视图中删除它并放入您的资产管道中)

#app/assets/javascripts/application.js$(document).on("change", "#cbo", function() {$.ajax({url: "/product_details/show",类型:POST",数据:{cbo_id":$(this).val()},数据类型:json",成功:功能(数据){alert('成功');}});});#app/controllers/product_details_controller.rb类 ProductDetailsController <应用控制器response_to :js, :json, :html高清秀@product = Product.find params[:id]response_with @product结尾结尾

这将使您能够接收您在控制器中设置的产品 JSON 对象,这是一个非常基本的示例.

如果你给我一些关于你想要实现的目标的更多信息,我当然会给你一个更精致的解决方案;希望上面的代码演示了MVC原理是如何工作的等

i am new developer in ROR.i use ajax with event change to send data from select_tag in show.html.erb into controller in def show ... end . i try to send data but my script not work.below is my script :

<script type="text/javascript">
    $('#cbo').change(function() {
       $.ajax({
        url: "/product_details/show",
        type: "POST",
        data: {"cbo_id" : $('#cbo').val()},
        dataType: "html",
        success: function(data) {
          alert('successfully');
        }
      });
    });
</script>

in product_details/show.html.erb

<%= form_tag(product_detail_path, :method => "post") do %>
  <%= select_tag "categories", options_from_collection_for_select(Category.all, :id,     :category_name),:id => 'cbo' %>
<% end %>

def show
  @getcbo = params[:cbo_id]
end

I think maybe wrong about url: "/product_details/show" .So how can i use url for send data to def show ... end in controller? .please help me !

解决方案

Lots of things you need to adjust


MVC

Firstly, let me explain how ajax calls should work in Rails

Because Rails is based around the MVC (model view controller) programming pattern - you need to build your application around this. The problem for many newbie developers is that it's a little tricky to get your head around this - something which can be learnt relatively simply:

As you can see, MVC basically puts the "controller" at the center of the party. You must appreciate that in an MVC app, your requests don't go to the "view" first (as many people think) - they go to the controller, which builds the relevant data from the model, then rendering the view for you

So whenever you send a request to your MVC application, you need to remember to base it around your controller actions (not your views)

--

Ajax

I don't know how much experience you have with ajax (Asynchronous Javascript and XML), but in case you don't have much, let me explain.

Ajax is basically just a way to create "pseudo requests" in your browser. By "pseudo request", I mean that it will essentially send another request out of the typical flow of your "normal" application:

Because HTTP is "stateless" (IE it doesn't persist any settings / "state"), there are no ways to provide "real time" connectivity without something like web sockets.

Ajax provides you with the ability to send & receive data after your typical load process has completed, making it appear that you're receiving data in "real time". In reality, you're just sending a pseudo request to your app (always remember that Ajax isn't magic)


Code

Your code needs to change in several places

--

Firstly, you need to make your javascript unobtrusive (IE remove it from your views & place into your asset piepline)

#app/assets/javascripts/application.js
$(document).on("change", "#cbo", function() {
   $.ajax({
   url: "/product_details/show",
   type: "POST",
   data: {"cbo_id" : $(this).val()},
   dataType: "json",
   success: function(data) {
       alert('successfully');
     }
   });
});

#app/controllers/product_details_controller.rb
Class ProductDetailsController < ApplicationController
   respond_to :js, :json, :html

   def show
      @product = Product.find params[:id]
      respond_with @product
   end
end

This will give you the ability to receive back the product JSON object that you've set in the controller, a very basic example.

If you give me some more information about what you'd like to achieve, I'll certainly give you a more refined solution; hopefully the above code demonstrates how the MVC principle works etc

这篇关于如何使用ajax将数据发送到ruby on rails中的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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