Rails - 从js.erb文件更改数据库中的布尔值

时间:2022-11-07 08:42:40

In my scenario in rails application after sharing content to facebook i will get a response object with post id

在我的场景中,在向facebook分享内容之后我将获得一个带有帖子ID的响应对象

post_id: "mypostidhere"

If its not successful i will get response object with an error message.

如果它不成功,我将获得带有错误消息的响应对象。

So according to the response i want to change a boolean column of last row for the current user in my database table. And for this in my .js.erb file i tried to access current_user.model.last but it throws the following error.

因此,根据响应,我想更改数据库表中当前用户的最后一行的布尔列。为此,在我的.js.erb文件中,我试图访问current_user.model.last,但它会引发以下错误。

undefined local variable or method `current_user' for 

So how can i change the boolean column in database according to the response?

那么如何根据响应更改数据库中的布尔列?

UPDATED

更新

Ajax code

Ajax代码

 $.ajax({
        type: "POST",
        url: "/action"
    });

Controller code

控制器代码

def action
  current_user.modal.last.toggle!(:boolean-column-name)
end

It changes the table column successfully. But after that i am receiving an error in browser CONSOLE as below

它成功更改了表列。但之后我在浏览器CONSOLE中收到错误,如下所示

POST http://URL/action
500 (Internal Server Error)

I am new to AJAX. What i am doing wrong in ajax request? Thanks in advance.

我是AJAX的新手。我在ajax请求中做错了什么?提前致谢。

2 个解决方案

#1


0  

current_user is not a local variable to your JS function/file, it is a helper provided and available in your rails application.

current_user不是JS函数/文件的局部变量,它是rails应用程序中提供并可用的助手。

So, what I will like to ask is:

所以,我想问的是:

How are you using current_user in the JS file?

你是如何在JS文件中使用current_user的?

What you can do is to make an ajax call to a controller method, and then access current_user directly from the controller.

您可以做的是对控制器方法进行ajax调用,然后直接从控制器访问current_user。

#2


0  

If you want to change the db, you're going to have to communicate with your Rails app, which is done with ajax:

如果你想更改数据库,你将不得不与你的Rails应用程序进行通信,这是通过ajax完成的:

#app/assets/javascripts/application.js
... // your events will fire
$.ajax({
   url: "/users/update",
   method: "PUT",
   data: { param: "value" }
   success: function(data) {
      // do something here
   };
});

This will allow you to do the following:

这将允许您执行以下操作:

#config/routes.rb
resources :users do
   put :update, on: :collection
end

#app/controllers/users_controller.rb
class UsersController < ApplicationController
   def update
      current_user.update(update_params)
   end

   private

   def update_params
       params.permit(:param)
   end
end 

#1


0  

current_user is not a local variable to your JS function/file, it is a helper provided and available in your rails application.

current_user不是JS函数/文件的局部变量,它是rails应用程序中提供并可用的助手。

So, what I will like to ask is:

所以,我想问的是:

How are you using current_user in the JS file?

你是如何在JS文件中使用current_user的?

What you can do is to make an ajax call to a controller method, and then access current_user directly from the controller.

您可以做的是对控制器方法进行ajax调用,然后直接从控制器访问current_user。

#2


0  

If you want to change the db, you're going to have to communicate with your Rails app, which is done with ajax:

如果你想更改数据库,你将不得不与你的Rails应用程序进行通信,这是通过ajax完成的:

#app/assets/javascripts/application.js
... // your events will fire
$.ajax({
   url: "/users/update",
   method: "PUT",
   data: { param: "value" }
   success: function(data) {
      // do something here
   };
});

This will allow you to do the following:

这将允许您执行以下操作:

#config/routes.rb
resources :users do
   put :update, on: :collection
end

#app/controllers/users_controller.rb
class UsersController < ApplicationController
   def update
      current_user.update(update_params)
   end

   private

   def update_params
       params.permit(:param)
   end
end