使用ajax的动态内容(ruby on rails)

时间:2022-04-04 02:16:51

I am using Rails and jquery, and I need a dynamic content with ajax.

我正在使用Rails和jquery,我需要使用ajax的动态内容。

But I don't know how to get the current user ID

但是我不知道如何获得当前的用户ID

For example the url is www.mywebsite.com/users/20

例如,url是www.mywebsite.com/users/20

In my javascript file I need the user ID (20)

在javascript文件中,我需要用户ID (20)

$.get("/users/<%= **20** %>.json", function(data)
      {

      }, "json");

Thanks in advance for any help.

谢谢你的帮助。


Is there another way to do this ?

还有别的办法吗?

5 个解决方案

#1


4  

You can assign the rails variable value to a javascript variable in your view file like

您可以将rails变量值分配给视图文件中的一个javascript变量

"<%= javascript_tag "var userId = #{@current_user.id};" %>"

"<%= javascript_tag "var userId = #{@current_user.id};"% > "

and access the variable inside js file wherever you want.

访问js文件中的变量。

#2


5  

Generally i put a hidden field with the id somewhere in the page, where i can easily access with $("#user_id") on the javascript file, then the request would be something like this:

通常,我在页面的某个地方放置一个id为隐藏字段,在那里我可以用$(“#user_id”)轻松访问javascript文件,然后请求会是这样的:

var id = $("#user_id").val();
$.get("/users/"+id+".json", function(data) { }, "json");

#3


2  

$.get("/users/<%= params[:id] %>.json", function(data)
  {

  }, "json");

Should do the trick.

应该足够了。

Include the snippet into the .erb file or create a .js.erb partial with this.

将代码片段包含到.erb文件中,或创建.js。erb部分。

#4


1  

Lots of choices:

大量的选择:

  1. If you have a current user object with the id (Note: this must be in a file ending with .js.erb):

    如果当前用户对象具有id(注意:该对象必须位于以.js.erb结尾的文件中):

    $.get("/users/<%= @current_user.id %>.json", function(data){...}, "json");

    美元。get(" /用户/ < % = @current_user。id % >。json”功能(数据){…}," json ");

  2. If you don't want to use a .js.erb file, but still need the value to be set somewhere on the page, then set a data-attribute or hidden input with the id value.

    如果你不想用。js的话。erb文件,但是仍然需要在页面的某个地方设置值,然后使用id值设置数据属性或隐藏输入。

  3. If you have the id value in the url (as in the current url you are on looks like www.mywebsite.com/users/20 and you want the GET request to use 20 as the value), you can use document.url or window.location.href and some string manipulation to get the id.

    如果url中有id值(如当前url中所示,类似于www.mywebsite.com/users/20,并希望GET请求使用20作为值),则可以使用document。url或window.location。href和一些字符串操作来获取id。

#5


0  

Try <%= escape_javascript(params[:id]) %> I'm not completely sure if escape_javascript is necessary or will help.

尝试<%= escape_javascript(params[:id]) %>我不太确定是否需要或会有帮助。

#1


4  

You can assign the rails variable value to a javascript variable in your view file like

您可以将rails变量值分配给视图文件中的一个javascript变量

"<%= javascript_tag "var userId = #{@current_user.id};" %>"

"<%= javascript_tag "var userId = #{@current_user.id};"% > "

and access the variable inside js file wherever you want.

访问js文件中的变量。

#2


5  

Generally i put a hidden field with the id somewhere in the page, where i can easily access with $("#user_id") on the javascript file, then the request would be something like this:

通常,我在页面的某个地方放置一个id为隐藏字段,在那里我可以用$(“#user_id”)轻松访问javascript文件,然后请求会是这样的:

var id = $("#user_id").val();
$.get("/users/"+id+".json", function(data) { }, "json");

#3


2  

$.get("/users/<%= params[:id] %>.json", function(data)
  {

  }, "json");

Should do the trick.

应该足够了。

Include the snippet into the .erb file or create a .js.erb partial with this.

将代码片段包含到.erb文件中,或创建.js。erb部分。

#4


1  

Lots of choices:

大量的选择:

  1. If you have a current user object with the id (Note: this must be in a file ending with .js.erb):

    如果当前用户对象具有id(注意:该对象必须位于以.js.erb结尾的文件中):

    $.get("/users/<%= @current_user.id %>.json", function(data){...}, "json");

    美元。get(" /用户/ < % = @current_user。id % >。json”功能(数据){…}," json ");

  2. If you don't want to use a .js.erb file, but still need the value to be set somewhere on the page, then set a data-attribute or hidden input with the id value.

    如果你不想用。js的话。erb文件,但是仍然需要在页面的某个地方设置值,然后使用id值设置数据属性或隐藏输入。

  3. If you have the id value in the url (as in the current url you are on looks like www.mywebsite.com/users/20 and you want the GET request to use 20 as the value), you can use document.url or window.location.href and some string manipulation to get the id.

    如果url中有id值(如当前url中所示,类似于www.mywebsite.com/users/20,并希望GET请求使用20作为值),则可以使用document。url或window.location。href和一些字符串操作来获取id。

#5


0  

Try <%= escape_javascript(params[:id]) %> I'm not completely sure if escape_javascript is necessary or will help.

尝试<%= escape_javascript(params[:id]) %>我不太确定是否需要或会有帮助。