Is there a javascript equivalent of url_for?
-
There are certain situations where it would be beneficial to use the url_for syntax on the client side. Specifically there are times when I am writing pure javascript code using jQuery to handle my ajax interactions, and I find myself needing to generate a url. As an example, I have a table full of customers. In each table row, I use the HTML5 data- syntax to store the id of the customer. Let's assume that when a user clicks on the row for a customer, I want to present them with a dialog containing more information for the customer. I know of a couple different ways to accomplish my goal, but I guess I am at a loss for the best or "right" way to do this in rails. Example: <table id="customers"> <tr data-customer-id="1000"> <td>ABC Customer</td> </tr> <tr data-customer-id="1001"> <td>DEF Customer</td> </tr> </table> $(document).ready(function() { $("tr").click(function() { var current_tr = $(this); var current_id = current_tr.attr("data-customer-id"); var customer_url = "<%=url_for :controller => :customer, :action => :show, :id => "nil"%>"; customer_url = customer_url.replace("nil",current_id); //imagine that I am loading in some more content from customer_url via ajax }); }); Not sure if this code makes it clear enough, but there is obviously an impedance mismatch between client and server here. Yes, I "hack" my way to a solution, but honestly this solution stinks. To restate my question, is there a way for me to generate the correct urls via javascript? Or barring that, is there a better way for me to tackle this problem altogether? Perhaps in a more "rails elegant" manner?
-
Answer:
I think ideally you should be able to write your HTML in such a way that you can use <a> tags for things like this, store the URL in the href attribute and then retrieve it from there in the JavaScript. Otherwise I would store the URL in a data attribute of the given HTML tag: <%= content_tag :table, id: "customers" do %> <%= content_tag :tr, 'data-customer-url' => customer_path(customer) do %> <td><%= customer.name %></td> <% end %> <% end %> $(document).ready(function() { $('tr').click(function() { $.ajax($(this).data('customer-url')); }); });
JarosÅaw Rzeszótko at Quora Visit the source
Related Q & A:
- How to properly end a javascript statement?Best solution by w3schools.com
- How to pass a javascript object back to the server?Best solution by developer.mozilla.org
- Why is the productivity of the ocean equivalent to that of a desert?Best solution by Yahoo! Answers
- Is there a Canadian equivalent to the Federal Trade Commission?Best solution by answers.yahoo.com
- Need help debugging a javascript?Best solution by allwebdevhelp.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.