|
| 1 | +# matestack core component: rails_view |
| 2 | + |
| 3 | +Show [specs](../../spec/usage/components/rails_view_spec.rb) |
| 4 | + |
| 5 | +Use your rails views or partials with this component in your views, matestack pages, components or apps. |
| 6 | + |
| 7 | +## Parameters |
| 8 | + |
| 9 | +This component expects that either `view` or `partial` is given as parameter. |
| 10 | + |
| 11 | +#### # view - optional |
| 12 | +Expects a string, specifying the path of the view that should be rendered by the component. |
| 13 | +The path resolution works like rails `render 'your_path'` path resolution. |
| 14 | + |
| 15 | +#### # partial - optional |
| 16 | +Expects a string, specifying the partial that should be rendered by the component. |
| 17 | +The path resolution works like rails `render partial: 'your_path'` path resolution. |
| 18 | + |
| 19 | +## Example 1 - Using Rails View and Partial on a page, component or app |
| 20 | + |
| 21 | +```ruby |
| 22 | +class WelcomePage < Matestack::Ui::Page |
| 23 | + def response |
| 24 | + rails_view view: 'welcome/header' |
| 25 | + rails_view partial: 'welcome/hero' |
| 26 | + heading text: 'Welcome', size: 1 |
| 27 | + end |
| 28 | +end |
| 29 | +``` |
| 30 | + |
| 31 | +The above page will first render your 'welcome/header' view. |
| 32 | +If for example you use erb templates, it will render your `app/views/welcome/header.html.erb` view. |
| 33 | +And secondly it will render your 'welcome/hero' partial. |
| 34 | +Again, if you use erb templates, it will render your `app/views/welcome/_hero.html.erb` partial. |
| 35 | +The usage of `rails_view` is the same on a page, component or app. |
| 36 | + |
| 37 | + |
| 38 | +## Example 2 - Using Rails View in a view |
| 39 | + |
| 40 | +You can also use `rails_view` in another view |
| 41 | + |
| 42 | +```html |
| 43 | +<%= matestack_component(:rails_view, view: 'welcome/header') %> |
| 44 | +<%= matestack_component(:rails_view, partial: 'welcome/hero') %> |
| 45 | +``` |
| 46 | + |
| 47 | +## Example 3 - Passing and accessing data |
| 48 | + |
| 49 | +You can access all your instance variables from your controller action within a view or partial rendered by `rails_view`. |
| 50 | +You can also pass data explicitly. Just add them to the hash you pass to the `rails_view` call. |
| 51 | +You can access those like you would do if you pass `locals` to a rails view or partial. |
| 52 | + |
| 53 | +Your `rails_view` call: |
| 54 | +```ruby |
| 55 | +rails_view view: 'welcome/header', headline: 'Great to see you!' |
| 56 | +``` |
| 57 | + |
| 58 | +and in your rails view you can access your data as follows: |
| 59 | +``` |
| 60 | +<h1><%= headline %></h1> |
| 61 | +``` |
0 commit comments