Reputation: 6841
I am using 'Rails Admin' in my application. In my initializer I have the following in 'rails_admin.rb':
config.model "Player" do
edit do
:name
:team
:shooting_percentage
end
end
That gives me the following fields on the edit view in Rails Admin: name, team, and shooting percentage.
I would like to add two fields to the edit form in Rails Admin for Player but I don't want to make them as attributes on that model (or any model for that matter). I want to add shots, and goals as just form fields.
The reason I want to add those two form fields is so that I can use them to calculate the 'shooting_percentage' attribute value on Player.
Is there a way in Rails Admin to have fields on a form that are not fields on the model? Furthermore, if that is possible is there a way to use the values entered into those form fields to calculate a value for an actual model field (shooting percentage)?
Upvotes: 1
Views: 2618
Reputation: 1585
Here is how I would do it:
class Player
include Mongoid::Document
field :name, type: String
field :team, type: String
field :percentage, type: Float
attr_accessor :shots, :goals
before_save do
self.percentage = shots.to_f / goals.to_f
end
rails_admin do
configure :percentage do
read_only true
end
edit do
include_all_fields
field :shots, :integer do
html_attributes min: 1, required: true
help 'number of shots'
end
field :goals, :integer do
html_attributes min: 1, required: true
help 'number of goals'
end
end
end
end
Upvotes: 1