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/asset-versioning.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ One common challenge when building single-page apps is refreshing site assets wh
4
4
5
5
## Configuration
6
6
7
-
To enable automatic asset refreshing, you need to tell Inertia the current version of your assets. This can be any arbitrary string (letters, numbers, or a file hash), as long as it changes when your assets have been updated.
7
+
To enable automatic asset refreshing, you need to tell Inertia the current version of your assets using the `InertiaRails.configure` method and setting the `config.version` property. This can be any arbitrary string (letters, numbers, or a file hash), as long as it changes when your assets have been updated.
Next, update your main JavaScript file to boot your Inertia app. To accomplish this, we'll initialize the client-side framework with the base Inertia component.
35
+
Next, update your main JavaScript file to boot your Inertia app. To accomplish this, we'll use the `createInertiaApp` function to initialize the client-side framework with the base Inertia component.
Copy file name to clipboardExpand all lines: docs/guide/configuration.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ Inertia Rails can be configured globally or in a specific controller (and subcla
4
4
5
5
## Global Configuration
6
6
7
-
If using global configuration, we recommend you place the code inside an initializer:
7
+
Use the `InertiaRails.configure` method to set global configuration options. If using global configuration, we recommend you place the code inside an initializer:
8
8
9
9
```ruby
10
10
# config/initializers/inertia.rb
@@ -19,7 +19,7 @@ The default configuration can be found [here](https://github.com/inertiajs/inert
19
19
20
20
## Local Configuration
21
21
22
-
Use`inertia_config`in your controllers to override global settings:
22
+
The`inertia_config`method allows you to override global settings in specific controllers. Use this method in your controllers to customize configuration for specific parts of your application:
Copy file name to clipboardExpand all lines: docs/guide/csrf-protection.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@
4
4
5
5
Inertia's Rails adapter automatically includes the proper CSRF token when making requests via Inertia or Axios. Therefore, **no additional configuration is required**.
6
6
7
-
However, if you need to handle CSRF protection manually, one approach is to include the CSRF token as a prop on every response. You can then use the token when making Inertia requests.
7
+
However, if you need to handle CSRF protection manually, one approach is to include the CSRF token as a prop on every response. You can then use the token when making Inertia requests with the `router.post` method.
8
8
9
9
:::tabs key:frameworks
10
10
== Vue
@@ -67,7 +67,7 @@ When a CSRF token mismatch occurs, Rails raises the `ActionController::InvalidAu
67
67
68
68
Obviously, this isn't a great user experience. A better way to handle these errors is to return a redirect back to the previous page, along with a flash message that the page expired. This will result in a valid Inertia response with the flash message available as a prop which you can then display to the user. Of course, you'll need to share your [flash messages](/guide/shared-data.md#flash-messages) with Inertia for this to work.
69
69
70
-
You may modify your application's exception handler to automatically redirect the user back to the page they were previously on while flashing a message to the session. To accomplish this, you may use the `rescue_from` method in your `ApplicationController`.
70
+
You may modify your application's exception handler to automatically redirect the user back to the page they were previously on while flashing a message to the session. To accomplish this, you may use the `rescue_from` method in your `ApplicationController` to handle the `ActionController::InvalidAuthenticityToken` exception.
Copy file name to clipboardExpand all lines: docs/guide/deferred-props.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ Inertia's deferred props feature allows you to defer the loading of certain page
4
4
5
5
## Server side
6
6
7
-
To defer a prop, you can use the defer method when returning your response. This method receives a callback that returns the prop data. The callback will be executed in a separate request after the initial page render.
7
+
To defer a prop, you can use the `InertiaRails.defer` method when returning your response. This method receives a callback that returns the prop data. The callback will be executed in a separate request after the initial page render.
8
8
9
9
```ruby
10
10
classUsersController < ApplicationController
@@ -20,7 +20,7 @@ end
20
20
21
21
### Grouping requests
22
22
23
-
By default, all deferred props get fetched in one request after the initial page is rendered, but you can choose to fetch data in parallel by grouping props together.
23
+
By default, all deferred props get fetched in one request after the initial page is rendered, but you can choose to fetch data in parallel by grouping props together using the `group` option with the `InertiaRails.defer` method.
Copy file name to clipboardExpand all lines: docs/guide/forms.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -186,7 +186,7 @@ For a full discussion on handling and displaying [validation](/guide/validation.
186
186
187
187
## Form helper
188
188
189
-
Since working with forms is so common, Inertia includes a form helper designed to help reduce the amount of boilerplate code needed for handling typical form submissions.
189
+
Since working with forms is so common, Inertia includes a form helper designed to help reduce the amount of boilerplate code needed for handling typical form submissions. The `useForm` method provides a convenient way to manage form state, validation, and submission.
Copy file name to clipboardExpand all lines: docs/guide/history-encryption.md
+32-3Lines changed: 32 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ Imagine a scenario where your user is authenticated, browses privileged informat
6
6
7
7
When you instruct Inertia to encrypt your app's history, it uses the browser's built-in [`crypto` api](https://developer.mozilla.org/en-US/docs/Web/API/Crypto) to encrypt the current page's data before pushing it to the history state. We store the corresponding key in the browser's session storage. When the user navigates back to a page, we decrypt the data using the key stored in the session storage.
8
8
9
-
Once you instruct Inertia to clear your history state, we simply clear the existing key from session storage roll a new one. If we attempt to decrypt the history state with the new key, it will fail an Inertia will make a fresh request back to your server for the page data.
9
+
Once you instruct Inertia to clear your history state, we simply clear the existing key from session storage and roll out a new one. If we attempt to decrypt the history state with the new key, it will fail and Inertia will make a fresh request back to your server for the page data.
10
10
11
11
> [!NOTE]
12
12
> History encryption relies on `window.crypto.subtle` which is only available in secure environments (sites with SSL enabled).
@@ -47,12 +47,41 @@ end
47
47
48
48
## Clearing history
49
49
50
-
To clear the history state, you can pass the `clear_history` option to the `render` method:
50
+
To clear the history state on the server side, you can pass the `clear_history` option to the `render` method:
Copy file name to clipboardExpand all lines: docs/guide/responses.md
+56-48Lines changed: 56 additions & 48 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
@@ -139,7 +113,7 @@ Inertia Rails provides a number of generators to help you get started with Inert
139
113
140
114
### Scaffold generator
141
115
142
-
To create a resource with Inertia responses, execute the following command in the terminal:
116
+
Use the `inertia:scaffold` generator to create a resource with Inertia responses. Execute the following command in the terminal:
Rails generators allow templates customization. For example, to customize the controller generator view template, create a file `lib/templates/inertia_templates/controller/react/view.jsx.tt`:
188
+
Rails generators allow templates customization. You can create custom template files in your application to override the default templates used by the generators. For example, to customize the controller generator view template for React, create a file at the path`lib/templates/inertia_templates/controller/react/view.jsx.tt`:
215
189
216
190
```jsx
217
191
exportdefaultfunction <%= @action.camelize %>() {
@@ -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
0 commit comments