You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guide/helpers.md
+26-2Lines changed: 26 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -59,16 +59,40 @@ By default, ViewComponents don't have access to helper methods defined externall
59
59
60
60
```ruby
61
61
classUseHelpersComponent < ViewComponent::Base
62
-
use_helpers :icon
62
+
use_helpers :icon, :icon?
63
63
64
64
erb_template <<-ERB
65
65
<div class="icon">
66
-
<%= icon:user %>
66
+
<%= icon? ? icon(:user) : icon(:guest) %>
67
67
</div>
68
68
ERB
69
69
end
70
70
```
71
71
72
+
Use the `from:` keyword to include individual methods defined in helper modules not available in the component:
73
+
74
+
```ruby
75
+
classUserComponent < ViewComponent::Base
76
+
use_helpers :icon, :icon?, from:IconHelper
77
+
78
+
defprofile_icon
79
+
icon? ? icon(:user) : icon(:guest)
80
+
end
81
+
end
82
+
```
83
+
84
+
The singular version `use_helper` is also available:
85
+
86
+
```ruby
87
+
classUserComponent < ViewComponent::Base
88
+
use_helper :icon, from:IconHelper
89
+
90
+
defprofile_icon
91
+
icon :user
92
+
end
93
+
end
94
+
```
95
+
72
96
## Nested URL helpers
73
97
74
98
Rails nested URL helpers implicitly depend on the current `request` in certain cases. Since ViewComponent is built to enable reusing components in different contexts, nested URL helpers should be passed their options explicitly:
0 commit comments