0

I am using jquery draggable and droppable functions. SO when an element is dropped an ajax call would be initiated passing parameters as query string. I know how to pass query string. But i need to do some conditional checking in query string parameters. I am not sure how it can be done. Please find my code below.

jQuery('#<%= qtr %>').droppable({
    hoverclass: 'e_high',
    accept: function(elm) {
        if (jQuery(this).hasClass('<%=name%>_edit'))
            return true;
        },
        drop: function() {
            jQuery.ajax({
                type: "POST",
                url: '/sections/reorder?site_id=<%[email protected]%>&page=<%=@page%>?<%[email protected]%>:nil&ref_id=<%=section.id%>&name=<%=name%>'

Is the above method of passing query string correct. I am getting a No method error. Nil object. When i checked the terminal the params are Parameters: {"page"=>"", "site_id"=>"11650ecf55668fed9e7a624c93adcba2"}

1
  • the question is not clear. About the nil method problem, probably a @variable is not defined. What kind of conditional you want to write? my advice: don't mix ruby and JS code. Use pure-JS funcions (in .js files) that you call from partials (always: argument.to_json in the Ruby arguments) Commented Oct 23, 2011 at 17:27

1 Answer 1

1

For one thing, your ternary question mark and colon are in the wrong place, they should be inside the ERB delimiters. You'd be better off use the data option for $.ajax here; this will not only be cleaner but it will keep you from using GET parameters in a POST request. And you should be escaping your values for using in JavaScript with escape_javascript. Try it like this:

jQuery.ajax({
    type: "POST",
    url: '/sections/reorder',
    data: {
        site_id: '<%= j(@site.id) %>',
        page: '<%= j(@page ? @page.id : '') %>',
        ref_id: '%<= j(section.id) %>',
        name: '%<= j(name) %>'
    },
    //...

This assumes of course that section and name are available in ERB as Ruby variables rather than client-side JavaScript variables.

And if for some reason you must use a GET query string with a POST request, then you'd want something like this:

url: '/sections/reorder?site_id=<%= j(@site.id) %>&page=<%= j(@page ? @page.id : '') %>&ref_id=<%= j(section.id) %>&name=<%= j(name) %>'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.