在Rails 3中,如何在JSON响应中呈现HTML ?

时间:2022-10-08 14:05:40

I'm porting an application from Merb 1.1 / 1.8.7 to Rails 3 (beta) / 1.9.1 that uses JSON responses containing HTML fragments, e.g., a JSON container specifying an update, on a user record, and the updated user row looks like . In Merb, since whatever a controller method returns is given to the client, one can put together a Hash, assign a rendered partial to one of the keys and return hash.to_json (though that certainly may not be the best way.) In Rails, it seems that to get data back to the client one must use render and render can only be called once, so rendering the hash to json won't work because of the partial render.

我正在将一个应用程序从Merb 1.1 / 1.8.7移植到Rails 3 (beta) / 1.9.1,该应用程序使用包含HTML片段的JSON响应,例如,在用户记录上指定更新的JSON容器,更新的用户行看起来是这样的。在Merb中,无论控制器方法返回的是什么,都可以将一个散列放在一起,将一个呈现的部分分配给其中一个键并返回散列。to_json(尽管这肯定不是最好的方法)。在Rails中,似乎要将数据返回到客户端,必须使用渲染,渲染只能调用一次,因此由于部分渲染,将哈希呈现给json将不起作用。

From reading around, it seems one could put that data into a JSON .erb view file, with <%= render partial %> in and render that. Is there a Rails-way of solving this problem (return JSON containing one or more HTML fragments) other than that?

通过阅读,似乎可以将数据放入JSON .erb视图文件中,其中<%=呈现部分%>并呈现它。除了这个问题之外,是否有铁路解决方法(返回包含一个或多个HTML片段的JSON) ?

In Merb:
controller:

在Merb:控制器:

only_provides :json
...
self.status = 204 # or appropriate if not async
return {
    'action' => 'update',
      'type' => 'user',
        'id' => @user.id,
      'html' => partial('user_row', format: :html, user: @user)
}.to_json

In Rails:
controller:

在Rails控制器:

respond_to do |format|
  format.json do
    render template: '/json/message-1', 
      locals: { 
        action: 'update',
        type: 'user',
        id: @user.id,
        partial: 'user_row.html.erb', 
        locals: { user: @user }
      }
  end
end

view: json/message-1.json.erb

观点:- 1. json.erb json /消息

{
  "action": <%= raw action.to_json %>,
  "type": <%= raw type.to_json %>,
  "id": <%= raw id.to_json %>,
  "html": <%= raw render(partial: partial, locals: locals).to_json %>
}

3 个解决方案

#1


26  

The closest to the original from Merb approach I could find in Rails is to use #render_to_string

我在Rails中找到的最接近于Merb方法的方法是使用#render_to_string。

render json: {
  'action' => 'update',
    'type' => 'user',
      'id' => @user.id,
    'html' => render_to_string(partial: 'user_row.html.erb', locals: { user: @user })
}

This gets around a fair bit of complexity that comes in from adding a layer of json.erb templates into the mix, whether it's Rails Purist approach I couldn't say; possibly something with RJS would typically be used.

这就避免了添加json层所带来的一些复杂性。erb模板的混合,无论是Rails纯粹主义的方法我都说不出来;可能会使用RJS。

#2


3  

There's another question that has more solutions for json.erb files. See json erb template cannot find other html partial

还有一个问题,关于json有更多的解决方案。erb文件。看到json erb模板找不到其他html部分

#3


-12  

class UsersController < ApplicationController  
  respond_to :json

  def show  
    @user = User.find(params[:id])
    respond_with(@user) do |format|
      if @user.save
        format.json { render :json => @user }
      else
        format.json { render :json => @user.errors, :status => :unprocessable_entity }
      end
    end
  end
end

#1


26  

The closest to the original from Merb approach I could find in Rails is to use #render_to_string

我在Rails中找到的最接近于Merb方法的方法是使用#render_to_string。

render json: {
  'action' => 'update',
    'type' => 'user',
      'id' => @user.id,
    'html' => render_to_string(partial: 'user_row.html.erb', locals: { user: @user })
}

This gets around a fair bit of complexity that comes in from adding a layer of json.erb templates into the mix, whether it's Rails Purist approach I couldn't say; possibly something with RJS would typically be used.

这就避免了添加json层所带来的一些复杂性。erb模板的混合,无论是Rails纯粹主义的方法我都说不出来;可能会使用RJS。

#2


3  

There's another question that has more solutions for json.erb files. See json erb template cannot find other html partial

还有一个问题,关于json有更多的解决方案。erb文件。看到json erb模板找不到其他html部分

#3


-12  

class UsersController < ApplicationController  
  respond_to :json

  def show  
    @user = User.find(params[:id])
    respond_with(@user) do |format|
      if @user.save
        format.json { render :json => @user }
      else
        format.json { render :json => @user.errors, :status => :unprocessable_entity }
      end
    end
  end
end