Reputation: 1125
I have an interactive javascript diagram on a show page in rails 3.2, the page also contains a heading which is linked to items in the graph. I would like update the page through an ajax request when a user clicks on items in the diagram, but I don't want the diagram to reload. I have not implemented ajax requests previously, and I cannot say I fully understand the concept being new to web development. However, I have read a bit about turbolinks and pjax, and my problem seems to related to these, but I have no idea how to implement such an approach.
show.html.erb
<% provide(:title, @item.name) %>
<h2>
<%= @item.name %>
</h2>
…
The url-path to the show page is (for item 1):
localhost:3000/items/1
I guess I would have to change the instance variable, @item, through an ajax request from my javascript code. However, I would also like to update the url so it is not simply a variable change but a url redirect. Any suggestions for how to do this?
Upvotes: 0
Views: 147
Reputation: 11951
This isn't a question that can be answered with code, as there are simply too many ways of implementing what you want, and not enough information to be able to give you anything meaningful.
The key differentiation you need to make is between server and client side code. Ruby executes on the server, and javascript executes in the client's browser. Therefore, you can't modify your @item
variable with an AJAX query. What you can do is edit the HTML that is rendered with AJAX, but that's a subject worthy of several books in itself.
Turbo links and PJAX are an implementation of AJAX that reloads the entire content of the <body>
tag (or a section that you can define) when you click a link, rather than doing a normal page reload. This saves a significant amount of time as assets (javascript, stylesheets, etc) are not reloaded. This may or may not be what you need, so take a look at the PJAX example page.
It would really be worth reading up on AJAX before going any further with this. One resource I can recommend is a book called Agile Development With Rails. This goes through the whole process of building a Rails app, adding AJAX sweetness on top.
However, there are plenty of resources online. First, get a handle on how Ajax works outside of Rails, and then start googling "Rails and AJAX".
Upvotes: 1