Reputation: 1442
I m new ti jquery. Anybody tell me How to access action controller's value from jquery
I have following code in view...
<% @user_rep.each do |result| %>
<%= link_to result.time,{:action =>'download_log'}, :id => 'l123'%></td>
<% end %>
And I jave written following code in jquery...
jQuery(document).ready(function(){
jQuery("#l123").click(function() {
jQuery("#file").show("slow"); // Showing some div
####What to write here
});
});
And i coded in download_log action
def download_log
IO.foreach "#{RAILS_ROOT}/public/#{filename}" do |line|
@file_content << line
@file_content << '<br/>'
end
end
Any body tell me.. When i click on 'l123' then a div will be shown n the controller action "download_log" is automatically called? If it is possible then how can i access download_log's value "@file_content" in jquery. Please help me.
Upvotes: 0
Views: 204
Reputation: 1652
You could go with something like this:
$(document).ready(function(){
$("#l123").click(function() {
$.ajax({
url: urlOfControllerAndAction,
type: "get",
success: function (response, textStatus, jqXHR) {
$("#file").html(response);
$("#file").show("slow")
},
error: function (jqXHR, textStatus, errorThrown) {
},
// callback handler that will be called on completion
// which means, either on success or error
complete: function () {
}
});
});
});
Ajax jQuery documentation here: http://api.jquery.com/jQuery.ajax/
Upvotes: 2