Feeds:
Posts
Comments

Archive for the ‘Days @ Slashprog’ Category

Greetings!

Today i learnt a bit on HTML, CSS and then proceeded with Chapter 10 of Learn Rails by Example

In HTML i worked with image tag <img> from  w3schools

I learnt the following things in <img> tag

  • <img> tag is used to insert an image into an HTML document
  • There is no closing tag for it.
  • Contains common attributes such as src, alt, width, height, border
  • src – specifies the path of the image
  • alt – specifies the alternate text for the image
  • width, height – specifies the dimension for the image
  • border – sets the border for an image

To insert an image into an HTML document, i just followed the code to display the image in browser

<html>
<body>

<img border="0" src="/home/rajee/animal.jpg" alt="Deer" width="100" height="100" />

</body>
</html>

Then i switched to learn CSS from CSS CookBook by Christopher Schmitt available in our library

The book covers nearly 500 pages

I set a goal to complete 5 pages per day.

Though its a too too small small goal, i want to follow it everday with good understanding of my workarounds. And this is my real goal 🙂 🙂

From yesterday i started to learn CSS

So, today i completed 10 pages from the book.

I came across the following two selectors in CSS cookbook

  • Type Selectors
  • Class Selectors

Type Selectors

It represents the HTML tags or the elements

Example

h1 { font-size:100%; }

body { background-color : red; }

Class Selectors:

It is used to apply the same CSS rule many times to different elements.

Example

<html>
 <head>
 <title>CSS Cookbook</title>
 <style type="text/css">
 .warning
 {
 font-weight:bold;
 }
 </style>
 </head>
 <body>
 <p>A para <a href="http://csscookbook.com">link</a>.
 <p>Second para illustrating the <em>class selector</em> example</p>
 </body>
</html>

Then i started to watch and work with the rails screencast

Previously, i stopped in quarter portion of Chapter 10 ( i.e., i just worked with first half an hour of the tutorial )

I resumed from where i left and watched next one hour lesson

In my workaround i learnt how to implement a bit of security model while updating users

The first step is to protect pages from unauthorised access by requiring signing in ( ie., if someone wants to access edit page, he/she should be redirected to sign-in page )

It is done using before_filter

Writing test cases in users_controller_spec.rb


describe "authentication of edit/update actions" do

 before(:each) do
 @user = Factory(:user)
 end

 it "should deny access to 'edit'" do
 get :edit, :id => @user
 response.should redirect_to(signin_path)
 end

 it "should deny access to 'update'" do
 put :update, :id => @user, :user => { }
 response.should redirect_to(signin_path)
 end
 end

Adding before_filter in users_controller.rb


before_filter :authenticate, :only => [:edit, :update]

def authenticate
 redirect_to signin_path unless signed_in?
 end

By 3.45p.m., we gathered for a SCRUM Meet and i told like as i exlpored on HTML,CSS,Chapter 10 of Learn Rails by Example

Will be continue to work with these technologies tomorrow 🙂

Read Full Post »

Greetings!

Today i started to watch Chapter 10 of Learn Rails by Example

Its a very long screencast approximately about 2 and half hours

I just watched and practised the first half an hour of the screencast

This lesson completes updating, deleting and showing users

The first step is to write test case for the presence of Edit page

Edit spec/controllers/users_controller_spec.rb


describe "GET 'edit'"do

 before(:each) do
 @user = Factory(:user)
 test_sign_in(@user)
 end

 it "should be successful" do
 get :edit, :id => @user
 response.should be_success
 end

 it "should have the right title" do
 get :edit, :id => @user
 response.should have_selector('title', :content => "Edit user")
 end

it "should have a link to change the Gravatar" do
 get :edit, :id => @user
 response.should have_selector('a', :href => 'http://gravatar.com/emails',
 :content => "change")
 end

 end

Add a edit action in app/controllers/users_controller.rb


def edit
 @user = User.find(params[:id])
 @title = "Edit user"
 end

Create a template to describe the edit form

The edit form is similar to new.html.erb.

It also contains a gravatar image link to change the image

edit.html.erb files looks like as follows


<h1>Edit User</h1>

<%= form_for(@user) do |f| %>
 <%= render 'shared/error_messages' %>
 <div>
 <%= f.label :name %><br />
 <%= f.text_field :name %>
 </div>


 <div>
 <%= f.label :email %><br />
 <%= f.text_field :email %>
 </div>

 <div>
 <%= f.label :password %><br />
 <%= f.password_field :password %>
 </div>

 <div>
 <%= f.label :password_confirmation, "Confirmation" %><br />
 <%= f.password_field :password_confirmation %>
 </div>

 <div>
 <%= f.submit "Update" %>
 </div>

