简体   繁体   中英

How to access action controller value from jquery

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.

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM