Part 1: What i want is to fetch records of two tables in one collection select. Later, i want to perform search based on selected item.
So far i have managed to get the records in this manner in one select:
Controller:
#result1 = Model1.all
#result2 = Model2.all
#all = #result2 | #result1
View:
<%= collection_select :id,:id,#all, :id, :id,{prompt: "All Templates"} %>
The problem here is i want to display the name form Model1 and type from Model2.
Part 2 If the user selects the name, i want to get record from Model1 and if the type is selected, i want to get records form Model2.
All i am able to get is the id of both the models in one collection select. I am out of ideas. Let me know if any more details are required. Any help is appreciated. Thanks.
You've supplied :id to collection_select for the text_method. Check the docs to see how this helper works.
One solution would be to create an 'alias' method in each of your models which you can then call in collection_select:
model1.rb
class Model1
def text_value
name
end
end
model2.rb
class Model2
def text_value
type
end
end
I've named the method, text_value, for demonstration purposes. You may need to come up with a different name for that attribute.
Incidentally type as an attribute is reserved for Single Table Inheritance tables so it would be better to use a different attribute name.
in the view
<%= collection_select :id,:id, #all, :id, :text_value, {prompt: "All Templates"} %>
Related
I'm trying to customize an enum field with something else than only the name.
For example my entity record from database have columns as: name, postal_code, id etc ..
and I would like to have something like this in the dropdown "#{name} #{postal_code}, #{department}
I'm doing this:
field :city, :enum do
enum do
# Here I would like to get the entity from DB in order to have all
# columns to something similar as =>
entity.collect.each { |c| "#{c.name} (#{c.postal_code}), #
{c.department.name}"}
end
end
but I don't know how to get the active records (entity in my example) of the actual value of City entity.
How can I do this?
note: that department belongs to another model who is associated to City
Considering the OP comments i'm going to asume that the model in question has this association defined like this:
belongs_to :city
And the city field itself something like this
rails_admin do
edit do
field :city
end
end
Because this way rails admin will render a select drop down that will allow you to search the cities without loading them all.
After that on the City model you can define the title method, quoting the docs
By default it tries to call "name" or "title" methods on the record in question. If the object responds to neither, then the label will be constructed from the model's classname appended with its database identifier. You can add label methods (or replace the default [:name, :title]) with:
RailsAdmin.config {|c| c.label_methods << :rails_admin_title }
The title method can then be defined
class City < ApplicationRecord
def rails_admin_title
"#{self.name} (#{self.postal_code}), #{self.department.name}"
end
end
In a related issue, you will probably want to configure how rails admin searches for the city, i'll just link to the docs.
You can do it, but you need to define a name and a (stable)value for each select drop down, a simple hash out to do it like this:
field :city, :enum do
enum do
entity = bindings[:object]
City.all.map do |c|
{c.id => "#{c.name} (#{c.postal_code}), #{c.department.name}"}
end
end
end
Im assuming each city has an id, but you can use any value you want to store on that field on your DB.
So your users would see the nice formatted string, but rails admin would post the form with the city id on the city field.
I have a table called key_words which has one column called name.
Then I have another table called publications with a column called key_words which is of array type.
When I create a publication the value saved in key_words is something like this ["1", "29", "40"] (these are ids saved in key_words table)
How can I find a publication if I type for example computers in a searchable input?
In this moment I have a search_form_for to find by the publication title and description.
= search_form_for #search, url: publications_path do |f|
#custom-search-input
.input-group.col-md-12
= f.input :title_or_short_description_or_description_cont, input_html: { class: 'form-control input-sm' }, placeholder: 'Buscar', label: false
I would create a third table key_words_publications to store the relationship instead of the array column. Check out the docs for HABTM here.
class Publication
has_and_belongs_to_many :key_words
end
class KeyWord
has_and_belongs_to_many :publications
end
Then you should be able to do something like:
key_words_name_cont
With django-tables2, i am trying to set the id of each element of my model as an "attribute" of every single <td> corresponding to a specific <tr>.
I'm using a dict in the Column Attributes definition like so:
class MainTable(tables.Table):
id = tables.Column()
Client = tables.Column(attrs={'td': {
'data-name': 'Client',
'data-type': 'text',
'data-pk': lambda record: record.pk,
'data-url': 'path/to/url',
'data-placeholder': 'New Client',
'data-title': 'New Client' }})
Every attribute is applying correctly except the 'data-pk'. Is there a way to get the primary key inside the dict? Or any other way to set this attribute using django-tables2 ?
Currently, you cannot do that on columns, but you can on rows:
class Table(tables.Table):
class Meta:
row_attrs = {
'data-pk': lambda record: record.pk
}
Which might make more sense anyway, since a record is mapped to a row, not to a single table cell in a row.
I can see the use for computable attrs with arguments on table columns, so if you decide you need it, you are welcome to open a Pull request.
I have the following situation. I have two table Student and Course linked via HABTM association.
On Student form, I want to have a list of Courses, however I need more information displayed than a simple checkbox and a label next to it. Essentially, I want to have an HTML table with information from my Course model with a checkbox for the association in the first column of the table.
How can this be accomplished?
Essentially this did it:
I iterated through my #courses collection and created each checkbox manually like this:
check_box_tag "student[course_ids][]", course.id
In my controller params I added this:
params.require[:student].permit(... ,course_ids: [])
My Student model needed this:
accepts_nested_attributes_for :courses
Pretty simple if you know what you're doing :-)
I am using RoR 4.1 with RailsAdmin 0.6.8. I can create custom fields in RA either by defining a method on the model and including it via
field :custom_method
or formatted_value
field :custom_field do
formatted_value{ bindings[:object].something_here }
end
The problem is I cannot sort by that column in list view. Anybody have experience with this?
field :custom_field do
sortable :name #or:lastname or :firstname
formatted_value{ bindings[:object].something_here}
end