<% end %>

<div>
 <%= gravatar_for @user %>
 <a href="http://gravatar.com/emails" target=_blank>change</a>
</div>

Will be continued working with Chapter 10

Read Full Post »

Hai,

Today we ( teammates ) spent time with our mentor Chandrashekar sir 🙂

We discussed with the user stories for our next project

While talking about it, our sir mentored us in a topic, “Time Management

He told about how to manage the time to make us productive on every day.

I recollected my friend, Shrini’s words, who use to mentor me then for a good career

Iam impressed by the following lines from our sir’s talk

If there is no one to assign a work for you, do self-plan and assign work for ourselves, so that you will get more interested to do it, rather than for others

Thanks for a wonderful talk sir

After the  class gets over, i started to watch Chapter 10 of Learn Rails by Example

This lesson is nearly 2 and 1/2 hour video

I have watched only one hour video but didnt work on it.

Hope to watch and work it out on the next working day

Read Full Post »

Hai,

Today i completed watching and working with Chapter 9 of Learn Rails by Example

I learnt on how to generate a successful Sign up, Sign in and Sign out methodologies

But i faced show stoppers in this lesson.

Hope, it will get solved working with it one more time and reading the book

The first step in  the development of Sign in and Sign out is to generate a session controller

Generate session controller


rails generate controller Sessions new

Modify the routes.rb The next step is to modify the routes.rb file to include the resource, sessions

resources :sessions
match '/signin', :to => 'sessions#new'
match '/signout' :to => 'sessions#destroy'

Creating Sign-in form


<h1>Sign in</h1>

<%= form_for(:session, :url => sessions_path) do |f| %>

<div>
 <%= f.label :email %><br />
 <%= f.text_field :email %>
 </div>
 <div>
 <%= f.label :password %><br />
 <%= f.password_field :password %>
 </div>
 <div>
 <%= f.submit "Sign in" %>
 </div>
 <% end %>

<p>New user? <%= link_to "Sign up now!", signup_path %></p>


This tutorial introduced me to important concepts such as cookies, authentication

I thought of updating the code of what i practised today, but i faced a problem in updating my git repository

When i gave the git merge sign-in-out command, received the following error

Error

Updating 77e2668..3db6c58
error: Your local changes to the following files would be overwritten by merge:
webrat.log
Please, commit your changes or stash them before you can merge.
Aborting

I searched to solve it but couldn’t end up

Hope to solve it soon and will share my learning experience with Chapter 9.

By 3:45 p.m., we gathered for a SCRUM meet, and i told as i worked with Chapter 9.

So far, i have completed learning nine lessons, and will be continuing with Chapter 10 :)


Read Full Post »

Hai

Today i worked the exercises at the end of Chapter 8 of Learn Rails by Example and watched the Chapter 9 screencast

Working with Chapter 8 Exercises

Question 1:

Write tests to check for the presence of each field (name, email, password, password_confirmation) on the signup form.

Solution


require 'spec_helper'
describe UsersController do
render_views
describe "GET 'new'" do

it "should have a name field" do
 get :new
 response.should have_selector("input[name='user[name]'][type='text']")
 end

 it "should have an email field" do
 get :new
 response.should have_selector("input[name='user[email]'][type='text']")
 end

 it "should have a password field" do
 get :new
 response.should have_selector("input[name='user[password]'][type='password']")
 end

 it "should have a password confirmation field" do
 get :new
 response.should have_selector("input[name='user[password_confirmation]'][type='password']")
 end

 end

Since i dont know to give a solution for Question 2, i skipped it and moved to Question 3

Question 3

Refine the code of ‘flash HTML’ in application.html.erb using content_tag helper

Solution

<% flash.each do |key, value| %>
<%= content_tag(:div, value, :class => "flash #{key}") %>
<% end %>

Then i started to watch Chapter 9 screencast.

It completes the sign in and sign out mechanism for the application

I didnt start to work on it.

By 4:00 p.m. we had a SCRUM meet and i told as i worked with Chapter 8 exercises and watched Chapter 9 tutorial

Will be start to work on Chapter 9 by tomorrow 🙂

Read Full Post »

Greetings!

Today i completed working with Chapter 8 of Learn Rails by Example

From this lesson, i learnt to create a functioning ‘Sign Up‘ form

The first step is to write test cases matching the valid information entered by the user

Writing success test case if the attributes are valid

spec/controllers/users_controller_spec.rb


