Leave a comment

How to Set Up Flash Hashes and Error Messages in Rails


Bob Roberts:

So simple but so many do not use this. We have a legacy app that has NO error messages. Try to login with wrong email, it just does not work but no message to tell you why. Seems like this should be a standard thing.

Originally posted on christopher r dodd code:

Notices

1. Setting a notice

Setting a flash notice is as easy as assigning it to the flash object

flash[:notice] = "The subject was created successfully"

Where should you put it?

A good place to put it is in the controller after the object successfully saves or is destroyed. For example:

def create
  @subject = Subject.new(subject_params)

  if @subject.save
    flash[:notice] = "Subject '#{@subject.name}' created Successfully"
    redirect_to root_path

  else
    render('new')
  end
end

2. Displaying the notice

If using Bootstrap and HAML, you can put this in the view where the user gets redirected to

-if !flash[:notice].blank?
  .row
    .notice.alert.alert-success
      =flash[:notice]

If you’re not using Bootstrap or HAML, try this:

<% if !flash[:notice].blank? %>
   <div class="notice"><%= flash[:notice] %></div>
<% end %>

Errors

1. Write your validations

For there to be errors, you need validations. If you haven’t built validations, I suggest you do this now.

2. Generate a partial for displaying your…

View original 62 more words

1 Comment

Mongodump cron job for nightly backup


I recently looked for a way to automatically backup Mongodb database nightly.  There are some nice bash scripts out there but I wanted to just do it in a cron job.  After some struggling with taring the huge db directory I came up with this:

0 0 * * * /bin/bash -l -c ‘cd /my_project_path && mongodump –host 0.0.0.0 -d mydb –username mayusername –password mypassword –out /var/dbbackups/backup_$(date +\%Y\%m\%d) && cd /var/dbbackups && tar -zcf backup_$(date +\%Y\%m\%d).tar.gz backup_$(date +\%Y\%m\%d)/mydb

The first part is the cron for midnight daily:

0 0 * * *

Then it cd’s into the rails project directory and gets ready to run bash:

/bin/bash -l -c ‘cd /my_project_path

Then mongodump adding the date on the dump directory:

mongodump –host 0.0.0.0 -d mydb –username mayusername –password mypassword –out /var/dbbackups/backup_$(date +\%Y\%m\%d)

And finally, cd into that directory and tar (compress) the directory:

cd /var/dbbackups && tar -zcf backup_$(date +\%Y\%m\%d).tar.gz backup_$(date +\%Y\%m\%d)/mydb

Notice that I added the database name on the directory to tar.  Mongodump will create the backup_20150429/mydb directory with all of the .bson files in it.

Leave a comment

Learning Ruby (and why I should have learned it earlier)


Bob Roberts:

I learned Rails from a Rails expert that did not know Ruby very well :( I learned so much Ruby just from messing around in Rails console.

Originally posted on Erik Jenks:

The Internet has said it over and over. Did I listen? Not right away. The almighty Interwebs have said “LEARN RUBY FIRST!” Nah, I do things my way, I do what I want. I should have listened.

I spent the past week and a half sitting down, learning the basics of Ruby, and guess what. Rails makes SO MUCH MORE SENSE NOW! So I should have listened to the wise words of those who have come before me.

Actually, I think that attempting to learn Rails before Ruby helped some. I understood some of the concepts, but then after diving deep into Ruby it all started to click.

The use of hashes, arrays, classes, and all the verbiage that came with Ruby became a snowball in my mind. Once the ball began rolling I was able to comprehend even more. I went back through some of my projects that I…

View original 149 more words

Leave a comment

Railsbricks Setup


Bob Roberts:

Cool cool find. Kudos to Nico Schuele for creating this gem

Originally posted on Troy Leak:

Today I found out about railsbricks. It’s a ruby gem that does the complicated task of setting up trivial functionality for your site like user logins, database configuration, admin pages, and other things like that. Extremely helpful in that endeavor was this blog post, which helped me parse some of the options I was presented with.

I did run into one problem that was not covered in his post, however. It seems the team that makes Postgresql keep moving around their psql.conf file and that causes your application to flip out without some extra configuration. As this was a test application, I went with SQLite3 for my database for simplicity’s sake.

Overall, though, railsbricks is another extremely helpful tool in creating rails apps quickly and conveniently.

View original

Leave a comment

High Charts Interactive Grids


Bob Roberts:

We use High Charts as well. Excellent tool imho

Originally posted on Stephen Emo's Blog:

Now that my web site has been live for a couple of months I have had time to go back and see some improvements I can make. The first thing I noticed was regarding the Highchart graphs, I noticed that you could filter out the lines of the graph that you where not interested in by clicking on the related legend. So I updated the graph so that clicking on it does not move you to a new page which meant this useful dynamic legend filter functionality could be used. You can still move pages by clicking on the left hand side grid boxes

Interact with the High Charts Graphs Interact with the High Charts Graphs

The second thing I noticed was that smaller towns ended up with the highest price. This was due to the low volumes of sales in the area. This meant a single site valued at 600,000 pounds would give high average…

View original 24 more words

Leave a comment

Refactor: A clever way to DRY up your create action


Originally posted on Dear Programmer:

Anyone that has spent at least 5 minutes with Ruby on Rails will recognize the following code:

class PersonController

  ...

  def create
    if person.save
      flash[:success] = 'Person saved!'
      redirect_to person
    else
      flash[:error] = 'Person not saved.'
      render :new
    end
  end

  ...

end

It’s probably the single most common method in a rails controller and since it follows the same pattern in each controller, it’s ripe for DRYing up!

Here’s how:

In your application_controller.rb add the following method:

def save_and_redirect(new_object, success_msg, error_msg, redirect_path)
  if new_object.save
    flash[:success] = success_msg
     redirect_to redirect_path
  else
    flash[:error] = error_msg
    render :new
  end
end

Then in your controllers call the method like this:

def create
 save_and_redirect(person, 'Person added!', 'Person not added.', person)
 end

And that’s how it’s done.

Also, in case anyone was wondering where my @person = Person.new(params[:person]) went and why I don’t use instance variables; I use Decent Exposure. Check it out!

However, the above…

View original 47 more words

Leave a comment

Rails 4: Flash message persists for the next page view


Originally posted on Ruby on Rails Developer Community:

The flash variable is intended to be used before a redirect, and it persists on the resulting page for one request. This means that if we do not redirect, and instead simply render a page, the flash message will persist for two requests: it appears on the rendered page but is still waiting for a redirect (i.e., a second request), and thus the message will appear again if you click a link.

To avoid this weird behavior, when rendering rather than redirecting we use flash.now instead of flash.

The flash.now object is used for displaying flash messages on a rendered page. As per my assumption, if you ever find a random flash message where you do not expect it, you can resolve it by replacing flash with flash.now.

View original