Skip to content

Commit 7b9ccc9

Browse files
committed
Add more docs
1 parent 3ae8c8c commit 7b9ccc9

File tree

3 files changed

+89
-46
lines changed

3 files changed

+89
-46
lines changed

docs/guide/responses.md

Lines changed: 53 additions & 45 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

@@ -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

docs/guide/shared-data.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ class EventsController < ApplicationController
2626
end
2727
```
2828

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:
32+
33+
```ruby
34+
# Parent controller
35+
class ApplicationController < ActionController::Base
36+
inertia_share app_name: 'My App', version: '1.0'
37+
end
38+
39+
# Child controller
40+
class UsersController < ApplicationController
41+
# Inherits app_name and version, adds/overrides auth
42+
inertia_share auth: -> { { user: current_user } }
43+
end
44+
```
45+
2946
### Conditional Sharing
3047

3148
You can control when data is shared using Rails-style controller filters. The `inertia_share` method supports these filter options:

0 commit comments

Comments
 (0)