2017-07-22 214 views
0

我正在尝试使用Rails 5和Bootstrap模式构建泛型模态局部视图。为了使这部分观点,我希望能够做这样的事情:将渲染函数作为参数传递给局部视图

<%= render 'shared/bootstrap_modal', modal_id: 'modalId', modal_title: 'Modal title', modal_body: <%= render 'user_list', users: users %> %>

换句话说,我有我有通用bootstrap_modal局部视图“模式体”节创建。我想通过modal_body参数传递一个渲染函数,这样我就可以在模态体中渲染任何我想要的东西。 Ruby on Rails有可能吗?

回答

0

你只是有一些语法错误,如果你改变你的内在render到这样的事情,这对我的作品

<%= render 'shared/bootstrap_modal', modal_id: 'modalId', modal_title: 'Modal title', modal_body: render('user_list', users: users) %> 
+0

太棒了!这有助于解决问题。 –

0

您可以使用render_to_string方法(docs)。

像这样的东西应该工作

<%= render 'shared/bootstrap_modal', modal_id: 'modalId', modal_title: 'Modal title', modal_body: render_to_string('user_list', locals: {users: users}) %>

+0

感谢您的答复!我在发布之后立即尝试了这个方法,但是我对这个解决方案有一些奇怪的副作用。我接受的答案完全符合我的需要。 –

+0

啊,好吧。对于那个很抱歉。接受的解决方案肯定更好。 –

相关问题