Ruby on Rails Tips including general tips and specific coding methods.
If you need to get the IP of a user, you can simply use request.remote.ip
ip = request.remote_ip
If you need to get the user browser, you can use the following
user_browser = request.env[‘HTTP_USER_AGENT’]
Of course, the results would be stored in ip and user_browser respectively. Continue reading
There will be times when you might want to change the shading of a cell in a table depending on the value of that cell.
I wanted to change the background color of a cell depending on if the contents of the expires field was past or not. This could be useful for dashboard type apps or pages. i.e. red/yellow/green
index.html Continue reading
There can be times when you want to show a field, or do some other action, only for a certain controller action such as ‘new’.
In this example, we have a form and we want to the currently logged in user (@current_user_name in this case) if this is a new complaint. However if we are editing we want to show the name of the user in the user table (@user.name) Continue reading
There are times when you need to store a hash/array in a field in a rails table.
By declaring a hash and putting it at the end of the params permit section, then the data from things such as a multi select will be stored as a hash. Adding =>  declares the field as a hash field.
:category => ,
:owner => )
I needed to remove users who were already assigned from a drop down list for assignments. This did the trick.
I use adauth to authenticate users in my Rails apps using their Active Directory information. A model and table called User(s) gets created.
However, the standard setup for Adauth does not grab the user’s email address as it appears in Active Directory. You can grab and store the email using the following method. Continue reading
Some dates will need to be stored in a date time format. This does not pose a problem when you are searching for dates equal to or greater than a specific date. You also avoid problems if you are storing dates in a date format.
However, if you search for a date less than or equal to a specific date using a datetime column, your results do not include that date. In other words, if you set up a search to show you everything where the date in question was less than or equal to 3/25/2016 you would not get any records where the date was 3/25/2016.
Some of the apps I do make a lot of use of the Rails flash notices. The default text attributes don’t make the notice stick out enough. I wanted to use a yellow background to highlight any notices.
Using the Inspect tool in chrome, I found that the controlling CSS was in assets/scaffolds.scss. In my case it happened to be around line 39
Look for #notice. The only attibute was color: green;. I changed that to add a yellow background color as follows Continue reading
I was working with a Rails app that pulled data from a non rails database.
Things were a bit quirky and I wanted to double check the type of the column. I found that putting the following in a view will work.
In this case, it returned “decimal”.