使用 AJAX 的 Rails 资源

发布于 2024-12-18 16:28:10 字数 322 浏览 0 评论 0原文

在 Rails 中,需要进行哪种 AJAX 调用才能创建和修改资源。假设我有这样的资源,

Man(age: integer, country_from:string, residence:string)

那么现在如何通过 AJAX 调用来实现这一点(例如将帖子发送到我的创建函数,如何发送参数,如何设置我的控制器)。详细点,我的AJAX非常非常非常弱。 (现在我已经让我的男人像rails生成脚手架男人年龄:int country_from:string ...)

PS

我使用rails 3

In rails, what kind of AJAX call would need to be made in order to create and modify resources. Say if i had a resource like this

Man(age: integer, country_from:string, residence:string)

Now how would this be made through an AJAX call (like sending post to my create function, how would parameters be sent in, how would my controllers be setup). Be detailed, my AJAX is very, very, very weak. (right now i have my Man made like rails generate scaffold Man age:int country_from:string ...)

PS

Im using rails 3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素罗衫 2024-12-25 16:28:10

所以我相信这有两个方面:javascript 和控制器的变化。

在你的控制器中,你需要确保它可以返回 json 输出(或 xml 或任何你选择的 ajax-y 输出):

def man
  # do your work
  return_data = {}
  # initialize your return data
  respond_to do |format|
    render :json => return_data.to_json, :layout => nil
  end
end

有很多方法可以生成 json 输出,但基本上你必须确保它的形状易于使用在视图 javascript 上。

我使用 jQuery,下面是执行 ajax 调用的代码:

function foo(some_param) {
  $.ajax({
    type: 'GET',
    url: "/<controller>/man?FOO=" + some_params,
    dataType: 'json',
    success: handle_success,
    error: handle_errors
 }

function handle_success(data) {
  # process return JSON. it's a javascript object corresponding to the shape
  # of your JSON. If your json was a hash server side, it will be an 'object', etc
}

function handle_error(data) {
  # handle error cases based upon failure in the infrastructure, not 
  # failure cases that you encounter due to input, etc.
}

您可以根据需要将 foo 函数绑定到某个按钮或 onclick。

我不确定这是否足够完整。如果您需要更多详细信息等,请告诉我。

So I believe there are two sides to this: the javascript and the controller changes.

In your controller you need to ensure it can return json output (or xml or whatever your chosen ajax-y output is):

def man
  # do your work
  return_data = {}
  # initialize your return data
  respond_to do |format|
    render :json => return_data.to_json, :layout => nil
  end
end

There are many ways to generate your json output but basically you have to make sure it's in a shape that is easily consumed on the view javascript.

I use jQuery and here's the code to execute an ajax call:

function foo(some_param) {
  $.ajax({
    type: 'GET',
    url: "/<controller>/man?FOO=" + some_params,
    dataType: 'json',
    success: handle_success,
    error: handle_errors
 }

function handle_success(data) {
  # process return JSON. it's a javascript object corresponding to the shape
  # of your JSON. If your json was a hash server side, it will be an 'object', etc
}

function handle_error(data) {
  # handle error cases based upon failure in the infrastructure, not 
  # failure cases that you encounter due to input, etc.
}

You can tie the foo function to some button or onclick as you desire.

I am not sure this is complete enough. Let me know if you need more detail, etc.

笑着哭最痛 2024-12-25 16:28:10

Rails 3 可以通过告诉表单您希望它

<%= form_for @man, :remote=>true do |f| %>
  <div class="field">
  <%= f.label :man %>
  <%= f.text_field :man %>
  </div>
  <%= f.submit "Save", :disable_with=>"Saving..."%>
<% end %>

在控制器中

class MansController < ApplicationController
  respond_to :js, :html

  def update
    @man = Man.find(params[:id])
    @man.update_attributes(params[:man])
    respond_with @man
  end
end

“远程”(ajax)来提供帮助然后,您可以
/app/views/mans/update.js.erb

<% if @man.errors.any? %>
  alert("This is javascript code that either renders the form in place of the other form");
<% else %>
  alert("success!")
<% end %>

注意:无论我在上面说“mans”,它都可能是“men”

Rails 3 can help by telling the form that you want it to be "remote" (ajax)

<%= form_for @man, :remote=>true do |f| %>
  <div class="field">
  <%= f.label :man %>
  <%= f.text_field :man %>
  </div>
  <%= f.submit "Save", :disable_with=>"Saving..."%>
<% end %>

in your Controllers

class MansController < ApplicationController
  respond_to :js, :html

  def update
    @man = Man.find(params[:id])
    @man.update_attributes(params[:man])
    respond_with @man
  end
end

Then, you can have
/app/views/mans/update.js.erb

<% if @man.errors.any? %>
  alert("This is javascript code that either renders the form in place of the other form");
<% else %>
  alert("success!")
<% end %>

Note: Wherever I say "mans" above, it might be "men"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文