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/responses.md
+53-45Lines changed: 53 additions & 45 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,77 +23,51 @@ Within Rails applications, the `Event/Show` page would typically correspond to t
23
23
> [!WARNING]
24
24
> To ensure that pages load quickly, only return the minimum data required for the page. Also, be aware that **all data returned from the controllers will be visible client-side**, so be sure to omit sensitive information.
25
25
26
-
### Using instance variables as props
26
+
### Automatically determine component name
27
27
28
-
Inertia enables the automatic passing of instance variables as props. This can be achieved by invoking the `use_inertia_instance_props` function in a controller or in a base controller from which other controllers inherit.
28
+
You can pass props without specifying a component name:
29
29
30
30
```ruby
31
-
classEventsController < ApplicationController
32
-
use_inertia_instance_props
33
-
34
-
defindex
35
-
@events=Event.all
36
-
37
-
render inertia:'Events/Index'
31
+
classUsersController < ApplicationController
32
+
defshow
33
+
render inertia: { user:@user } # Will render '../users/show.jsx|vue|svelte'
38
34
end
39
35
end
40
36
```
41
37
42
-
This action automatically passes the `@events` instance variable as the `events` prop to the `Events/Index` page component.
43
-
44
-
> [!NOTE]
45
-
> Manually providing any props for a response disables the instance props feature for that specific response.
46
-
47
-
> [!NOTE]
48
-
> Instance props are only included if they are defined **after** the `use_inertia_instance_props` call, hence the order of `before_action` callbacks is crucial.
49
-
50
-
### Automatically determine component name
51
-
52
-
Rails conventions can be used to automatically render the correct page component by invoking `render inertia: true`:
38
+
If the default component path doesn't match your convention, you can define a custom resolution method via the `component_path_resolver` config value. The value should be callable and will receive the path and action parameters, returning a string component path.
53
39
54
40
```ruby
55
-
classEventsController < ApplicationController
56
-
use_inertia_instance_props
57
-
58
-
defindex
59
-
@events=Event.all
60
-
61
-
render inertia:true
41
+
inertia_config(
42
+
component_path_resolver:->(path:, action:) do
43
+
"Storefront/#{path.camelize}/#{action.camelize}"
62
44
end
63
-
end
45
+
)
64
46
```
65
47
66
-
This renders the `app/frontend/pages/events/index.(jsx|vue|svelte)` page component and passes the `@events` instance variable as the `events` prop.
67
-
68
-
Setting the `default_render` configuration value to `true` establishes this as the default behavior:
48
+
### Using instance variables as props
69
49
70
-
```ruby
71
-
InertiaRails.configure do |config|
72
-
config.default_render =true
73
-
end
74
-
```
50
+
Inertia enables the automatic passing of instance variables as props. This can be achieved by invoking the `use_inertia_instance_props` function in a controller or in a base controller from which other controllers inherit.
75
51
76
52
```ruby
77
53
classEventsController < ApplicationController
78
54
use_inertia_instance_props
79
55
80
56
defindex
81
57
@events=Event.all
58
+
59
+
render inertia:'Events/Index'
82
60
end
83
61
end
84
62
```
85
63
86
-
With this configuration, the `app/frontend/pages/events/index.(jsx|vue|svelte)` page component is rendered automatically, passing the `@events` instance variable as the `events` prop.
64
+
This action automatically passes the `@events` instance variable as the `events` prop to the `Events/Index` page component.
87
65
88
-
If the default component path doesn't match your convention, you can define a custom resolution method via the `component_path_resolver` config value. The value should be callable and will receive the path and action parameters, returning a string component path.
66
+
> [!NOTE]
67
+
> Manually providing any props for a response disables the instance props feature for that specific response.
89
68
90
-
```ruby
91
-
inertia_config(
92
-
component_path_resolver:->(path:, action:) do
93
-
"Storefront/#{path.camelize}/#{action.camelize}"
94
-
end
95
-
)
96
-
```
69
+
> [!NOTE]
70
+
> Instance props are only included if they are defined **after** the `use_inertia_instance_props` call, hence the order of `before_action` callbacks is crucial.
97
71
98
72
## Root template data
99
73
@@ -236,3 +210,37 @@ You can find the default templates in the gem's source code:
236
210
To enable client-side history navigation, all Inertia server responses are stored in the browser's history state. However, keep in mind that some browsers impose a size limit on how much data can be saved within the history state.
237
211
238
212
For example, [Firefox](https://developer.mozilla.org/en-US/docs/Web/API/History/pushState) has a size limit of 16 MiB and throws a `NS_ERROR_ILLEGAL_VALUE` error if you exceed this limit. Typically, this is much more data than you'll ever practically need when building applications.
213
+
214
+
## Detecting Inertia Requests
215
+
216
+
Controllers can determine if a request was made via Inertia:
217
+
218
+
```ruby
219
+
defsome_action
220
+
if request.inertia?
221
+
# This is an Inertia request
222
+
end
223
+
224
+
if request.inertia_partial?
225
+
# This is a partial Inertia request
226
+
end
227
+
end
228
+
```
229
+
230
+
## Inertia responses and `respond_to`
231
+
232
+
Inertia responses always operate as a `:html` response type. This means that you can use the `respond_to` method to handle JSON requests differently, while still returning Inertia responses:
Copy file name to clipboardExpand all lines: docs/guide/routing.md
+19-1Lines changed: 19 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,25 @@ When using Inertia, all of your application's routes are defined server-side. Th
9
9
If you have a page that doesn't need a corresponding controller method, like an "FAQ" or "about" page, you can route directly to a component via the `inertia` method.
10
10
11
11
```ruby
12
-
inertia 'about' => 'AboutComponent'
12
+
# In config/routes.rb
13
+
Rails.application.routes.draw do
14
+
# Basic usage - maps 'dashboard' URL to 'Dashboard' component
15
+
inertia 'dashboard' => 'Dashboard'
16
+
17
+
# Using a symbol - infers component name from route
Copy file name to clipboardExpand all lines: docs/guide/shared-data.md
+17Lines changed: 17 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,6 +26,23 @@ class EventsController < ApplicationController
26
26
end
27
27
```
28
28
29
+
### Inheritance and Shared Data
30
+
31
+
Shared data defined in parent controllers is automatically inherited by child controllers. Child controllers can also override or add to the shared data:
0 commit comments