Skip to content

Using #with_content on pass through slots throws 'undefined method with_content for nil:NilClass' #918

@dkimot

Description

@dkimot

In lieu of a pr I have a suspected cause, detailed at the end of this issue.

Steps to reproduce

Using with_content on a pass through slot with no arguments, like what is documented here.

Expected behavior

The content argument passed into with_content is rendered as the slot.

Actual behavior

The error undefined method with_content for nil:NilClass is raised.

System configuration

Rails version:
6.1.3.1

Ruby version:
3.0.0

Gem version:
2.31.1

Suspected cause

SlotableV2's defined method for a slot, seen here, checks to see if the slots caller passed in args or a block and uses that to determine the response.

If you call the slot without args or a block it's value is the result of get_slot(slot_name) which is the value nil returned on line 202 of that file.

If I pass args into the slot <% modal.header(classes: nil).with_content(title) %> it renders the slot correctly.

Potential solution

I'm happy to open a PR to fix this, but I'm not sure what the solution should be. To get access to with_content the slot's method will need to return an instance of SlotV2 whether or not arguments/a block are passed in. I don't know if that means the parts of set_slot responsible for initializing the slot need to be extracted to a method both get_slotandset_slotcall or ifget_slot` should just be removed completely. I could also be way off base with my proposed solution.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions