使用 AJAX 的 Rails 资源
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以我相信这有两个方面:javascript 和控制器的变化。
在你的控制器中,你需要确保它可以返回 json 输出(或 xml 或任何你选择的 ajax-y 输出):
有很多方法可以生成 json 输出,但基本上你必须确保它的形状易于使用在视图 javascript 上。
我使用 jQuery,下面是执行 ajax 调用的代码:
您可以根据需要将 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):
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:
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.
Rails 3 可以通过告诉表单您希望它
在控制器中
“远程”(ajax)来提供帮助然后,您可以
/app/views/mans/update.js.erb
注意:无论我在上面说“mans”,它都可能是“men”
Rails 3 can help by telling the form that you want it to be "remote" (ajax)
in your Controllers
Then, you can have
/app/views/mans/update.js.erb
Note: Wherever I say "mans" above, it might be "men"