I'm trying to make a form to create new record for a model user which has one billing_information. Billing_information has an attribute account_name that I want to include in the form. I tried using the delegate method but it's not working. It produces :-
error: unknown attribute 'billing_information_account_name' for User.
class User < ActiveRecord::Base
accepts_nested_attributes_for :billing_information
has_one :billing_information, inverse_of: :user
delegate :account_name, to: :billing_information, allow_nil: true
rails_admin do
create do
field :name
field :email
field :billing_information_account_name do
def value
bindings[:object].account_name
end
end
end
end
end
Does anyone has a better solution? Thank you.
Sadly, you won't get help from rails admin in this case, but it can be done.
You have to add a new virtual field and handle in a setter the input. Take a look at this example.
class User < ApplicationRecord
has_one :billing_information, inverse_of: :user
# A getter used to populate the field value on rails admin
def billing_information_account_name
billing_information.account_name
end
# A setter that will be called with whatever the user wrote in your field
def billing_information_account_name=(name)
billing_information.update(account_name: name)
end
rails_admin do
configure :billing_information_account_name, :text do
virtual?
end
edit do
field :billing_information_account_name
end
end
end
You can always create the full billing_information using the nested attributes strategy, meaning add the billing_information field and you'll get a nice form to fill all the information.
Related
I have models User and Article.
User has the following associations:
class User < ActiveRecord::Base
has_many :articles
and Article's:
class Article < ActiveRecord::Base
belongs_to :user, required: true
I use rails_admin as a control panel and customize a required fields in rails_admin.rb as follows:
config.model Article do
list do
field :id
field :title
field :user_id
field :created_at
end
end
But how to implement that instead of :user_id displays the name of the user? I have a username field in my database.
Thanks!
In your Article model, you can add a custom method that will return the user_name:
def user_name
self.user.username
end
Then, you can use this user_name field in your admin like other model attributes:
config.model Article do
list do
field :id
field :title
field :created_at
field :user_name
end
end
Here's another stab at it...
Rails_admin looks for a "name" or "title" field on the model, if there are none, it will display a somewhat ugly "User#1".
All you need to do in your User Model is to add the following method:
def title
try(:username)
end
I like to go back to this presentation for Rails Admin's best practices: http://www.slideshare.net/benoitbenezech/rails-admin-overbest-practices
It's a bit dated but everything you need is there (around page 14 for your case)
I have a many to many relationship with DoctorProfile and Insurance. I'd like to create these associations off of a form from a client side app. I'm sending back an array of doctor_insurances_ids and trying to create the association in one line. Is it possible to send back an array of doctor_insurances ids? If so what's the proper way to name it for mass assignment in the params?
The error I'm getting with the following code is
ActiveRecord::UnknownAttributeError: unknown attribute 'doctor_insurances_ids' for DoctorProfile.
class DoctorProfile
has_many :doctor_insurances
accepts_nested_attributes_for :doctor_insurances # not sure if needed
class Insurance < ActiveRecord::Base
has_many :doctor_insurances
class DoctorInsurance < ActiveRecord::Base
# only fields are `doctor_profile_id` and `insurance_id`
belongs_to :doctor_profile
belongs_to :insurance
def create
params = {"first_name"=>"steve",
"last_name"=>"johanson",
"email"=>"steve#ymail.com",
"password_digest"=>"password",
"specialty_id"=>262,
"doctor_insurances_ids"=>["44", "47"]}
DoctorProfile.create(params)
end
You're not putting a doctor_insurance_id in your Doctor Profile so your DoctorProfile.create(params) line isn't going to work. You could do something like this:
def create
doctor = DoctorProfile.create(doctor_profile_params)
params["doctor_insurances_ids"].each do |x|
DoctorInsurance.create(doctor_profile_id: doctor.id, insurance_id: x)
end
end
def doctor_profile_params
params.require(:doctor_profile).permit(:first_name, :last_name, :email, :password_digest, :specialty_id)
end
Here is some simplified code:
class Page
include Mongoid::Document
has_many :content, class_name: 'Content', dependent: destroy, autosave: false
accepts_nested_attributes_for :content
field :title, type: String
end
class Content
include Mongoid::Document
belongs_to :page
field :text, type: String
end
class PagesController < ApplicationController
def update
#page = Page.unscoped.find(params['id'])
#page.assign_attributes params.require(:page)
puts #page.reflect_on_association(:content)
# {:relation=>Mongoid::Relations::Referenced::Many, :extend=>nil, :inverse_class_name=>"Page", :name=>:content, :class_name=>"Content", :dependent=>:destroy, :autosave=>false, :validate=>true}
end
end
I have a controller spec that creates a page and associated content from factories and tries to patch the page. Whatever I send in page paramas (just the title or with content_attributes) it never saves anything, just as expected.
If I test this in dev, when the params consist just of Page.title, nothing is updated; if there is content attributes array in the params, the content gets updated.
The only thing I want is to assign new params to Page and associated objects, and then later to either manually persist or reject. What am I doing wrong?
rails 4.1.9
mongoid ~> 4.0.0
===
Update:
In my real project Content model, the field :text is localized; after I changed it to a simple String field, the spec fails as well (persists the field even though the autosave is false and save was never called).
try adding validates_associated :page to Content model
I have a callback of a model that needs to create a dependent object based on another field entered in the form. But params is undefined in the callback method. Is there another way to access it? What's the proper way to pass a callback method parameters from a form?
class User < ActiveRecord::Base
attr_accessible :name
has_many :enrollments
after_create :create_enrollment_log
private
def create_enrollment_log
enrollments.create!(status: 'signed up', started: params[:sign_up_date])
end
end
params are not accessible in models, even if you pass them as a parameter then it would be consider as bad practice and might also be dangerous.
What you can do is to create virtual attribute and use it in your model.
class User < ActiveRecord::Base
attr_accessible :name, :sign_up_date
has_many :enrollments
after_create :create_enrollment_log
private
def create_enrollment_log
enrollments.create!(status: 'signed up', started: sign_up_date)
end
end
Where sign_up_date is your virtual attribute
params will not be available inside the models.
One possible way to do this would be to define a virtual attribute in the user model and use that in the callback
class User < ActiveRecord::Base
attr_accessible :name,:sign_up_date
has_many :enrollments
after_create :create_enrollment_log
private
def create_enrollment_log
enrollments.create!(status: 'signed up', started: sign_up_date)
end
end
you will have to make sure that the name of the field in the form is user[:sign_up_date]
I am completely new to ActiveAdmin and RoR and i cant figure out how to change the visible value of the dropdowns in a has_many association.
Fillup Model
class Fillup < ActiveRecord::Base
// key is car_id:integer
belongs_to :car
end
Car Model
class Car < ActiveRecord::Base
validates :description, :presence => true
key is fillup_id:integer
has_many :fillups
end
What it currently shows:
It currently shows im assuming an encoded reference to the Car assigned to it.
What i need it to show:
I need it to show the description given which is defined as description:string in the Car Model.
Something like this should work...
In app/admin/model_name.rb
form do |f|
f.inputs "My Model Name" do
# add your other inputs
f.input :cars, :collection => Car.all.map{ |car| [car.description, car.id] }
f.buttons
end
end
Read this article to learn more about modifying the form.
AciveAdmin uses formtastic, you should read about that as well.
In your Car model, just add something like :
def to_s
description
end
It should do the job !
Explanation : Actually, your Car's to_s method returns the object id corresponding to the current instance, that's the default thing used when using a method like puts on an object. To replace a model's display name, you have to override this method and it will work anywhere in your app when you use puts #car or in your templates doing <%= #car %>