jquery - Rails 4 and AJAX: How to have AJAX change the URL, so reloading still keeps the same js.erb file and user can properly use back/forward buttons -



jquery - Rails 4 and AJAX: How to have AJAX change the URL, so reloading still keeps the same js.erb file and user can properly use back/forward buttons -

in rails 4 app, how can utilize ajax page result changes user sees without reloading (normal ajax stuff), , (i) if reload url see same result , (ii) can nail button go previous page, and, there, forwards button come ajax created page. basically, page appears deed "normally" loaded page?

i have reviewed stuff railscasts 136, 240, , 246, , stack overflow pushstate rails ajax, build next simple code ajax loading post:

config/routes.rb .... resources :posts views/posts/index.html.erb: <h2> posts </h2> <div id="all-posts"> <% @posts.each |post| %> <div id="show-single-post"><%=link_to post.title, remote: true %></div> <% end %> </div> views/posts/show.js.erb: $('#all-posts').hide().after('<%= j render('show_form') %>'); views/posts/_show_form.html.erb: <h3> here post clicked on </h3> <h3><%= @post.title %> </h3> <h4><%= @post.text %></h4> app/controllers/posts_controller.rb class requestscontroller < applicationcontroller ... def index @posts = post.all end def show @post = post.find(params[:id]) end

--this renders show view on index page without reloading. however, url has not changed. seek , alter url:

app/assets/javascripts/index.js $(document).on('click', '.requestshow [data-remote=true]', function() { history.changestate(null, '', $(this).attr('href')); homecoming false; }); $(window).on('popstate', function () { $.get(document.location.href) });

--this code changes url /posts/1. but: when user hits refresh, posts/1 url looks different file--show.html.erb. , if user clicks on 1 post ("post 1"), goes index , clicks on post ( "post 2"), post 1 view seems remain in cache, that, post 2, if nail back, takes not index, post 2 show.js.erb view. using replacestate addresses this, messes button (sends way home page, not index page).

jquery ruby-on-rails ajax

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -