Skip to content

Commit f346a7d

Browse files
author
Makoto Furuya
committed
Updating README to add examples for view helpers
1 parent 845450e commit f346a7d

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

README.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ a file inside the `config/initializers` directory.
545545
#### Using view helpers
546546

547547
Sometimes you'll need to use view helper methods like `link_to`, `h`, `mailto`,
548-
`edit_resource_path` in the returned JSON representation returned by the `data`
548+
`edit_resource_path`, `check_box_tag` in the returned JSON representation returned by the `data`
549549
method.
550550

551551
To have these methods available to be used, this is the way to go:
@@ -558,22 +558,46 @@ class MyCustomDatatable < AjaxDatatablesRails::Base
558558
def_delegator :@view, :mail_to
559559

560560
# or define them in one pass
561-
def_delegators :@view, :link_to, :h, :mailto, :edit_resource_path, :other_method
561+
def_delegators :@view, :link_to, :h, :mailto, :edit_resource_path, :check_box_tag, :other_method
562+
563+
# Define columns as described above for `id`, `first_name`, `email`, and others
564+
def view_columns
565+
...
566+
end
562567

563568
# now, you'll have these methods available to be used anywhere
564569
# example: mapping the 2d jsonified array returned.
565570
def data
566571
records.map do |record|
567572
{
573+
id: check_box_tag('users[]', record.id),
568574
first_name: link_to(record.fname, edit_resource_path(record)),
569-
email: mail_to(record.email),
575+
email: mail_to(record.email)
570576
# other attributes
571577
}
572578
end
573579
end
574580
end
575581
```
576582

583+
If you want to keep things tidy in the data mapping method, you could use
584+
[Draper](https://github.com/drapergem/draper) to define column mappings like below.
585+
586+
```ruby
587+
...
588+
def data
589+
records.map do |record|
590+
{
591+
id: record.decorate.id,
592+
first_name: record.decorate.first_name,
593+
email: record.decorate.email
594+
# other attributes
595+
}
596+
end
597+
end
598+
...
599+
```
600+
577601

578602
#### Options
579603

0 commit comments

Comments
 (0)