2

I want to fetch a table from a .html file and show it in a div in my .html.erb file... I tried with loading a simple .txt file as below but its now working.. Please let me know what changes are to be done. Am I going wrong in giving the path??

app/view/account/show_result.html.erb:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script type="text/javascript">

    function showy(count){
         alert(count);
        $("#quickview").load("D:\sample.txt");
        return true;
     }

</script>
</head>
<body>
<h3>Recent Test Results</h3><br>
<div id ="quickview">
    HELLO
</div>
// for loop with index i
<a id="repo" href="" onclick="showy(<%= i %>)" >Quick view!</a>
//some other code
</body>
</html>

Update1: I actually want to access a HTML file in the public folder of my app. The path goes like this.. RORapp/public/reports///.html file

      $("#quickview").load("http://localhost:3000/reports/"+userid+"/"+fold+"/overview.html #overviewTable");

Update2:

When I just the copy the url(http://localhost:3000/reports/"+userid+"/"+fold+"/overview.html) and open in browser, i can see the html page.. But when i try to load a part of it, I'm not able to do it..In cmd , I get an error saying

AbstractController::ActionNotFound (The action 'reportng' could not be found for AccountController):

account is my main controller, and reportng is the css file used for styling of that html page

On checking the console for errors, I got the following error ( on pressing F12 in browser)

TypeError: element.dispatchEvent is not a function
fire()protot...?body=1 (line 4072)
element = Object[Document show_test_results]
eventName = "dom:loaded"
memo = undefined
_methodized()protot...?body=1 (line 257)
fireContentLoadedEvent()protot...?body=1 (line 4106)
[Break On This Error]   

element.dispatchEvent(event);

Solution: Now apart from loading the reqd html file , I'm able to change the attributes too :)

    function showy(count){
        var linkDiv = $('#repo_'+count);
        var fold = linkDiv.data('folder');
        var userid = '<%= self.current_user.id %>';
        var server = '<%= request.host_with_port %>';

        $( "#success_"+count ).load( "http://"+server+"/reports/"+userid+"/"+fold+"/overview.html #overviewTable" , function(){
            $("#success_"+count).find("a").attr("href","http://"+server+"/reports/"+userid+"/"+fold+"/suite1_test1_results.html");
        });

         **SOME CODE**

}
6
  • You most likely need to be working with a server. I check the javascript console for errors - I'm guessing you'll see one about "Not Allow By Cross-domain Policy" Commented Oct 30, 2013 at 12:40
  • I'm using webrick server.. I'm starting it on my machine .. and accessing the app as localhost:3000 Commented Oct 30, 2013 at 12:43
  • 1
    Then your path to sample.txt should look something like http://localhost:3000/path/to/sample.txt (or be a relative path) Commented Oct 30, 2013 at 12:44
  • OH yes!! Lemme try and get back.. Commented Oct 30, 2013 at 12:47
  • Cool, glad you sorted it out. You can load HTML in the same way. Remember to accept Rory's answer below to confirm that the problem is fixed. Commented Oct 30, 2013 at 12:57

1 Answer 1

2

AJAX calls to locations local to the client machine are blocked by the browser for security reasons.

For this to work you need to put it online, or use a local server, eg: http://localhost/mysite

Sign up to request clarification or add additional context in comments.

7 Comments

I'm using webrick server.. I'm starting it on my machine .. and accessing the app as localhost:3000.. I'm new to ROR.. Can you pls explain what needs to be done?? When I used the same code in a normal .html file, the .load() function did work..
You need to access the sample.txt via a URL on your localserver, eg. http://localhost:3000/folder/sample.txt or even a relative path from the root: /folder/sample.txt. The problem is because you are using a local URI (the D: drive).
I did it but its not working.. I need to access a html file in public folder of my ROR app..(public/reports/1/overview.html) How can I get the path for that?? I tried this $("#quickview").load("localhost:3000/../../sample.txt"); and $("#quickview").load("localhost:3000/reports/"+userid+"/"+fold+"/… #overviewTable");
Yeah I have given http://.. Here while creating link its not showing. Is my url right?
In that case check the console for errors (press F12 in the browser)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.