Ruby on Rails Tips including general tips and specific coding methods.
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”.
I found a great cheatsheet for Rails at
It’s definitely worth a look
There are times when I want to remark out code in a rails view for testing purposes.
I tried using the =begin / =end that can be used in a controller but I couldn’t get that to work. i.e Continue reading
I found a helpful summary of coding styles for rails.
When you create a Rails project, it is helpful to put notes in the project. The notes can look like the following in your controllers:
# TODO change the alignment of the column
# FIXME – User password retrieval is not working as expected
# OPTIMIZE Add indexes
Or, in the case of views, use the following format Continue reading
link_to can be modified to show a button instead of a text link. The trick is to enclose the link/button text in a button tag and add .html_safe .
<%= link_to ““.html_safe, ‘http://blog.chrismendlatech.com’, target:’_blank’, class: ‘button’%>
<%= link_to "< button>My Blog< /button>".html_safe, 'http://blog.chrismendlatech.com', target:'_blank', class: 'button'%>
If you are using form tags in a ruby view, they need to be outside of any table elements or the form/submit button will not work properly.
The following will work:
<%= form_tag documents_path, :method => ‘get’ do %>
<%= text_field_tag :search, params[:search] %> <%= submit_tag “Search”, :name => nil %>
<% end %>
I screwed up my droplet (ruby on rails) on my digitalocean hosting by running bundle install as root. When you do that, there is a warning that this will break rails for other users. I haven’t taken the time to figure out how to fix that. Since I was getting started with that hosting, I decided to simply delete the existing droplet and recreate it.
However, since I do need to use root from time to time, I made it a standard procedure to always run whoami before running any bundle commands. That way, I can make sure I am logged in as rails and not root.
I needed a way to show the status (Up/down) of internal Ruby applications. I found an answer on StackOverflow that pointed me in the right direction (Apologies that I can’t locate that post). . I modified that somewhat by putting it into a method and passing the site name to it.
If you put the following code in a controller, and all it as indicated in the remarks from your view, it will check the site and respond with UP or down. I have a list of about a half dozen applications in a table with a status column. Continue reading