ruby and rails
文章平均质量分 75
midfinger
这个作者很懒,什么都没留下…
展开
-
12.3.3 scaling issue of the status feed
the problem of the implementation of last chapter is: 1. it has a line of code: following_ids = user.following_idsthis will fetch all the followed users of this user, but what we need to...原创 2011-10-30 17:54:05 · 123 阅读 · 0 评论 -
8.4 rspec integration tests
in integration test, you can test many controllers at the same time, test a complete workflow, like login to the home page, open signup page, fill in contents, and submit...... maybe you have ...原创 2011-10-11 16:53:43 · 75 阅读 · 0 评论 -
8.3 sign up success
Chapter 8.3this part, we will deal with the case that sign up success. 1. start from TDD!!!! describe "success" do before(:each) do @attr = { :name => "New User", :em...原创 2011-10-11 14:39:49 · 190 阅读 · 0 评论 -
8.2 sign up failure
chapter 8.2 for this section, we will do this: if a signup form has invalid submission, the app will re-render the signup page, show the error details. 1. we will do TDD again!!! resou...原创 2011-10-11 11:59:54 · 234 阅读 · 0 评论 -
understand rails authenticity Token!
What happens: When the user views a form to create, update, or destroy a resource, the rails app would create a random authenticity_token, store this token in the session, and place it in a hidden f...原创 2011-10-11 11:24:26 · 79 阅读 · 0 评论 -
8.1 sign up form
chapter 8.1 1. let's first make a branch for the sign up chapter:git checkout -b signing-up 2. also reset the database:rake db:reset 3. we already got two basic test for new action in ...原创 2011-10-11 11:17:15 · 324 阅读 · 0 评论 -
7.3 better user views
Chapter 7.3 now, we have a good user model, we can try to make a show page to show the user info. we already have some test for the new action in the users controller. now we will add some...原创 2011-10-10 16:43:36 · 75 阅读 · 0 评论 -
write a authenticate method
chapter 7.2.4 1. we need to create a user, save it to database2. we need to get the user object by email3. we need to verify that it has a given password not surprising, we will start from...原创 2011-10-10 16:18:10 · 180 阅读 · 0 评论 -
7.2 secure the password
chapter 7.2 again, let's start from TDD again!!!!!!!!!!!!!! 1. since we define the encrypt_password into private area, how do we test it???????? ok, we need some public interface to use it...原创 2011-10-10 12:20:19 · 103 阅读 · 0 评论 -
7.1.3 active record callback
chapter 7.1.3 now that our user model has an attr for storing password, we need to arrange to generate and save an encrypted password when save user to the database. the technique of doing thi...原创 2011-10-10 11:47:30 · 68 阅读 · 0 评论 -
7.1 adding passwords to user model.
1. rake db:reset this command is very useful, it will clear the old sample data from database, so we again have a clean database 2. here, we will just use plain password, with bad secure, we w...原创 2011-10-09 14:44:15 · 173 阅读 · 0 评论 -
6.3 make model, view, controller work together.
1. let's start to console without --sandbox param to create some record into our database: rails consoleUser.create!(:name => "china zhang", :email => "china@china.com") to see if t...原创 2011-10-09 10:20:02 · 4612 阅读 · 0 评论 -
6.3 add some debug info to the layouts, rails environment.
<html> <body> <div class="container"> <%= render 'layouts/footer'%> <%= debug(params) if Rails.env.development? %> </div> <原创 2011-10-08 18:29:45 · 68 阅读 · 0 评论 -
6.2 the uniqueness caveat when saving into database: adding index to tables!!!
using validates :email, :uniqueness => true doesn't guarantee uniqueness!!!!! here is why: 1. Alice sign up with address alice@wonderland.com2. Alice accidentally clicks on "Submit" ...2011-10-08 16:25:02 · 91 阅读 · 0 评论 -
开发学到的东西
1.options = User.all(:conditions => "status <> 3", :order => "firstname, lastname").collect{|x| [x.name, x.id.to_s]} cars = Car.where(:colour => 'black').all (adding .all means ...2011-09-26 17:28:47 · 77 阅读 · 0 评论 -
9.1 sessions
a session is a semi-permanent connection between 2 computers, such as client running browser & server running rails. there are several model for session behaviors:1. forget session on browse...原创 2011-10-12 10:00:52 · 90 阅读 · 0 评论 -
9.2 sign in failure
start from TDD!!! 1. require 'spec_helper'describe SessionsController do render_views . . . describe "POST 'create'" do describe "invalid signin" do before(:each) ...原创 2011-10-12 12:19:30 · 209 阅读 · 0 评论 -
12.3 the status feed
1. we need to get all the micropost of the users followed by current user. Micropost.from_users_followed_by(user) so we can write a test this method. describe Micropost do . . . ...原创 2011-10-30 15:34:16 · 152 阅读 · 0 评论 -
12.2 a working follow button with Ajax
1. in the last chapter, in the user profile screen, the user can click on the follow button, then he is redirect back to the original profile screen. so we can ask, why do we leave that profile ...原创 2011-10-29 18:10:06 · 113 阅读 · 0 评论 -
12.2 a web interface for following and followers.
1.before we do the UI, we need to populate database.we will write a rake task to do this: namespace :db do desc "populate database" task :populate => :environment do Rake:Task["...原创 2011-10-28 22:14:32 · 339 阅读 · 0 评论 -
12. following user, 12.1 relationship model
1. we need to use a relationships table to stand for the following relationship: has_many :following, :through => :relationships, :source => "followed_id" 2. then $ rails generate m...原创 2011-10-18 14:29:00 · 170 阅读 · 0 评论 -
11.3 manipulating microposts.
1. since all micropost actions will be done in users page, so we only need :create and :destroy actions. so the routes will be: resources :microposts, :only => [:create, :destroy] 2...原创 2011-10-17 15:31:24 · 192 阅读 · 0 评论 -
11.2 show microposts.
1. add test to test the new users/show view: describe UsersController do render_views . describe "GET 'show'" do before(:each) do @user = Factory(:user) end . . ...原创 2011-10-17 12:01:38 · 98 阅读 · 0 评论 -
11.1 user micropost -- a micropost model.
1. we will first generate a micropost model. $ rails generate model Micropost content:string user_id:integer note, if you the content is longer, you can use type "text" instead of string ...原创 2011-10-17 10:43:52 · 161 阅读 · 0 评论 -
10.4 destroying users.
in this chapter, we will add destroy action to users controller to finish REST. first, we need to add administrative users who can use the destroy action. so we need to add a new attr of admin whi...原创 2011-10-16 15:47:05 · 119 阅读 · 0 评论 -
10.3 showing users list
in this chapter, we will do user list, i.e. the index action of users controller.at the same time, we will study pagination, and how to populate many sample data into database. 1. we will make s...原创 2011-10-15 20:41:57 · 114 阅读 · 0 评论 -
10.2 protect pages.
again, we will start from TDD!!! 1. since both edit and update need the same authentication, we can put their test together: describe "authentication of edit/update pages" do befor...原创 2011-10-15 15:11:46 · 103 阅读 · 0 评论 -
10.1 updating users.
1. git checkout -b updating-users 2. in this chapter, we will make you can update user profile.a. we will use a "edit" action to render a view to edit user.b. we will use "update" action and a...原创 2011-10-14 18:30:01 · 127 阅读 · 0 评论 -
9.4 sign out
whew!!!, last chapter is a long one! now, we are going to implement sign out. 1. no wonders, we will start from TDD. describe "DELETE 'destroy'" do it "should sign a user out" do t...原创 2011-10-13 15:21:13 · 165 阅读 · 0 评论 -
9.3 sign in success.
1. we will first finish the create action: def create user = User.authenticate(params[:session][:email], params[:session][:password]) if user.nil? fl...原创 2011-10-12 15:39:20 · 230 阅读 · 0 评论 -
9.1 about flash.now[:error] vs flash[:error]
There’s a subtle difference between flash and flash.now. The flash variable is designed to be used before a redirect, and it persists on the resulting page for one request—that is, it appears once...原创 2011-10-12 15:37:58 · 95 阅读 · 0 评论 -
6.2 user validations
1. ok, let's add validations to our models:a. name should not be blank.b. email should follow email format.c. email should be unique. 2. there are some common validations:validates pressen...原创 2011-09-13 18:31:58 · 258 阅读 · 0 评论 -
6.1 play with the newly created model.
in prev chapter, we created a user model, now we will play with it. 1. first, let's start rails console, this time we will use a param: --sandboxthis param make sure when you exit the cons...2011-09-05 12:26:35 · 106 阅读 · 0 评论 -
windows下如何运行一个unit test文件中的一个方法
ruby test/unit/abcd.rb --name test_the_1st_method也就是运用name参数,指定只运行其中一个method2009-07-13 15:12:56 · 223 阅读 · 0 评论 -
学习rails心得
1. 书籍:rails 圣经: Agile Web Development with RailsRuby 圣经: programming rubyRails Recipes: rails 菜单,介绍几十个最常用的rails的偏方,涉及View, AJAX, 数据库,email, 是实用主义至上的书籍,相信每个rails项目都会用到其中的偏方。有这三本书足以。2. ...原创 2009-07-12 23:08:15 · 76 阅读 · 0 评论 -
如何把需要在project中多处用到的功能或者方法封装成plug-in
例如在一个项目中,多处用到了搜索数据库中多个column的功能:class Post < ActiveRecord::Base has_many :comments def self.search(query, fields, options = {}) find :all, options.merge(:conditions => [[fields].flatten.map...原创 2009-07-12 15:40:59 · 140 阅读 · 0 评论 -
ajax in rails and rjs
rails and ajaxrender:update do |page| page.replace_html 'user_list', :paitial => 'user', :collection => @users page.visual_effect :highlight, 'user_list'endrender:update 生成了一个javascrip...原创 2009-06-29 11:34:06 · 89 阅读 · 0 评论 -
这一节我们要接触一点稍稍高科技的东西AJAX and RJS
首先解释一下AJAX的工作原理,其实我都忘了,翻了些以前的笔记才想起来:比如你提交一个form的时候,不想更新整个页面,只是想更新局部的页面,因为其他的页面内容没有改变,不需要重新载入,否则浪费时间和带宽。这时候就用到AJAX了。你发送一个xhr request, (XMLhtmlrequest),服务器收到这种request之后,判断request.xhr?, 如果是,则不会返...2009-05-21 18:39:36 · 83 阅读 · 0 评论 -
Time in text_field
如果一个model中含有时间字段,那么add a new object的时候,我们可以使用datetime_select helper method to render it as a list of drop down lists, 就是,年月日小时,分都显示成一个droplist,可以选择。当然,并不是所有人都喜欢这种输入方式。如果用户可以通过文本的方式输入时间,岂不是更好?然后由我们...2009-05-21 17:50:53 · 96 阅读 · 0 评论 -
play with formating time.
如果我们在irb中输入:Time.now=>Thu May 21 14:31:35 +0800 2009我们看到这个格式一般不是我们要显示出来的格式,之所以以这种格式显示出来,是因为调用了Time对象的to_s方法,而to_s方法可以接收一些参数,如果没有参数,那么显示出来的就是默认的上面那种格式,我们可以通过参数改变格式:Format ...2009-05-21 15:09:05 · 89 阅读 · 0 评论