使用rails 4如何通过ajax控制器方法发送数据并返回对象ID

时间:2022-12-01 16:12:30

I feel like this should be simple but I am new to ajax and using ajax in rails.

我觉得这应该很简单,但我是ajax的新手,并在rails中使用ajax。

If the a user follows a link to my site, that link will contain a token. On page load, I am grabbing that token.

如果用户关注指向我网站的链接,则该链接将包含令牌。在页面加载时,我抓住那个令牌。

I need to send the token to a controller method (I think), lookup a user via the token, and send that user_id back to the ajax call.

我需要将令牌发送到控制器方法(我认为),通过令牌查找用户,并将该user_id发送回ajax调用。

I have the token in a js var. And I have a controller method but am stuck there.

我在js var中有令牌。我有一个控制器方法,但我被困在那里。

many thanks for the help.

非常感谢您的帮助。

1 个解决方案

#1


0  

Because you don't have the User ID beforehand, it's harder to have a RESTful structure in the Controller, though you could have a from_token action in your UsersController:

因为您事先没有用户ID,所以在Controller中使用RESTful结构会更困难,尽管您可以在UsersController中使用from_token操作:

class UsersController < ApplicationController
  #...

  def from_token
    user = User.find_by_token(params[:token])

    respond_to do |format|
      format.json { render json: {user: {id: user.id}} }
    end
  end
end

And in routes.rb:

并在routes.rb中:

#...

resources :users do
  get 'from_token' => 'users#from_token'
end

Depending on what data you want for the JavaScript, it may be easier to return all the user data, rather than just the ID. In which case have a look at jBuilder for building more complex JSON objects.

根据您想要的JavaScript数据,可能更容易返回所有用户数据,而不仅仅是ID。在这种情况下,请查看jBuilder以构建更复杂的JSON对象。

In terms of the JavaScript, it depends on what framework you're using to make the ajax request in the browser. If it's jQuery (the Rails default) use the ajax get request API.

就JavaScript而言,它取决于您在浏览器中使用什么框架来生成ajax请求。如果是jQuery(Rails默认),请使用ajax get request API。

var token = '{{extracted token}}';
$.get('/users/from_token.json?token=' + token, function(data) {
  var userId = data.user.id;
});

#1


0  

Because you don't have the User ID beforehand, it's harder to have a RESTful structure in the Controller, though you could have a from_token action in your UsersController:

因为您事先没有用户ID,所以在Controller中使用RESTful结构会更困难,尽管您可以在UsersController中使用from_token操作:

class UsersController < ApplicationController
  #...

  def from_token
    user = User.find_by_token(params[:token])

    respond_to do |format|
      format.json { render json: {user: {id: user.id}} }
    end
  end
end

And in routes.rb:

并在routes.rb中:

#...

resources :users do
  get 'from_token' => 'users#from_token'
end

Depending on what data you want for the JavaScript, it may be easier to return all the user data, rather than just the ID. In which case have a look at jBuilder for building more complex JSON objects.

根据您想要的JavaScript数据,可能更容易返回所有用户数据,而不仅仅是ID。在这种情况下,请查看jBuilder以构建更复杂的JSON对象。

In terms of the JavaScript, it depends on what framework you're using to make the ajax request in the browser. If it's jQuery (the Rails default) use the ajax get request API.

就JavaScript而言,它取决于您在浏览器中使用什么框架来生成ajax请求。如果是jQuery(Rails默认),请使用ajax get request API。

var token = '{{extracted token}}';
$.get('/users/from_token.json?token=' + token, function(data) {
  var userId = data.user.id;
});