Skip to content

Commit 79f6a35

Browse files
authored
Merge pull request #238 from skryukov/docs-improve-searchability
Docs: improve searchability by naming methods
2 parents 483c6bf + 7b9ccc9 commit 79f6a35

12 files changed

+139
-66
lines changed

docs/guide/asset-versioning.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ One common challenge when building single-page apps is refreshing site assets wh
44

55
## Configuration
66

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.
88

99
```ruby
1010
InertiaRails.configure do |config|

docs/guide/client-side-setup.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ npm install @inertiajs/svelte svelte
3232

3333
## Initialize the Inertia app
3434

35-
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.
3636

3737
:::tabs key:frameworks
3838
== Vue

docs/guide/configuration.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Inertia Rails can be configured globally or in a specific controller (and subcla
44

55
## Global Configuration
66

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:
88

99
```ruby
1010
# config/initializers/inertia.rb
@@ -19,7 +19,7 @@ The default configuration can be found [here](https://github.com/inertiajs/inert
1919

2020
## Local Configuration
2121

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:
2323

2424
```ruby
2525
class EventsController < ApplicationController

docs/guide/csrf-protection.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
Inertia's Rails adapter automatically includes the proper CSRF token when making requests via Inertia or Axios. Therefore, **no additional configuration is required**.
66

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.
88

99
:::tabs key:frameworks
1010
== Vue
@@ -67,7 +67,7 @@ When a CSRF token mismatch occurs, Rails raises the `ActionController::InvalidAu
6767

6868
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.
6969

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.
7171

7272
```ruby
7373
class ApplicationController < ActionController::Base

docs/guide/deferred-props.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Inertia's deferred props feature allows you to defer the loading of certain page
44

55
## Server side
66

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.
88

99
```ruby
1010
class UsersController < ApplicationController
@@ -20,7 +20,7 @@ end
2020

2121
### Grouping requests
2222

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.
2424

2525
```ruby
2626
class UsersController < ApplicationController

docs/guide/forms.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ For a full discussion on handling and displaying [validation](/guide/validation.
186186

187187
## Form helper
188188

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.
190190

191191
:::tabs key:frameworks
192192
== Vue

docs/guide/history-encryption.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Imagine a scenario where your user is authenticated, browses privileged informat
66

77
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.
88

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.
1010

1111
> [!NOTE]
1212
> History encryption relies on `window.crypto.subtle` which is only available in secure environments (sites with SSL enabled).
@@ -47,12 +47,41 @@ end
4747

4848
## Clearing history
4949

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:
5151

5252
```ruby
5353
render inertia: 'Dashboard', props: {}, clear_history: true
5454
```
5555

5656
Once the response has rendered on the client, the encryption key will be rotated, rendering the previous history state unreadable.
5757

58-
You can also clear history on the client site by calling `router.clearHistory()`.
58+
### Client-side clearing
59+
60+
You can also clear history directly on the client side by calling the `router.clearHistory()` method:
61+
62+
:::tabs key:frameworks
63+
== Vue
64+
65+
```js
66+
import { router } from '@inertiajs/vue3'
67+
68+
router.clearHistory()
69+
```
70+
71+
== React
72+
73+
```js
74+
import { router } from '@inertiajs/react'
75+
76+
router.clearHistory()
77+
```
78+
79+
== Svelte 4|Svelte 5
80+
81+
```js
82+
import { router } from '@inertiajs/svelte'
83+
84+
router.clearHistory()
85+
```
86+
87+
:::

docs/guide/manual-visits.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,14 @@ router.post('/users', {
270270
== Svelte 4|Svelte 5
271271

272272
```js
273-
274-
```
275-
276273
import { router } from '@inertiajs/svelte'
277274

278275
router.post('/users', {
279-
name: 'John Doe',
280-
276+
name: 'John Doe',
277+
281278
})
279+
```
280+
282281
:::
283282

284283
## Custom headers

docs/guide/responses.md

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -23,77 +23,51 @@ Within Rails applications, the `Event/Show` page would typically correspond to t
2323
> [!WARNING]
2424
> 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.
2525
26-
### Using instance variables as props
26+
### Automatically determine component name
2727

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:
2929

3030
```ruby
31-
class EventsController < ApplicationController
32-
use_inertia_instance_props
33-
34-
def index
35-
@events = Event.all
36-
37-
render inertia: 'Events/Index'
31+
class UsersController < ApplicationController
32+
def show
33+
render inertia: { user: @user } # Will render '../users/show.jsx|vue|svelte'
3834
end
3935
end
4036
```
4137

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.
5339

5440
```ruby
55-
class EventsController < ApplicationController
56-
use_inertia_instance_props
57-
58-
def index
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}"
6244
end
63-
end
45+
)
6446
```
6547

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
6949

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.
7551

7652
```ruby
7753
class EventsController < ApplicationController
7854
use_inertia_instance_props
7955

8056
def index
8157
@events = Event.all
58+
59+
render inertia: 'Events/Index'
8260
end
8361
end
8462
```
8563

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.
8765

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.
8968
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.
9771
9872
## Root template data
9973

@@ -139,7 +113,7 @@ Inertia Rails provides a number of generators to help you get started with Inert
139113

140114
### Scaffold generator
141115

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:
143117

144118
```bash
145119
bin/rails generate inertia:scaffold ModelName field1:type field2:type
@@ -185,7 +159,7 @@ Inertia Rails tries to detect the presence of Tailwind CSS in the application an
185159

186160
### Controller generator
187161

188-
To create a controller with an Inertia response, execute the following command in the terminal:
162+
Use the `inertia:controller` generator to create a controller with an Inertia response. Execute the following command in the terminal:
189163

190164
```bash
191165
bin/rails generate inertia:controller ControllerName action1 action2
@@ -211,7 +185,7 @@ $ bin/rails generate inertia:controller pages welcome next_steps
211185

212186
### Customizing the generator templates
213187

214-
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`:
215189

216190
```jsx
217191
export default function <%= @action.camelize %>() {
@@ -236,3 +210,37 @@ You can find the default templates in the gem's source code:
236210
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.
237211

238212
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+
def some_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:
233+
234+
```ruby
235+
def some_action
236+
respond_to do |format|
237+
format.html do
238+
render inertia: 'Some/Component', props: { data: 'value' }
239+
end
240+
241+
format.json do
242+
render json: { message: 'This is a JSON response' }
243+
end
244+
end
245+
end
246+
```

docs/guide/routing.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,25 @@ When using Inertia, all of your application's routes are defined server-side. Th
99
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.
1010

1111
```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
18+
inertia :settings
19+
20+
# Within namespaces and scopes
21+
namespace :admin do
22+
inertia 'dashboard' => 'Admin/Dashboard'
23+
end
24+
25+
# Within resource definitions
26+
resources :users do
27+
inertia :activity, on: :member
28+
inertia :statistics, on: :collection
29+
end
30+
end
1331
```
1432

1533
## Generating URLs

0 commit comments

Comments
 (0)