Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rails4,Ajax请求,我能用html和json对象得到响应吗?_Ajax_Ruby On Rails 4 - Fatal编程技术网

Rails4,Ajax请求,我能用html和json对象得到响应吗?

Rails4,Ajax请求,我能用html和json对象得到响应吗?,ajax,ruby-on-rails-4,Ajax,Ruby On Rails 4,我目前正在申请将html部分插入模式 $('#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, f

我目前正在申请将html部分插入模式

  $('#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');        
  }); 
由rails应用程序提供:

     # 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
我想知道是否可以同时返回部分表单html和子对象(在客户端js脚本中使用的组和角色)

谢谢你的反馈

更新1--

我可以这样发送JSON响应吗

   format.json {
      @response = {
        "html": { "form": <%= (render partial: 'users/form').to_json.html_safe %> },
        "data": { "groups": <%= @groups %> , "roles": <%= @roles %> }
      }
     render(json: @response, status: :success )
  }
format.json{
@答复={
“html”:{“form”:},
“数据”:{“组”:,“角色”:}
}
呈现(json:@response,status::success)
}
其中@group是数组数组,@roles是散列


感谢您的建议

您可以从单个请求返回单个响应。您可以将HTML放入JSON响应(作为字符串),您可以将JSON放入HTML响应(毕竟,它只是一个模板),等等。谢谢在关于JSON答案的问题中看到我的更新。我想你必须使用
render\u to\u string
而不是
render
谢谢…明白了。还看了: