Solved with the gracious help of @jenrzzz on IRC (whom I presume is the same as @jenrzzz on Twitter?
The solution was to change
@blog = OpenStruct.new(title: blog_title,
subtitle: blog_subtitle,
entries: [])
to
view.assigns[:blog] = OpenStruct.new(title: blog_title,
subtitle: blog_subtitle,
entries: [])
The former had Just Worked when dealing with this template as Haml in an earlier incarnation of the project. Changing to Slim, however, made my (implicitly) assigned variables go floating off into the void, never to return. Explicitly using view.assigns[:var_name]
works both ways.
If someone can point me to where this charlie-fox is documented somehow, I'd be very appreciative. Until then, it may be runes and incantations, but as long as it's working, repeatable runes and incantations, I'm good to go... to bed, at this point.
Original content follows.
Trying to move to Slim from Haml in a Rails app, but having incredibly n00b problems that have stumped me all day long.
How the heck do I access object methods or fields in a Slim template? And how come this isn't blindingly obvious?
View spec and view template given below, as well as a transcript of a Pry session.
What am I missing here? I know it's trivial, but I'm just not seeing it. Any help greatly appreciated. Thanks.
- We drop into the Pry debugger in our outermost
:before
block, just before the view is rendered. This allows us to examine the data that's been set (in@blog
) and set a breakpoint in theActionView::TestCase#reender
code just before the actual rendering is done. - We set the breakpoint, and then
- exit the current breakpoint, allowing RSpec to run until the next breakpoint is hit.
- We hit the breakpoint we just set, and look at the object that's been copied into the view assigns.
- Just to be blindingly obvious, we verify that accessing the blog title via a nested array works...
- ...as does a method call.
- We call
view#render
, just as it will be immediately after we exit the current breakpoint. We notice that the<h1>
and<h2>
tag pairs have empty content; our title and subtitle were not read by the template engine. - We exit again, and the spec fails (as we now expect).
Again, why?