I have a page which shows a highchar. I'd like to use Javascript to fetch several pieces of information for a specific user. I want to fetch from my database and place it on the highcharts graoh. I have set up a JSfiddle which shows static data. But
我有一个显示highchar的页面。我想使用Javascript来获取特定用户的几条信息。我想从我的数据库中取出并将其放在highcharts graoh上。我已经设置了一个显示静态数据的JSfiddle。但
What I am trying to do is:
我想做的是:
- Make javascript call a Rails action with parameters.
- Query the rails database(projectHours table).
- Rails returns the response.
- Javascript updates highcharts and maps the information stored in the projectHours table.
使用参数使javascript调用Rails操作。
查询rails数据库(projectHours表)。
Rails返回响应。
Javascript更新highcharts并映射存储在projectHours表中的信息。
So my question is What if I want to use information from my db?
所以我的问题是如果我想使用我的数据库中的信息怎么办?
Two of the following models
以下两种型号
Effort.rb
class Effort < ActiveRecord::Base
belongs_to :project_task
belongs_to :user
end
Users.rb
class User < ActiveRecord::Base
has_many :projects
has_many :efforts
A further note, I think that in my efforts controller I may need to add some form of render action
另外请注意,我认为在我的努力控制器中我可能需要添加某种形式的渲染动作
rails reponds_to do |f| f.json
{ render :json => some_hash
}
end
1 个解决方案
#1
9
You can use jQuery's get function in order to send a GET request to your rails app like so
您可以使用jQuery的get函数来向您的rails应用程序发送GET请求
$.get("/efforts/24", { name: "John", time: "2pm" } );
And in your controller you could do something like
在你的控制器中你可以做类似的事情
def show
@effort = Effort.find(params[:id])
# or find by some of the name of time params
respond_to do |f|
format.json { render :json => @effort }
end
end
Depending on how you want to render the results you can either let javascript handle the ajax:success by adding in to the original jQuery get
根据你想要渲染结果的方式,你可以让javascript通过添加到原始的jQuery来处理ajax:success
$.get("/efforts/24", { name: "John", time: "2pm" } ).success(function(data) {
alert(data)
// Or do whatever you want :)
});
Or you can change the respond_to to
或者您可以将respond_to更改为
respond_to do |f|
format.html { render :nothing => true }
format.js
end
And create a show.js.erb in your app/views/efforts directorty. In this file you can use the responded object
并在你的app / views / effort directorty中创建一个show.js.erb。在此文件中,您可以使用响应的对象
$('body').html("<h1><%= escape_javaScript(@effort.title) %></h1>").append("
<%=escape_javaScript(@effort.content) %>");
Some good tutorials
一些很好的教程
Rails和jQuery jQuery GET
#1
9
You can use jQuery's get function in order to send a GET request to your rails app like so
您可以使用jQuery的get函数来向您的rails应用程序发送GET请求
$.get("/efforts/24", { name: "John", time: "2pm" } );
And in your controller you could do something like
在你的控制器中你可以做类似的事情
def show
@effort = Effort.find(params[:id])
# or find by some of the name of time params
respond_to do |f|
format.json { render :json => @effort }
end
end
Depending on how you want to render the results you can either let javascript handle the ajax:success by adding in to the original jQuery get
根据你想要渲染结果的方式,你可以让javascript通过添加到原始的jQuery来处理ajax:success
$.get("/efforts/24", { name: "John", time: "2pm" } ).success(function(data) {
alert(data)
// Or do whatever you want :)
});
Or you can change the respond_to to
或者您可以将respond_to更改为
respond_to do |f|
format.html { render :nothing => true }
format.js
end
And create a show.js.erb in your app/views/efforts directorty. In this file you can use the responded object
并在你的app / views / effort directorty中创建一个show.js.erb。在此文件中,您可以使用响应的对象
$('body').html("<h1><%= escape_javaScript(@effort.title) %></h1>").append("
<%=escape_javaScript(@effort.content) %>");
Some good tutorials
一些很好的教程
Rails和jQuery jQuery GET