Preview a view_component with form object passed - how to pass form object in preview? #1693
-
I like the idea of passing form object to a ViewCompenent e.g: _app/views/posts/form.html.erb <%= form_with(model: post) do |f| %>
<%= render BulmaInputComponent.new(f, :title) %> app/components/bulma_input_component.rb
app/components/bulma_input_component.html.erb <div class="field">
<%= f.label label, class: "label" %>
<%= tag.div class: "control" do %>
<%= f.text_field name %>
<% end %>
</div> all works well 👍 However I'm struggling to get the preview working class BulmaInputComponentPreview < ViewComponent::Preview
def default
f = form_with model: Post.new # <<<- what should I use for this ????
name = :title
render(BulmaInputComponent.new(f, name))
end
end it would be nice if there was an access in previews to the
Any ideas ? 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I'd recommend using preview templates here. If you still wish to use a preview defined in the current way, one thing to note is that the return value of form_with(model: Post.new) do |f|
name = :title
render(BulmaInputComponent.new(f, name))
end This is just speculation, though, so I'd recommend preview templates. With the solution above, I would recommend using the |
Beta Was this translation helpful? Give feedback.
I'd recommend using preview templates here. If you still wish to use a preview defined in the current way, one thing to note is that the return value of
form_with
is probably rendered output as opposed to the form helper object you want. So maybe this will work:This is just speculation, though, so I'd recommend preview templates. With the solution above, I would recommend using the
fields
form helper to avoid having a wrapperform
tag.