|
| 1 | +module Components |
| 2 | + <%- @modules.each do |module_name| %><%= " "* @indet %>module <%= module_name.camelize %><%- @indet += 1 %> |
| 3 | + <%- end %><%=" "* @indet %>class <%= @file_name %> |
| 4 | +
|
| 5 | + <%=" "* @indet %>include React::Router |
| 6 | +
|
| 7 | + <%=" "* @indet %>routes(path: "/") do # change path to be the base path |
| 8 | + <%=" "* @indet %> # you will probably want to update your config/routes.rb file so that it matches all |
| 9 | + <%=" "* @indet %> # subroutes: i.e. get '(*subroutes)' => "<%= (@modules.last || 'home').underscore %>#<%= @file_name.underscore %>" |
| 10 | + <%=" "* @indet %> # basic route has: |
| 11 | + <%=" "* @indet %> # a path |
| 12 | + <%=" "* @indet %> # a name - used to reference the route in methods like redirect, and link) |
| 13 | + <%=" "* @indet %> # a handler - the component that will be mounted on this route |
| 14 | + <%=" "* @indet %> route(path: "subroute1-path", name: :subroute1, handler: Subroute1Component) |
| 15 | + <%=" "* @indet %> route(path: "subroute2-path", name: :subroute2, handler: Subroute2Component) |
| 16 | + <%=" "* @indet %> # routes can take parameters designated with a colon: |
| 17 | + <%=" "* @indet %> route(path: "subroute3-path/:user_id", name: subroute3, handler: Subroute3Component) |
| 18 | + <%=" "* @indet %> # the redirect method will transition any incoming matching routes to a new route |
| 19 | + <%=" "* @indet %> redirect(from: "/", to: :subroute1) |
| 20 | + <%=" "* @indet %> # the not_found method indicates which component to load if no route matches: |
| 21 | + <%=" "* @indet %> not_found(handler: NotFound) |
| 22 | + <%=" "* @indet %>end |
| 23 | + |
| 24 | + <%=" "* @indet %>router_param :user_id, as: :user do |id| |
| 25 | + <%=" "* @indet %> # Translate incoming route params to internal values. |
| 26 | + <%=" "* @indet %> # In this case we will refer to the translated user_id as user. |
| 27 | + <%=" "* @indet %> # The block param (id) will have the value of the param. |
| 28 | + <%=" "* @indet %> # This is useful for looking up ActiveRecord models by ids, etc. |
| 29 | + <%=" "* @indet %>end |
| 30 | + |
| 31 | + <%=" "* @indet %>def show # note that the top level router has a show method NOT render |
| 32 | + <%=" "* @indet %> div do |
| 33 | + <%=" "* @indet %> # content to display on every route |
| 34 | + <%=" "* @indet %> # link generates an anchor tag. |
| 35 | + <%=" "* @indet %> link(to: :subroute3, params: {user_id: 12}, class: "link-class") { "Click to go to subroute3" } |
| 36 | + <%=" "* @indet %> # within an event handler use transition_to to move to a new route |
| 37 | + <%=" "* @indet %> # the route_handler method will display the current route, it can be called in the |
| 38 | + <%=" "* @indet %> # router, or in some child component. |
| 39 | + <%=" "* @indet %> route_handler |
| 40 | + <%=" "* @indet %> end |
| 41 | + <%=" "* @indet %>end |
| 42 | +<%=" "* @indet %>end |
| 43 | + <%- @modules.each do %><%- @indet -= 1 %><%=" "* @indet %>end |
| 44 | + <%- end %>end |
0 commit comments