0

I am currently requisition an html partial to be inserted into a modal

  $('#user-addTool').on('click', function(e) {
    $('#userModal div.modal-content').html('');
    var ajaxUrl = "http://" + window.location.host + "/users/new";
    e.preventDefault();
    e.stopPropagation();
    $.get(ajaxUrl, function(data){
        $('#userModal div.modal-content').html(data);
        initGroupSelector();
        initRoleSelector();
        $('#role-selector').multiselect('disable');
    }, "html");    
    $('#userModal').modal('show');        
  }); 

served by a rails app :

     # GET /users/new
  # GET /users/new.json
  def new
    @user = User.new
    @user.profile = Profile.new
    authorize @user
    render partial: 'users/form', layout: false 
  end

I wonder if I can return both, the partial form html AND a son object ( groups and roles to be used in the client js script ?

thanks for feedback

UPDATE 1 --

Can I send a JSON response this way ?

   format.json {
      @response = {
        "html": { "form": <%= (render partial: 'users/form').to_json.html_safe %> },
        "data": { "groups": <%= @groups %> , "roles": <%= @roles %> }
      }
     render(json: @response, status: :success )
  }

In which @group is an Array of Arrays and @roles a Hash of Hashes ?

thanks for advices

  • You get back single responses from single requests. You can put HTML in a JSON response (as a string), you can put JSON in an HTML response (it's just a template, after all), etc. – Dave Newton Nov 26 '15 at 17:42
  • Thanks see my update in the question, about JSON answer .. –  Nov 26 '15 at 21:39
  • 1
    I think you'll have to use `render_to_string` instead of `render` – Amr Noman Nov 26 '15 at 22:09
  • Thanks ... got it .. also looked at : http://stackoverflow.com/questions/4810584/rails-3-how-to-render-a-partial-as-a-json-response –  Nov 27 '15 at 09:53

0 Answers0