describe "success" do

 before(:each) do
 @attr = { :name => "New User", :email => "user@example.com",
 :password => "foobar", :password_confirmation => "foobar" }
 end

 it "should create a user" do
 lambda do
 post :create, :user => @attr
 end.should change(User, :count).by(1)
 end

 it "should redirect to the user show page" do
 post :create, :user => @attr
 response.should redirect_to(user_path(assigns(:user)))
 end

 it "should have a welcome message" do
 post :create, :user => @attr
 flash[:success].should =~ /welcome to the sample app/i
 end

Add  code to save the user.

It is included inside create method in app/controllers/users_controller.rb


def create
 @user = User.new(params[:user])
 if @user.save
 redirect_to @user, :flash => { :success => "Welcome to the Sample App!"}
 else
 @title = "Sign up"
 render 'new'
 end

 end

Then wrote integration test for the Sign up form


rails generate integration_test users

Writing integration tests cases in spec/requests/users_spec.rb


require 'spec_helper'

describe "Users" do

 describe "signup" do

 describe "failure" do

 it "should not make a new user" do
 lambda do
 visit signup_path
 fill_in "Name",         :with => ""
 fill_in "Email",         :with => ""
 fill_in "Password",     :with => ""
 fill_in "Confirmation", :with => ""
 click_button
 response.should render_template('users/new')
 response.should have_selector('div#error_explanation')
 end.should_not change(User, :count)
 end

 end

 describe "success" do

 it "should make a new user" do
 lambda do
 visit signup_path
 fill_in "Name",         :with => "Example User"
 fill_in "Email",         :with => "user@example.com"
 fill_in "Password",     :with => "foobar"
 fill_in "Confirmation", :with => "foobar"
 click_button
 response.should have_selector('div.flash.success',
 :content => "Welcome")
 response.should render_template('users/show')
 end.should change(User, :count).by(1)
 end

 end

 end

end

Here’s my updated Git and Heroku URL’s

Git URL : https://github.com/rajee/sample_app

Heroku URL : http://floating-river-137.heroku.com/

By 3:45 p.m., we gathered for a SCRUM meet and i told as completed working with Chapter 8

Our mentor, Chandrashekar sir asked us to go through a ReadySet templates ( http://readyset.tigris.org/ )

After the meet, i explored a little bit on it.

ReadySet template is a open source project consists of project management templates helpful in documenting the project.

By tomorrow, i will continue my rails learning process from Chapter 9 🙂

Read Full Post »

Hai

Since we had a power cut in the morning for 3 hours i started to work from 1p.m.

Today i watched and worked with half of Chapter 8 – Learn Rails by Example

So far, this lesson taught me

  • How to create a Sign Up form ?
  • To write a failure test case if the atrributes are invalid
  • How to display the error messages in top of the form ?

Creating a Sign Up form

Edit app/views/users/new.html.erb

 


<h1>Sign up</h1>

<%= form_for(@user) do |f| %>
 <%= render 'shared/error_messages' %>    #render another form to show error messages
 <div>
 <%= f.label :name %><br />
 <%= f.text_field :name %>
 </div>

 <div>
 <%= f.label :email %><br />
 <%= f.text_field :email %>
 </div>

 <div>
 <%= f.label :password %><br />
 <%= f.password_field :password %>
 </div>

 <div>
 <%= f.label :password_confirmation, "Confirmation" %><br />
 <%= f.password_field :password_confirmation %>
 </div>

 <div>
 <%= f.submit "Sign up" %>
 </div>

<% end %>

Add a method create in app/controllers/users_controller.rb


def create
 @user = User.new(params[:user])
 if @user.save
 #Handle a successful save.
 else
 @title = "Sign up"
 render 'new'
 end
 end

Writing a failure test case for invalid data

Edit the file spec/controllers/users_controller_spec.rb


describe "POST 'create'" do

 describe "failure" do

 before(:each) do
 @attr = { :name => "", :email => "", :password => "",
 :password_confirmation => "" }
 end

 it "should have the right title" do
 post :create, :user => @attr
 response.should have_selector('title', :content => "Sign up")
 end

 it "should render the 'new' page" do
 post :create, :user => @attr
 response.should render_template('new')
 end

 it "should not create a user" do
 lambda do
 post :create, :user => @attr
 end.should_not change(User, :count)
 end

 end
 end

 

Displaying error information

Create a folder called shared in app/views.

Then create a partial _error_messages.html.erb in app/views/shared

This file is rendered in app/views/users/new.html.erb


<% if @user.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(@user.errors.count, "error") %>
prohibited this user from being saved:
</h2>
<p>There were problems with the following fields:</p>
<ul>
<% @user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>

</div>
<% end %>

 

By 4:40 p.m. we gathered for a SCRUM meet and i told as i was working with Chapter 8

Will try to complete the rest of this chapter by tomorrow


Read Full Post »

Older Posts »