简体   繁体   English

Rails:如何从一个控制器的动作渲染多个部分?

[英]Rails: How to render more than one partial from one controller's action?

I'm creating comments and I use create.js file that contains an ajax call to add comments, 我正在创建注释,我使用包含ajax调用的create.js文件来添加注释,

Now I'm adding notification into a partial, I want the notification section to refresh at the same time when the comment is added, 现在我将通知添加到局部,我希望通知部分在添加注释的同时刷新,

I'm trying to do this but it doesn't work, I was wondering if there would be a way around it: 我正在尝试这样做,但它不起作用,我想知道是否会有办法解决它:

inside def create action 内部def create动作

if @comment.save
  track_activity @comment  
  #refresh_dom_with_partial('div#comments_container', 'comments')
  respond_to do |format|
    format.js { @comments = @commentable.user_comments.order(:created_at) }
    format.html #{ redirect_to @commentable }
    render :partial => "layouts/notification"

  end
else
  render :new
end

the render :partial => "layouts/notification" is kind of breaking the comment Ajax call as well. render :partial => "layouts/notification"也是一种打破Ajax调用的注释。 Bottom line: I don't know how to refresh more than 1 partial from this action. 结论:我不知道如何从这个动作中刷新超过1个部分。

你可以在create.js中渲染局部

$("#some_div_id").empty().html("<%=escape_javascript(render('layouts/notification'))%>");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将一个控制器的局部视图渲染到轨道上的ruby中的另一个控制器 - How to render partial view of one controller to another controller in ruby on rails 如何在Rails中使用对象的控制器的create动作用对象的新实例渲染局部对象? - How can I render a partial with a new instance of my object from the create action of that object's controller in Rails? 无法使用jbuilder渲染多个局部 - unable to render more than one partial with jbuilder 如何在Rails的ruby中的一个控制器中执行多个更新 - how to do more than one updates in one controller in ruby on rails 如何传递多个参数以在 Rails 中渲染? - How to pass more than one parameters to render in rails? 当我在另一个控制器的动作中从一个控制器渲染部分时,缺少模板错误 - Missing Template Error when I render a partial from one controller in the action of another controller 使用Rails 5 Action Cable的多个参数 - More than one params with Rails 5 Action Cable Rails 3:如何从另一个控制器的视图呈现动作 - Rails 3: How to render an action from another controller's view 我如何为多个 controller 导轨 6 布线方法 - How can i route a mothod for more than one controller rails 6 如何在Rails 4控制器中“允许”来自“X-Frame-Options”的多个域? - How to “allow-from” more than one domain for “X-Frame-Options” in Rails 4 controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM