简体   繁体   English

将 haml-partial 传递给控制器​​变量

[英]Passing haml-partial to a controller variable

I have a function sending messages to a user on a site.我有一个向网站上的用户发送消息的功能。

Function arguments: user_id, from_user_id, subject, content.函数参数:user_id、from_user_id、subject、content。 I need to pass a haml template to the content variable that supports the link_to helper.我需要将一个 haml 模板传递给支持 link_to 助手的内容变量。

Can I do something like content: partial 'my_partial' ?我可以做类似content: partial 'my_partial'事情吗?

This can be done?这能做到吗?

Because I'm really not comfortable writing long lines in the controller:因为我真的不习惯在控制器中写长行:

Msg.create(user_id: 2420, 
           from_user_id: 2421, 
           subject: 'Finish now!', 
           content: "<p style='text-align: center;'>User A can help!.<br><br><br><br><a class='match-btn-green-radius' href='#'>Finish</a></p>")

It looks like this:它看起来像这样:

好像

Msg.create(user_id: 2420, 
           from_user_id: 2421, 
           subject: 'Finish now!', 
           content: render_to_string(partial: 'foo/bar', locals: { foo: 'bar' }))

This will render the partial at app/foo/_bar.html.haml with a local variable foo set to the value "bar" .这将在app/foo/_bar.html.haml渲染局部变量foo设置为值"bar"

Another approach is to use haml directly.另一种方法是直接使用haml (Be very careful about indentation) (注意缩进)

engine = Haml::Engine.new("
%p Haml code!
h1 Title
")

Msg.create(...., content: engine.render)

Obviously this is just for very simple html, and I'm perfectly well aware of the fact that mixing html in controllers violates all sorts of rules.显然,这仅适用于非常简单的 html,我非常清楚在控制器中混合 html 会违反各种规则。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM