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

Leave a comment

What Ruby On Rails is, and the only other three things you really need to understand to get started


Bob Roberts:

My favorite quote:
“The terminal is a piece of voodoo”
I totally felt that way the first time in a console!!

Originally posted on Ruby Warriors:

What is Ruby On Rails?

Ruby is a programming language. Hardcore code – the stuff that’s impossible to understand for an untrained person. You can do stuff with this language; anything, really. You can program stuff, write software with it etc.

Rails is bit like a piece of software you use to

View original 530 more words

Leave a comment

2014 in review


The WordPress.com stats helper monkeys prepared a 2014 annual report for this blog.

Here’s an excerpt:

The concert hall at the Sydney Opera House holds 2,700 people. This blog was viewed about 13,000 times in 2014. If it were a concert at Sydney Opera House, it would take about 5 sold-out performances for that many people to see it.

Click here to see the complete report.

Leave a comment

Creating a Simple Form with Rails


Bob Roberts:

This is a great write up that I thought that I would share ;)

Originally posted on Cindi Writes Code:

Form helpers are designed to make working with resources much easier compared to plain HTML.

http://guides.rubyonrails.org/form_helpers.html
http://api.rubyonrails.org/ – search for “form_for” to find under ActionView::Helpers::FormHelper

We can use a form builder to create a form within an .erb template. The main form builder for Rails is provided by a helper method called “form_for”.  Example code:

The form_for helper creates a form that allows the user to create or update the attributes of a specific model object.

The method can be used in several slightly different ways, depending on how much you wish to rely on Rails to infer automatically from the model how the form should be constructed. For a generic model object, a form can be created by passing form_for a string or symbol representing the object we are concerned with.

In the example above, we are passing in the article object (“:article”).

The variable f yielded to the…

View original 977 more words

Leave a comment

Pretend to generate without really generating!


Bob Roberts:

Hmmmm, I never knew this :(

Originally posted on Ruby Warriors:

When running ‘rails generate’, you can add a -p (pretend) flag at the end of the command to see what the output will look like, without actually running it. Very handy!

View original 72 more words