简体   繁体   English

Rails:单击后如何让 Jquery 更新数据库列

[英]Rails: How to get Jquery to update a database column after a click

To start out, I know what I am trying to do is not typical to Rails.首先,我知道我正在尝试做的事情对于 Rails 来说并不典型。 I open to suggestions to better alternatives.我愿意接受有关更好选择的建议。 I am novice with rails so suggestions are welcome:)我是rails新手,所以欢迎提出建议:)

Basically, I have a notifications bar that has 5 notifications.基本上,我有一个有 5 个通知的通知栏。 After a user clicks and sees the notifications I want to set the a column in the database called seen to true.在用户单击并看到通知后,我想将数据库中名为 see 的列设置为 true。

Here is my Jquery function:这是我的 Jquery function:

<script type="text/javascript">
            $(document).ready(function(){
              $("button").click(function(){
                    $("div#count").html("<%= escape_javascript(render('shared/notification_count')) %>");
              });
            });
</script>

And here is the code I am trying to execute only after the click(located in _notification_count.html.erb)这是我仅在单击后才尝试执行的代码(位于_notification_count.html.erb)

<% notification_ids =   current_user.notifications.map(&:id).join(', ')%>
<% sql = "UPDATE notifications SET SEEN = true where id IN(#{notification_ids})" %>
<% User.connection.select_all(sql) %>

However, it appears that this code is getting executed automatically when the page loads and not after the click.但是,此代码似乎在页面加载时自动执行,而不是在单击后自动执行。 What is my problem?我的问题是什么?

Firstly, you cannot simply attach a render to your button to make changes to your database (whether it's a read or a write).首先,您不能简单地将render附加到您的按钮来更改您的数据库(无论是读取还是写入)。 You must make an AJAX call back to your controller using something like您必须使用类似于

$.ajax({
    url: "/path/to/your/method",
    type: "GET",
    dataType: "script"
});

And in your controller you will handle what you need to do, and then render your js file with a在您的 controller 中,您将处理您需要做的事情,然后使用

respond_to do |format|
    format.js {render 'shared/notification_count'}
end

This is because when a user clicks on your button, the code will be executed on the clients side, but if you never make a request back to the server, then you will not be able to see the updated version of your database.这是因为当用户单击您的按钮时,代码将在客户端执行,但如果您从未向服务器发出请求,那么您将无法看到数据库的更新版本。 I suggest you reading more on this , that helped me a lot.我建议您阅读更多关于的内容,这对我有很大帮助。

The contents of your.erb files are simply templates: the are evaluated at load time. your.erb 文件的内容只是模板:在加载时评估。 If you want the user interaction to trigger some behavior, you have to do it purely in javascript.如果您希望用户交互触发某些行为,则必须纯粹在 javascript 中进行。 As a result, the way to have the database interactions occur are in this chain:因此,发生数据库交互的方式在此链中:

  1. user does something用户做某事
  2. javascript talks to the controller (via either ajax or by submitting a form) javascript 与 controller 对话(通过 ajax 或提交表格)
  3. The controller gets the model in question and asks it to do something controller 得到有问题的 model 并要求它做某事
  4. the model interacts with the database. model 与数据库交互。

So basically in your model you need to have code to fire those notifactions.所以基本上在你的 model 中你需要有代码来触发这些通知。 The controller will ask the model to do so only when it receives a request from the user/view to do so.只有在收到来自用户/视图的请求时,controller 才会要求 model 这样做。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM