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 :-)
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 am trying to create a cart using django-carton App. I have two models: Halls and Caterer. I want to add either of these Halls or Caterer object in cart when I will call add() method. While using this API, I need to register my model in settings.py as below
CART_PRODUCT_MODEL = 'marriage_halls.models.Hall'
I can register only one model at a time. So I can't add Caterer object in the cart.To resolve this issue, I'm planning to create new 'product' model which will contain 3 columns viz. {name, price, city}. These are the columns which are common in both Hall and Caterer and I want to display them when I'll call show() method. My first question is, is it a correct way to do it?
If its a correct approach, What I want to do is, whenever I will add new Hall or Caterer in their respective tables through Django's admin interface, only these 3 column values should get inserted to Product table (INSERT new row in product table).
How can I achieve this?
Make Product a base class and use multi table inheritance.
https://docs.djangoproject.com/en/1.10/topics/db/models/#multi-table-inheritance
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"} %>
am Using Apex4.1,
in my application I have one Tabular form which has the following fields,
Emp_id
Emp_name
Dept_id
Here Emp_id is the Updatable column and it is a select list LOV and
Emp_name is a upadatable column,
Here what I need is,
If I select the Emp_id from the LOV ,the Emp_Name should be stored automatically based
on the value selected in EMP_ID,
In tabular form I could not create Dynamic action like creating in normal forms,
Can anyone help me in this issue?
Thank you.
APEX does not currently provide dynamic actions on tabular form items. Hopefully this may be addressed in APEX 4.2 but the Statement of Direction does not explicitly say so.
So for now if you need to do this you will have to write your own Javascript, using the unique IDs of the tabular form items to manipulate them (the IDs look like "fcc_rrrr" where "cc" is the column number and "rrrr" is the row number). See this SO q&q for sample Javascript code that uses these.
The Javascript you need to write is a little daunting (for a beginner), but one thing to note is that in your case you can avoid any need for using AJAX to get the employee name by embedding the name in the return value of the LOV something like this:
select emp_name d, emp_id||':'||emp_name r
from employee
order by 1
This way the return values will look like '123:John Smith'; your Javascript can parse this string and extract 'John Smith' and insert it into the emp_name item on the same row. Obviously you will also need to parse this string to obtain the emp_id value you will need when updating the database when the page is submitted.
I have a Django app that displays a list of rows in a table to the user. Each row maps to an entry in a database. I want to let the user select the rows they would like deleting by adding a checkbox to the end of each row and a delete button ( similar to how gmail lets you delete multiple mail messages). I can't quite figure out how to write the view in terms of finding out which rows were selected and how to map these to the IDs of the entries that need deleting from the database. A simple code snippet showing how to do this would be greatly appreciated.
UPDATE:
I've found this code snippet that I think should do the trick
You can use the CheckboxSelectMultiple widget to auto-generate the corresponding HTML code so you don't have to do it manually.
You can define your form like so:
class UsersForm(forms.Form):
users = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=[QuerySetIterator(Users.objects.all(), "", False)], label="")
Another advantage is that you also get validation for free.
Create a formset and pass can_delete = True to the constructor. Then, in the template,
{{formset}}