I'm building a form on ROR4, using the simple_form gem, to modify my user's data. The thing is that my database is normalized to have the user's first and last name in lower case. So I have the following in my User model:
before_save :lowercase_names
def lowercase_names
self.first_name.downcase!
self.last_name.downcase!
end
But of course when I populate my upadate form with the user object I get the following:
First name: carlos
Last name: ledezma
I was wondering if there is a way to override this behavior so Rails would print instead:
First name: Carlos
Last name: Ledezma
That is, the titleized version of the fields.
Thanks in advance for the help
In your form set the value to titleized name
=f.input :first_name, :value => #user.first_name.titleize
The above line will vary depending on if you are using simple form or not. But it will give you the basic idea. You are overwriting the value of the input field
If you want it to be changed everywhere, then override getter for first_name and last_name
def last_name
self.read_attribute(:last_name).titleize
end
def first_name
self.read_attribute(:first_name).titleize
end
Be aware that this will titleize your first name and last name everywhere you call the getter
Try using the value attribute of the text_field helper in your form to set the shown value as the capitalized first and last name:
<%= f.input :first_name, :value => f.object.first_name.capitalize %>
<%= f.input :last_name, :value => f.object.last_name.capitalize %>
Updated
Also, you can override getter method in User model like:
def first_name
self.read_attribute(:first_name).capitalize
end
def last_name
self.read_attribute(:last_name).capitalize
end
Related
I need to overwrite any blank field submissions in my simple form with the text value "N/A". I've found how to set a default value beforehand, but I would like the field left blank for users to fill out and submit, and only changed if they leave it blank. Is this possible? Thanks!
<%= f.input :survey_year, :input_html => { :value => 'N/A'} %>
Should do the trick. See the flipflops.org link in my comment above for alternative approaches.
Try to the following
before_save :default_values
def default_values
self.name ||= 'N/A' #=> note self.name = 'N/A' if self.name.nil?
end
When a user submits a form with blank/nil name then it will submit to "N/A" otherwise none
I have a permit and vehicle model. I am trying to update the AA create controller to work how I have it in my rails app. That is taking the vehicle license_number entered and inputting it into the permit table, then also taking the inputted permit_id and inputting it into the permits attribute of the vehicle it is related to in the vehicle table.
admin/permit.rb
permit_params :permit_id, :vehicle, :date_issued, :issued_by, :date_entered, :entered_by
form do |f|
f.inputs do
f.input :permit_id
f.input :vehicle, :collection => Vehicle.all.map{ |vehicle| [vehicle.license_number]}
f.input :date_issued, as: :date_picker
f.input :issued_by
end
f.actions
end
controller do
def new
#permit = Permit.new
#vehicle = #permit.build_vehicle
#vehicle = Vehicle.all
super
end
def create
vehicle = Vehicle.find_by(license_number: permit_params[:vehicle_attributes][:license_number])
#permit = current_user.permit.build(permit_params.merge(date_entered: Date.today,
entered_by: current_user_admin.email))
super
end
end
My errors that I am getting, is that it is inputting the license_number in for the permit_id and then it is also saying the permit_params is not a defined variable. Any help would be great, thanks!
You have an interesting case here: it is confusing because you have a model called Permit, and usually in Rails you name the params method something like permit_params. Turns out, permit_params is the general method signature for ActiveRecord to implement strong params: https://activeadmin.info/2-resource-customization.html
With that, instead of calling permit_params in your create action, you need to call permitted_params[:vehicle_attributes][:license_number]. That’s why it’s considering permit_params as an undefined variable. Again, those two method signatures will be the same for all your ActiveAdmin forms.
Additionally, I’m not sure if this is a typo but you define #vehicle twice in your new method. I’m not sure you need to build a vehicle for the permit form unless you’re doing nested forms. Either way, I think the last line should read #vehicles = Vehicle.all Then you can use that in your form, which also could use an update in the collection part of your form field:
form do |f|
f.inputs do
f.input :permit_id
f.input :vehicle, collection: #vehicles.map { |vehicle| [vehicle.license_number, vehicle.id] }
f.input :date_issued, as: :date_picker
f.input :issued_by
end
f.actions
end
The collection_select form tag will take the first item in the array as the value that appears in the form, and the second value will be the value passed through in the params (https://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select).
Then in your create action, you can find the Vehicle with the id:
Vehicle.find(permitted_params[:vehicle_attributes][:vehicle_id])
I would avoid Permit as a model name, try using VehiclePermit.
I have a form_for for creating a new record. I have set getter and setter methods to access form field in my view. Below are my getter ans setter methods with my form view,
Getter & Setter Methods respectively :
def manufacturer_model_name
self.manufacturer_models.pluck(:name).join(', ') unless self.manufacturer_models.blank?
end
def manufacturer_model_name=(names)
names = names.split(',').map{|n| n.strip}.delete_if(&:empty?) if names.present?
names.uniq.each do |name|
id = ManufacturerModel.where(:name => name, :manufacturer_id => manufacturer.id).first_or_create.id
if self.new_record?
self.user_skill_manufacturer_models.build(:user_skill_id => self.id, :manufacturer_model_id => id)
else
self.user_skill_manufacturer_models.where(:user_skill_id => self.id, :manufacturer_model_id => id).first_or_create.id
end
end if names.present?
end
Form View:
= f.text_field :manufacturer_model_name, :class => 'form-control
My problem is that, my input field is autocomplete with multiple set to true, to get multiple values. when user enters multiple comma separated values and submits form and if there are any errors on the form, my new action is rendered and user losts all the entered values forcing him to reenter all again. How can I solve this problem?
It would be better to make a manufacturer_model_name_form field or some such via attr_accessor, and then parse that in validate. It would look something like this:
class ManufacturerModel < ActiveRecord::Base
attr_accessor :manufacturer_model_name_form
validate :validate_manufacturer_model_name
def validate_manufacturer_model_name
errors.add(:manufacturer_model_name, 'not a valid manufacturer model name') if !!true # perform your validation here in place of `!!true`
end
end
Then, in your form, you would use manufacturer_model_name_form instead of manufacturer_model_name:
= f.text_field :manufacturer_model_name_form, :class => 'form-control'
I'm trying to add an auto complete field using https://github.com/peterwillcn/rails4-autocomplete
I have a Topic model that belongs to a City. City belongs to Country. i.e. city = London. then city.country.name = England.
I can get my autocomplete to display the city name with
#controller
class TripsController < ApplicationController
autocomplete :city, :name
#view
<%= f.autocomplete_field :city_id, autocomplete_city_name_trips_path %>
But I want the country name to show in the auto complete field too. i.e. London, England. not just London.
Is there a better way to do this?
EDIT: also this passes the city name to the controller, I would like it to pass the city_id
If you've got the country in another field then it is pretty easy with autocomplete. You want to use the update_elements option in your view to tell autocomplete what other fields need to be filled in.
<%= f.autocomplete_field :city,
autocomplete_department_name_incidents_path,
update_elements: {country: '#trips_country'} %>
The #trips_country in the above snippet is the ID of the field in your form that you want updated. country: is extra data that autocomplete fetches for you.
So you also need to go to your form and tell autocomplete to grab other data.
autocomplete :city, :name, extra_data: [:country]
That's what would work if everything were in the same model. I've never tried to pull data from a different model using autocomplete so I don't know if it will work. Hopefully this is enough of a push to get you there.
The rails4-autocomplete gem has the option display_value which is what you want to use.
class TripsController < ApplicationController
autocomplete :city, :name, display_value: :name_with_country
Then in the City model
def name_with_country
name + ', ' + country.name
end
I have a "Word" model that has 3 string variables: "word_a" , "word_b" , "word_ab".
A form in my view collects the values for "word_a" and "word_b":
<%= f.text_field :word_a %>
<%= f.text_field :word_b %>
What is the best way to save the value for "word_ab" which will be made automatically from a combo of "word_a" and "word_b"?
The way Im doing it now seems really really wrong. After submitting the first form, I have the controller redirect to another edit page with a 'word_ab' form that has a value that combines 'word_a' and 'word_b'.
<%= f.text_field :word_ab, :value => #word.word_a+"_"+#word.word_b %>
The user then has to resubmit the form to save 'word_ab' into the database. Can't I do this is a controller?
Are you sure you want to save concated value to db? What about editing?
If you still want to do it - best idea is to add attr_accessor in model
attr_accessible :word_a, :word_b
attr_accessor :word_a, :word_b
First line allows yo perform mass assign, second one creates setter and getter methods.
Then, still in a model do
before_validation(:on => :create) do
self.word_ab = word_a + word_b
end
You may perform this on before_save as well, and you may validate word_a and word_b separately with regular validators.
Pro tip: create getter method that returns concated string
def word_ab
self.word_a + self.word_b
end
Associated models
class User
has_one :profile_picture
def word_ab
self.profile_picture.url + self.word_a + self.word_b
end
end