|
1 |
| ---- |
2 |
| -description: Still WIP --> 3.0 and future 3.1 changes needs to be added here |
3 |
| ---- |
4 |
| - |
5 | 1 | # Integrating Action View Helpers
|
6 | 2 |
|
7 |
| -Using Rails view helpers ([https://api.rubyonrails.org/classes/ActionView/Helpers.html](https://api.rubyonrails.org/classes/ActionView/Helpers.html)) in components, pages and apps is supported with some limitations currently.  |
| 3 | +Using Rails view helpers ([https://api.rubyonrails.org/classes/ActionView/Helpers.html](https://api.rubyonrails.org/classes/ActionView/Helpers.html)) in components, pages and layouts is supported when using the approaches shown below; |
8 | 4 |
|
9 | 5 | ## Helpers without a block
|
10 | 6 |
|
11 |
| -You just have to put a `plain` before a view helper, if this view helper is rendering a HTML string, for example: |
| 7 | +Simple Action View helpers working without a block, can easily be integrated in a Matestack class response by calling `plain`: |
12 | 8 |
|
13 | 9 | ```ruby
|
14 |
| -plain link_to "Show", post_path(@post) |
| 10 | +def response |
| 11 | + plain t("my.locale") |
| 12 | + # ... |
| 13 | + plain link_to "Show", post_path(@post) |
| 14 | + # ... |
| 15 | + plain my_own_view_helper_method |
| 16 | + # ... |
| 17 | + plain any_method_returning_a_string |
| 18 | +end |
| 19 | +``` |
| 20 | + |
| 21 | +## Helpers yielding a block |
| 22 | + |
| 23 | +If you want to use a helper like `form_for` you have to follow following approach: |
| 24 | + |
| 25 | +```ruby |
| 26 | +def response |
| 27 | + # ... |
| 28 | + plain do # <-- add this |
| 29 | + form_with url: "/some_path" do |f| |
| 30 | + matestack_to_s do # <-- add this, which converst following block to a string |
| 31 | + plain f.text_field :foo # <-- call plain here again |
| 32 | + br |
| 33 | + div class: "some-input-wrapper" do |
| 34 | + plain f.text_field :bar |
| 35 | + end |
| 36 | + br |
| 37 | + plain f.submit |
| 38 | + end |
| 39 | + end |
| 40 | + end |
| 41 | + # ... |
| 42 | + plain do |
| 43 | + link_to root_path do |
| 44 | + matestack_to_s do |
| 45 | + div class: "some-link-wrapper" do |
| 46 | + plain "foo from block" |
| 47 | + end |
| 48 | + end |
| 49 | + end |
| 50 | + end |
| 51 | + # ... |
| 52 | + # Code below will not work! |
| 53 | + plain link_to root_path do |
| 54 | + matestack_to_s do |
| 55 | + div class: "some-link-wrapper" do |
| 56 | + plain "foo from block" |
| 57 | + end |
| 58 | + end |
| 59 | + end |
| 60 | +end |
15 | 61 | ```
|
16 | 62 |
|
17 | 63 | {% hint style="info" %}
|
18 | 64 | A component needs to be called in context of a controller (with included `Matestack::Ui::Core::Helper`), which is true when you're calling components from Rails views or on Matestack Pages/Layouts (which are themselves called by a controller normally).
|
19 | 65 |
|
20 |
| -When calling a component in isolation (which is possible), the view helpers might not work properly! |
| 66 | +When calling a component in isolation (which is possible), the view helpers might not work properly, especially when (implicitly) requiring a request object! |
21 | 67 | {% endhint %}
|
22 | 68 |
|
23 |
| -## Helpers yielding a block |
24 |
| - |
25 |
| -{% hint style="danger" %} |
26 |
| -It's currently not possible to use view helpers requiring a block, such as the `form_for`. We're working on supporting them soon! |
| 69 | +{% hint style="info" %} |
| 70 | +We're currently working on an improved way to integrate ActionView Helpers which will enable removing the `plain` calls from your code. |
27 | 71 | {% endhint %}
|
28 |
| - |
29 |
| -If you want to use a helper like `form_for` please bypass the current limitation by integrating a Rails partials as describe in the next section. |
|
0 commit comments