Rails:应该如何加载动态内容?通过某种模板?
我正在构建一个调查系统,用户可以在其中选择各种类型的问题。每个问题都应加载到当前页面。
有 6 种不同的问题类型,每种类型都有不同的设置/表单字段。您还可以添加多个相同类型的问题。
那么我应该存储表单模板然后动态加载它们(我使用的是 jQuery)吗?
如果是这样,它们应该存储在哪里以及应该如何调用它们(它们中有 erb)?
编辑:为了澄清,我知道如何做 javascript 的事情。从 Rails 的角度来看,我很好奇它是如何工作的。我会为每种问题类型创建一堆部分内容吗?我该如何加载部分内容?
I'm building a survey system where a user can pick various types of questions. Each question should be loaded into the current page.
There are 6 different question types, each with different settings/form fields. You can also add multiples of the same type of question.
So should I have the form templates stored and then load them dynamically (I'm using jQuery)?
And if so, where should they be stored and how should they be called (they'd have erb in them)?
EDIT: For clarification, I know how to do the javascript stuff. I'm curious from the Rails side of things how this works. Would I create a bunch of partials for each question type? And how would I load just the partial content?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议: http://railscasts.com/episodes/197-nested- model-form-part-2
在链接中,评论 57 声称这里的方式不显眼:http ://github.com/thb/surveysays
但我会避免使用 ajax 来实现此类功能:如果可以在客户端完成,就在客户端完成:)
I suggest: http://railscasts.com/episodes/197-nested-model-form-part-2
And in the link, comment 57 claims to have it unobstrusive way here: http://github.com/thb/surveysays
But I'd avoid using ajax for such kind of feature: if it can be done client side, do it client side :)
您可以向服务器发出 AJAX 请求,并让 jQuery 用从服务器发回的新 HTML 内容完全替换特定内容(例如
)中的内容。
查看jQuery.load的文档:http://api.jquery.com /load
从 Rails 方面来看,是的,有通过 AJAX 发回的部分表单,但也可以包含在未启用 JavaScript 的“正常”HTML 请求中。
You can make an AJAX request to the server and have jQuery completely replace the content in a certain, say,
<div>
with new HTML content sent back from the server.Take a look at the documentation for jQuery.load: http://api.jquery.com/load
From the Rails side, yes, have the forms in partials that are sent back via AJAX, but can also be included in 'normal' HTML requests for folks that do not have JavaScript enabled.