Rendering one ERB page into another using Ruby/Sinatra/AJAX -
i making ruby/sinatra app uses twitter api retrieve timelines. have index page form asks twitter handle search. when submit button clicked, i'd timelines appear underneath form on same page, instead of on result.erb they're going now.
i rendering result page wrong in ajax call ("<%= erb :result %>" literally showing up) i've been failing find right way , can't tell if that's problem.
app.rb:
class app < sinatra::base '/' erb :index end post '/result' @username = params[:username] || "justinbieber" connection = gettweets.new(@username) @all_tweets = connection.get_search_results erb :result, :locals => {'username' => @username} end end end
index.erb:
<html> <head> <link rel="stylesheet" type="text/css" href="/stylesheets/style.css"> <script src="//code.jquery.com/jquery-1.11.3.min.js"></script> <script src="/javascripts/script.js.erb"></script> </head> <body> <div id="input-form"> <form action="/result" method="post"> @ <input type="text" name="username"> <input type="submit"> </form> </div> <div id="loading"></div> <div id="result"></div> </body> </html>
result.erb:
<%= @all_tweets.first.user.name %> <% @tweet_array.each |tweet| %> <p><span class="username">@<%= @username %>: </span> <span class="tweet"><%= tweet %></span> </p> <% end %>
script.js.erb:
$(document).ready(function() { $('form').submit(function(e){ e.preventdefault(); $('#loading').html("<img src='images/loading.gif' height='325' width='325'><br>processing..."); $.ajax({ type: "post", url: "/result", data: $('form').serialize(), success: function(){ $('#loading').hide(); $('#result').html("<%= erb :result %>") }, error: function(){ $("#result").html("no success.") } }); }); });
thanks!
fixed... answer in script.js... forgot include argument in success function. whoops 8)
success: function(result){ $('#loading').hide(); $('#result').html(result) },
Comments
Post a Comment