i need response ajax call. here want do:
- make ajax call rails app html document.
- inside rails controller, read html document disk. send document view using "render html: @htmldoc"
- inside view, have tag id "lesson" in div. using response ajax call has html document, put doc "lesson" tag. html rendered textnode "lesson" tag while need rendered html.
here view in new.html.erb:
<div id="lesson"> <h2>choose lesson on left panel start study module.</h2> </div> <div> <button type="button" class="btn btn-primary"id="btn_next">next</button> </div>
here ajax call in new.html.erb:
$(document).ready(function(){ $("#btn_next").click(function(){ $.ajax({ type:'post', url:'/study', data: { id: "demo.html" }, success:function(result){ $("#lesson").html(result); } }); });
});
here routes.rb:
'/study', to: 'study_sessions#new' post '/study', to: 'study_sessions#create'
here code inside controller:
def create @filename = params[:id] @htmldoc = file.read("public/uploads/#{@filename}") if file.exist("public/uploads/#{@filename}") render html: @htmldoc end
here demo.html file:
<table class="table table-striped"> <th>head 1</th><th>head 2</th> <tr><td>row 1</td><td>row 1</td></tr> <tr><td>row 2</td><td>row 2</td></tr> </table>
here result when view browser before clicked button:
choose lesson on left panel start study module.
here result after clicked button:
<table class="table table-striped"> <th>head 1</th><th>head 2</th<tr><td>row 1</td><td>row 1</td></tr> <tr><td>row 2</td><td>row 2</td></tr> </table>
i expect demo.html file rendered nice html table, not string of text.
much appreciate folks!
you need tell rails render html content using .html_safe
method. try doing so:
render html: @htmldoc.html_safe
Comments
Post a Comment