2017-07-22 115 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

啊,好吧。對於那個很抱歉。接受的解決方案肯定更好。 –

相關問題