Rails regex date validations in rails 2.3 - ruby-on-rails

I've created a view that has two input's in it, both dates, and I want the user to type them in a specific format. I created it as an index rather than a form, long story. I'm trying to use Regex to validate the date's the user types in. I want the dates to be in the format of YYYY-MM-DD, so if the user types in a date in a different format it causes an error. Should this regex be placed in the controller or in the model? I've tried validates_format_of in the model but it doesn't seem to be working. I'm also on rails 2.3 and ruby 1.8.7.

validates_format_of :date, :with => /^\d{4}-\d{2}-\d{2}$/
you saying validates_format_of doesn't seem to be working. What does it mean? Please give more information.

Related

What is the robust way to check form input values in Rails

I have a form and want the form input values to be check. If the values pass the check, then values can be stored in DB. I know I can check them in view, controller, or even model. I think probably the best way is to check them in all the three parts, and only check them in view before submit could cause problem because others can manually modify and send the request to the controller.
So for example, I have user variable in view. I don't want date type attribute user.start_date to be modify to be earlier than user.apply_date, how should I make the protection robust?
What you're looking for are model validations: http://guides.rubyonrails.org/active_record_validations.html
Check out the date_validator gem (https://github.com/codegram/date_validator)
It allows for validations like this:
validates :start_date, date: { after: :apply_date }
Doing it in the model, as that's the usual way. Ruby on Rails supports model errors extensively, and if there's an error you can simply redisplay the record and the errors in that record.
There are standard validations you can do to ensure a value is present, is unique, etc.
You can also do custom validations in the model
validate :start_date_cannot_be_before_apply_date
def start_date_cannot_be_before_apply_date
if start_date < apply_date
errors.add(:start_date, "can't be before the apply date")
end
end

Adding Default Value to Column Depending on the Value of Another Field in A Model In A Rails 3.2.13 Database Migration

I already have a date field (string) in my model. I plan to add other string fields with different long date formats to use in a text search related to the existing date field. This is to facilitate text searching for the visually impaired.
I have done something like this before which worked just fine.
add_column :media_libraries, :media_year, :string, default: Time.now.year
However what I want to do is to add the date fields and default the new date field using the value of an existing date field.
I have the code below in one of my views which has the field I want to use to initialize the value of the new long date fields.
media_created.to_date
Can I do something like this in my migration where I can set a default value depending on the value of another field on the record?
add_column :media_libraries, :media_created_en, :string, default: :media_created.to_date.strftime("somedateformat")
I have read another post add a database column with Rails migration and populate it based on another column where one of the answers suggested a query within the migration. This would be my attempt to implement that. However others posting on Stack Overflow feel that it is not a good idea to reference other fields in a migration. For me since this is a one-time thing I'm not worried about those issues.
MediaLibrary.update_all(media_created_en: :media_created.to_date.strftime("%B %d, %Y"), media_created_fr: :media_created.to_date.strftime("%d %B %Y"), media_created_pt: :media_created.to_date.strftime("%d %B %Y"), media_created_es: :media_created.to_date.strftime("%d de %B de %Y"))
I want to do this for existing records since I have over 1000 records to update. I will either put code in my model or my controller to set the fields for future updates to the model.
I have looked everywhere but can only find built-in functions used for default or the one example above. The API says nothing other than 'options{}'. If this cannot be done I am ready to write a method to do this one time in the admin section of my application.
Any help will be appreciated. I will keep searching.
I kept researching and found nothing so I decided to try a few things in the migration. I was not able to successfully incorporate a field on the model in my default values. Since I was in a time crunch I decided to just create the fields in the migration then wrote a method in the controller to update the fields on my model. Now I will wrote the code to update the fields for records being added to make sure the fields are updated properly.
If anyone has a solution for this please post it here but I'm moving on.

Ruby on Rails timedate field for standalone form

I have a system which handles reservations. I am writing the UI for a user to select a start and end date which is submitted to the rooms controller and returns all available rooms for those dates. I'm struggling with
The actual form
Processing the dates in the rooms controller.
Having used form_for a lot, I know how datetime_select works, but using form_tag I can't seem to find how to implement the same thing.
Also, it looks as though using datetime_select :year, :start_year passes the date to the controller in 5 parameters, which is going to be messy to parse.
I wonder if I configure the form properly, the controller will know how to parse the date natively and automatically solve the second problem
Thanks for your help!
Check out this answer if I understand your question correctly then you can implement it in the same way as you would using form_for.

How do you internationalize the message being thrown

class Foo < ActiveRecord::Base
...
validates_presence_of :name, :message => "cannot be blank.
...
I am looking for ways to internationalize the message so that a localized message is shown from users from a different locale. How should I do that?
Rails Guides: Translations for active records will give you a hint about it. Basically, you don't have to store the string in your model. You can change it in your locale files.
I18n is one of the good way to internationalize the message.
But if your message customization level is too much then every time you need to modify the ymls and also need to add new yml for different set of users.
In such case you can use 'redis' it gives you more dynamic control. It store data in key => value hash. It is really fast. Following links will help youl
http://redis.io/documentation
http://jimneath.org/2011/03/24/using-redis-with-ruby-on-rails.html
https://github.com/jodosha/redis-store
http://www.engineyard.com/blog/2009/key-value-stores-for-ruby-part-4-to-redis-or-not-to-redis/

Validation of Tag length

For acts-as-taggable-on, how do you make Tags be a certain amount of characters? I want users to only be able to have a maximum of 50 characters when they are creating tags.
Thank you.
You can use a validation for this. Try using this in your model. Replace :tag_name with the correct field.
validates_length_of :tag_name, :maximum=>50
Also an awesome reference to rails validations is here.

Resources