Skip to content

Commit d35ce31

Browse files
committed
Documents how to forward methods to the view in README
* Adds `Using view helpers` section to README to document how to allow using view helpers inside our classes.
1 parent a2e2852 commit d35ce31

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ def data
118118
end
119119
```
120120

121+
[See here](#using-view-helpers) if you need to use view helpers in the returned 2d array, like `link_to`, `mail_to`, `resource_path`, etc.
122+
121123
#### Get Raw Records
122124
```ruby
123125
def get_raw_records
@@ -205,6 +207,36 @@ jQuery(document).ready(function() {
205207

206208
### Additional Notes
207209

210+
#### Using view helpers
211+
212+
Sometimes you'll need to use view helper methods like `link_to`, `h`, `mailto`, `edit_resource_path` in the returned JSON representation returned by the `data` method.
213+
214+
To have these methods available to be used, this is the way to go:
215+
216+
```ruby
217+
class MyCustomDatatable < AjaxDatatablesRails::Base
218+
# either define them one-by-one
219+
def_delegator :@view, :link_to
220+
def_delegator :@view, :h
221+
def_delegator :@view, :mail_to
222+
223+
# or define them in one pass
224+
def_delegators :@view, :link_to, :h, :mailto, :edit_resource_path, :other_method
225+
226+
# now, you'll have these methods available to be used anywhere
227+
# example: mapping the 2d jsonified array returned.
228+
def data
229+
records.map do |record|
230+
[
231+
link_to(record.fname, edit_resource_path(record)),
232+
mail_to(record.email),
233+
# other attributes
234+
]
235+
end
236+
end
237+
end
238+
```
239+
208240
#### Options
209241

210242
An `AjaxDatatablesRails::Base` inherited class can accept an options hash at initialization. This provides room for flexibility when required. Example:

0 commit comments

Comments
 (0)