-
Notifications
You must be signed in to change notification settings - Fork 7
Pitfalls
The most trap i step on is when i forget to select attributes, that are used during rendering. Here is a quick example.
A User model has the attributes first_name and last_name and a method full_name that combines the two:
class User < ActiveRecord::Base
def full_name
"#{first_name} #{last_name}"
end
end
Now when i define a fancygrid to show the full name only, it will not work
def index
fancygrid_for :users do |g|
g.columns :full_name
g.find
end
end
The problem is that the attributes first_name and last_name are not selected in the sql query and the records are returned without them. So the solution is to select both attributes in the find block
def index
fancygrid_for :users do |g|
g.columns :full_name
g.find do |query|
query.select :first_name, :last_name
end
end
end
Another solution is to disable the query optimization. However, this is not recommended for large data.
def index
fancygrid_for :users do |g|
g.columns :full_name
g.select = false
g.find
end
end
If you add a block to the fancygrid render method, then you are able to format column values. Inside that block you usually have a switch statement to handle each column. If the else case is missing on the end of the statement, unformatted columns will appear with no content.
This is bad
= fancygrid :users do |column, record, value|
- case column.name
- when :email
= mail_to value
This is good
= fancygrid :users do |column, record, value|
- case column.name
- when :email
= mail_to value
- else
= value