How to open a partial view from controller as a Jquery Mobile dialog?

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

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

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.