あなたが何をしようとしているのかはっきりしていません。コントローラー変数にアクセスしてjqueryビューにアクセスしますか、それともajax jquery関数からコントローラーアクションを要求し、応答からデータを取得しますか?
私はあなたが最初のことをしたいと思うので、あなたはコントローラーで行うことができます:
class MyController < ApplicationController
def my_action
@file_content = some_content
end
end
ビューmy_action.html.erb
<script type="text/javascript">
$(document).ready(function() {
$("#play").click(function() {
$.get("/requests/download_log", { filename: <%= j @file_content %> });
});
});
</script>
さて、たまたま私が言った2番目のことをしたいのなら、あなたはできる
class MyController < ApplicationController
def download_log
@file_content = some_content
respond_to do |format|
format.json { render json: @file_content.to_json }
end
end
end
とビューで
success_handler = function(json_data) {
// Do any operation with the json_data
}
error_handler = function(jqXHR, textStatus, errorThrown) {
// Handle ajax errors here
}
$("#play").click(function() {
$.ajax("/requests/download_log.json", dataType: "json", success: success_handler, error: error_handler);
});