torsdag 17 oktober 2013

has_many-relations and nested models



Ruby on rails has a very easy convienent structure for building relations between objects of different classes. For example if you want to make it possible to relate many comments to many blogposts you can just:
  • Add a has_many statement to the Post model
  • Add a belongs_to statement to your Comment model

I tested out this specific example through following Getting started with Rails tutorial.


But for my Cookbook-app I wanted the possibility of adding a recipe-object and all the ingredient-objects in that recipe within the same form:

 After some reading on this problem i found out that you can achieve this by using "accepts_nested_attributes_for. By putting "accepts_nested_attributes_for :ingredients" in to the recipe model I make this nestled form possible. Then you can use the field_for method in my View to add new recipes to be able to add atrributes to the associated ingredients for that recipe in that same form.

 Then i got stuck for a little bit, thinking too much about creating a controller for ingredients. In the end it turns out that i can do all I need from withing the recipe controller. First I needed to update the recipe method new. Since new is passing POST information to the recipes/new-view:
  
def new
    @recipe = Recipe.new
    3.times { @recipe.ingredients.build }
  end

This way everytime I create a instancevariable containing a new recipe-object i also associate three ingredient-object with it. Then in the view, the field_for method creates input rows for each of these ingredients.

Next I needed to add the ingredients attributes to the recipe controllers create-method:

  def create
    @recipe = Recipe.new(params[:recipe].permit(:title, :desc, ingredients_attributes: [:name]))

    if @recipe.save
      redirect_to @recipe
    else
      render "new"
    end
  end

I got stuck on this for a while cause the way of doing this is different from rails 3 and rails  4 which I use, so most of the info i find online was missleading. This is something to consider for future problems!

This is a super useful railscast on the subject:
 





tisdag 15 oktober 2013

Ruby on Rails tutorials

I went back and re-did some of the popular web tutorials for ruby on rails that are out there.


http://tryruby.org:
A tutorial for the syntax in ruby. You dont need to know any programming at all for this. For me it was a bit to basic...

http://railsforzombies.org:
Super useful tutorial that explains the model, controller, view - structure of ruby on rails with video lectures and exercises. Recommended!

lördag 12 oktober 2013

Cookbook app idea

Ok so I have an idea for a new project and this what I am going to try to do

Cookbook

Description: A digital cookbook to collect favorite recipes.


Functions: 
  • Signed in users can add, edit, show and destroy their own recipes. Visitors can only show recipes.
  • Recipes contains: Author, Picture, Title, Description, Ingredients, Quantities, Comments, Tags.
  • Search functions + listings
  • Possibility to mark the recipes you want to cook and get them to list nicely on a shopping list. Quantities of milk for example should be summarized.
  • Shopping list can be downloaded somehow?
  • The add-form for should be smart (propose ingredient name so that when you typ “mi” milk comes up as an alternative.
  • Mobile-friendly?
  • Nice layout and CSS-stylesheet
  • Validations

First encounter with Ruby on Rails...


Yesterday I started working a bit with Ruby on rails. I followed this tutorial to set up a basic blog with Create, Read, Update, Destroy functionality. To make myself think and not just copy the tutorial I made a list of recipes instead of a blog.
I also added some validation features so that you cannot add recipes without title or description

Some thoughts so far:

There is a lot going on under the hood:
I am still kind of amazed how RoR can actually work. It seems that many times it just reads your mind?! The downside of this is that once I got stuck it was not as easy a PHP for example to just hardcode/comment out something/test and revise. For example partials, how can that work?!

I do not understand Ruby syntax that well:
I got stuck for an hour or so on a line of code that returned an error and tried everything until I understood that there was actual a typo in the tutorial. RoR is complex enough that if I am going to learn in I have to get more comfortable with ruby

Idea for project:
Got an idea on how to do a Cookbook with some cool functionalities.

onsdag 9 oktober 2013

Get up and running with ruby on rails

I first started out using ruby on rails earlier this year and it was a real hassle to get it all working.

This time around on another computer it was easier since i knew how to do it. Here are some tips for setting up ruby on rails on OS X Mountain Lion.

Start with getting Xcode, Homebrew, RVM and finally lastest versions of both ruby and rails.
I used this guide: http://www.interworks.com/blogs/ckaukis/2013/03/05/installing-ruby-200-rvm-and-homebrew-mac-os-x-108-mountain-lion

Next, get TextMate. Its a great editor for working with Ruby on Rails. Here is a good video on how create your first RoR-app and open it as a project in Textmate:


Next step, install Git. Git and Github are useful tools for back up you project as you progress. Its even more useful if you are more then one person working on the code. Follow the "Step 4 Install Git" in this tutorial: http://www.moncefbelyamani.com/how-to-install-xcode-homebrew-git-rvm-ruby-on-mac/

Next step, read section 1.3 in this online book on how to set up Git and GitHub: http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#sec-version_control

Pow is great Rack server that you can use to test out your applications locally and its super easy to set up. Got to http://pow.cx/ and scroll to the bottom of the page to find instruction on how you set it up in less then a minute!

Thats it!



Let´s Go

Hi, I am Evert and I study Media Technology at KTH in Stockholm. I´m now in my fourth year and starting my masters in Human-Computer Interaction.

While studying at KTH so far has been interesting and sometimes fun I still feel like the guy in the video most of the time…..




So!!

To put use to what I’ve learned so far, I am starting to do some simple programming outside of school. This blog will be sort of a daily diary of what I am programming and learning, mostly just for myself.

Right now I am trying to learn Ruby on Rails and LaTeX, so posts will be mostly about that.