使用 slim 和 Rails 4 的动态部分名称 & 获取部分名称无效 Ruby 标识符错误

Dynamic partial name using slim and Rails 4 & getting Partial Name is Not Valid Ruby Identifier Error

我正在构建一个 Rails 4 博客应用程序并使用 Slim。 我正在尝试使用 Post table 中的 'img_style' 列动态设置部分名称。这将根据不同的枚举 'image styles'.

调用不同的部分 查看

= render = 'img_style_{#@post.img_style}'

这应该产生等同于:

= render = 'img_style_1'

我希望它能够渲染我的局部 '_img_style_1.html.slim'

现在,使用我的代码,它只会在屏幕上呈现此错误:

There is an Error: The partial name (img_style_{#@post.img_style}) is not a valid Ruby identifier; make sure your partial name starts with underscore, and is followed by any combination of letters, numbers and underscores.
用单引号 ('') 定义的

Ruby String 不允许表达式替换。您需要改用双引号 (""),并使用正确的替换语法(# before {):

"img_style_#{@post.img_style}"

ETA 相关地,也许你已经这样做了,但我会确保 img_style 只包含使用验证定义部分的值,例如:

validates_inclusion_of :img_style, in: [1,2,3]