简体   繁体   English

如何获取jQuery列表以将数据持久保存到Rails中的数据库中?

[英]How do I get jQuery lists to persist data to the database in rails?

I want to create a series of questions with jQuery, maybe jQuery mobile. 我想用jQuery(也许是jQuery mobile)创建一系列问题。 For each question I want it to have two answers. 对于每个问题,我希望它有两个答案。 I want the list of questions to appear on the same page. 我希望问题列表显示在同一页面上。

Maybe something like this... 也许是这样的...

(Question here) (这里的问题)

<div class="ui-field-contain">
    <fieldset data-role="controlgroup" data-type="horizontal" data-mini="true">
        <legend>Yes or No</legend>
        <input type="radio" name="radio-orientation" id="isVertical" value="isVertical" checked="checked">
        <label for="isVertical">Yes</label>
        <input type="radio" name="radio-orientation" id="isHorizontal" value="isHorizontal">
        <label for="isHorizontal">No</label>
    </fieldset>
</div>

I can't seem to find any information on linking the button to the user. 我似乎找不到有关将按钮链接到用户的任何信息。 So when the user picks one of the buttons, I want to store their choice into the "choice" attribute of their quiz. 因此,当用户选择按钮之一时,我想将其选择存储到测验的“选择”属性中。

I don't want anyone to write code for me I just need a general idea on how to get a jQuery button click to put the user's choice in the database. 我不想让任何人为我编写代码,我只需要一个有关如何获取jQuery按钮单击以将用户的选择放入数据库的一般思路。 For each question. 对于每个问题。

You can tackle the above problem using many different ways but, I will list an approach down below. 您可以使用许多不同的方法来解决上述问题,但是,我将在下面列出一种方法。

You can create 2 models. 您可以创建2个模型。

  1. model to store the tempdata 用于存储临时数据的模型
  2. model to store the actual data after the form is submitted. 模型以在提交表单后存储实际数据。

When the form is submitted you should delete the record from the tempdata model. 提交表单后,您应该从tempdata模型中删除记录。

As the user is filling out the form you can utilize the jquery autosave plugin to send data to the tempdata model. 当用户填写表单时,您可以利用jquery autosave插件将数据发送到tempdata模型。

Using the above approach if the user is filling out a long form or a series of questions they will have the ability to resume where they left off if something were to happen(lost internet connection, etc). 如果用户要填写较长的表格或一系列问题,则使用上述方法,他们将能够在发生某些事情(丢失的互联网连接等)时,从中断的地方继续。

As for linking a user to a specific question... You will need to think about how you are going to design your models. 至于将用户链接到特定问题...您将需要考虑如何设计模型。 For example I would have a user model , question model and a answers model . 例如,我将有一个user modelquestion modelanswers model In your case the question model would have a user_id attribute (foreign key) which allows you to link a specific question to a specific user. 在您的情况下,问题模型将具有user_id属性(外键),该属性使您可以将特定问题链接到特定用户。 Check out this nested forms tutorial from railscast for a better explanation. 查看来自railscast的此嵌套表单教程 ,以获得更好的解释。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据保存在本地html文件模因奖网页中,以使计数器永久保存? - How do I persist data in a local html file meme award webpage to allow the counter to persist permanently? 卸载应用程序后如何将数据保存在数据库中? - how to persist data in database after uninstalling application? 如何从 web 中的 firebase 实时数据库获取数据? - How do I get data from firebase realtime database in web? 如何从 Javascript 中的 SQLite 数据库中获取表数据? - How do i get table data from SQLite Database in Javascript? 如何使用 firebase/nuxtjs 获取数据库数据 - How do I get database data using firebase/nuxtjs 如何在数据库 mysql 中保存 jquery 可排序连接列表? - How Can i save jquery sortable connected lists in database mysql? 如何在使用Ruby on Rails在数据库中搜索值的ajax表单上添加jquery验证? - How do I add jquery validation on an ajax form that searches for the value in the database using Ruby on rails? 我如何使用JQuery从不同的表中获取数据 - How do i get data from different tables using JQuery jQuery.ajax:如何获取易于发送的数据? - jQuery.ajax: How do I get the data to send easily? 如何获取存储在JQuery中的value中的数据? - How do I get the data stored in value in JQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM