Comments (14)
and then you can use:
column :parent, ->(category) { category.parent&.name }
from trestle.
As @slenderock mentions, having the associations in place are the key to getting this working.
One thing I would add is that in many cases, you can drop the custom block all together and just use:
column :parent
This will automatically display the parent instance (if present) by finding a method from Trestle.config.display_methods
that it responds to. This defaults to [:display_name, :full_name, :name, :title, :username, :login, :email]
so you won't need to alter anything when using the name
method.
It will also automatically link the name to a corresponding admin for the parent model, if it exists.
from trestle.
Try this:
column :parent, ->(parent) { parent.name }
or
column :parent, &:name
from trestle.
Hi @slenderock this will show me the current child name, I need to show the parent name
from trestle.
Hello, @zippax
Sorry for that. I made a little typo
column :parent, ->(current_record) { current_record.parent&.name }
I guess "&" is required to prevent errors when parent doesn't exist for this record.
Hope this helps)
from trestle.
Error
undefined method 'parent'
if I change it to :parent_id will show me :
undefined method 'name' for 1:Fixnum
from trestle.
undefined method 'parent'
for what class ?
from trestle.
column :parent, ->(current_record) { current_record.parent_id&.name }
ActionView::Template::Error at /categories
undefined method `name' for 1:Fixnum
from trestle.
You should use active_record object(not id) if you want to get method #name.
So you need to change back #parent_id to #parent
If you get "undefined method 'parent'" you probably forgot to set belongs_to association in your model.
Please check this out.
It will be great if u can share github link where this problem produses
Btw, I have this line of code in my project and it works fine:
column :user, ->(current_record) { current_record.user.full_name }
from trestle.
Actually the Category model have name and parent_id so there is no association between 2 models. both are in the same table
=> Category(id: integer, name: string, parent_id: integer, created_at: datetime, updated_at: datetime)
from trestle.
this is a Github repo https://github.com/zippax/cs
from trestle.
Seems like you don't have parent model at all. So how you want to get parent name, if you don't have this relation?
from trestle.
so I have to make subCategory model and make association between it and the Category model
from trestle.
You should have this code for subcategory
has_many :subcategories, class_name: 'Category', foreign_key: :parent_id
belongs_to :parent, class_name: 'Category', inverse_of: :subcategories, optional: true
from trestle.
Related Issues (20)
- how show unchecked checkbox in a column(show table)? HOT 1
- Implementing rescue_from HOT 2
- adding maps HOT 3
- Saving seems to not provide a notice of saving but saves anyways HOT 1
- select empty if no relation? HOT 2
- How to add icons in sidebar buttons? HOT 4
- How to fix "to_param called on non-resourceful admin. You may need to explicitly specify the admin." HOT 1
- Listing dates per page HOT 1
- uninitialized constant custom field rails 7 HOT 3
- Changing date format HOT 3
- Option to disable form as "show" view? HOT 1
- My custom helper not is recognized HOT 2
- How i use concerns at trestle? HOT 1
- Remove Multiple Records From MODEL Page
- WebSockets / action_cable support? HOT 2
- Pagination for a custom index View
- Fix typo for Vietnamese locale HOT 1
- Direct upload or chunked file upload support? HOT 2
- Fails without sassc-rails
- Unable to locate favicon.ico in public folder HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from trestle.