我如何模拟Rails和GWT的PUT / DELETE? [英] How can I emulate PUT/DELETE for Rails and GWT?

查看:143
本文介绍了我如何模拟Rails和GWT的PUT / DELETE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的应用程序符合REST标准。我在后端使用Rails,在前端使用 GWT 。我想做更新和删除。我意识到我可以做一些像mydomain.com/:id/delete(GET)并完成同样的事情。不过,正如我之前所说的,我希望有一个符合REST的后端。因此,我想要做mydomain.com/:id(DELETE)并隐式调用我的delete方法。



现在,我的理解是,如果浏览器浏览器是GWT的RequestBuilder)不支持DELETE / GET,Rails以某种方式用POST和其他一些url参数完成这个任务。那么,我该如何用GWT RequestBuilder完成这项工作?

Rails用隐藏属性来做到这一点。最简单的方法是创建一个新的rails应用程序,生成一个脚手架并查看浏览器中的HTML。



试试这个:

p>

  rails jp 
cd jp
./script/generate scaffold RequestBuilder名称:string
rake db :迁移
./script/server

然后导航到 http:// localhost:3000 / request_builders ,点击New并查看HTML。您会看到如下所示的内容:

 < form action =/ request_buildersclass =new_request_builder
id =new_request_buildermethod =post>
< div style =margin:0; padding:0>
< input name =authenticity_tokentype =hiddenvalue =e76 .../>
< / div>

这是一个创建,方法是POST。输入名称,保存然后编辑:

 < form action =/ request_builders / 1class =edit_request_builder
id =edit_request_builder_1method =post>
< div style =margin:0; padding:0>
< input name =_ methodtype =hiddenvalue =put/>
< input name =authenticity_tokentype =hiddenvalue =e76 .../>
< / div>

当然,表单是通过POST发送的,但Rails有一个隐藏字段来模拟PUT请求。

  var m = document.createElement('input' ); 
m.setAttribute('type','hidden');
m.setAttribute('name','_method');
m.setAttribute('value','delete');

要使用另一个前端工作,您必须同时使用这两个工具:




  • 使用相同样式的URL,例如/ request_builders / 1(RESTful URL)

  • 包含隐藏字段(Rails技巧)


I would like to make my application somewhat REST compliant. I am using Rails on the backend and GWT on the frontend. I would like to do updates and deletes. I realize I can do something like mydomain.com/:id/delete (GET) and accomplish the same thing. However, as I stated previously, I would like to have a REST compliant backend. Thus, I want to do mydomain.com/:id (DELETE) and have it implicitly call my delete method.

Now, it's my understanding that if a browser (my browser is GWT RequestBuilder) doesn't support DELETE/GET, Rails somehow accomplishes this task with a POST and some other url parameter. So, how can I accomplish this with a GWT RequestBuilder?

解决方案

Rails does this with hidden attributes. The easiest way to figure this out would be to create a new rails application, generate a scaffold and have a look at the HTML in a browser.

Try this:

rails jp
cd jp
./script/generate scaffold RequestBuilder name:string
rake db:migrate
./script/server 

Then navigate to http://localhost:3000/request_builders, click on New and have a look at the HTML. You'll see something like:

<form action="/request_builders" class="new_request_builder" 
  id="new_request_builder" method="post">
  <div style="margin:0;padding:0">
    <input name="authenticity_token" type="hidden" value="e76..." />
  </div>

This is a creation, method is POST. Enter a name, save then Edit:

<form action="/request_builders/1" class="edit_request_builder" 
  id="edit_request_builder_1" method="post">
  <div style="margin:0;padding:0">
    <input name="_method" type="hidden" value="put" />
    <input name="authenticity_token" type="hidden" value="e76..." />
  </div>

Of course the form is sent with POST, but Rails hads a hidden field to simulate a PUT request. Same for deletion, but the scaffold will do it with a bit of Javascript:

var m = document.createElement('input'); 
m.setAttribute('type', 'hidden'); 
m.setAttribute('name', '_method'); 
m.setAttribute('value', 'delete');

To have this work with another front-end, you'll have to both:

  • Use the same style URL such as /request_builders/1 (RESTful URLs)
  • Include the hidden fields (Rails trick)

这篇关于我如何模拟Rails和GWT的PUT / DELETE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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