- 浏览: 55682 次
- 来自: ...
最近访客 更多访客>>
文章列表
the problem of the implementation of last chapter is:
1. it has a line of code:
following_ids = user.following_ids
this will fetch all the followed users of this user,
but what we need to just if user_id is included in this set.
SQL already optimized things like this, check inclusion in ...
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
.
.
.
describe "from_users_followed_by" do
before(:each) do
@other_user = Factory(: ...
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 page at all?
that is a waste of resource.
right, this is exactly the problem solved by Ajax.
...
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["db:reset"].invoke
make_users
make_microposts
make_relationships
...
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 model Relationship follower_id:integer followed_id:integer
3. we need to add index to th ...
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. we will test access control to the microposts controller first:
describe MicropostsController d ...
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
. .
it "should show the user's microposts" do
mp1 = Factory(:micropost, :us ...
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
2. next, since we expect to retrieve all microposts associated with a user, we need to add ...
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 which is a boolean.
and in the model, you can use admin? to get this attr value.
1. let's write ...
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 sure sign-in user can see all users list.
non-signed-in user can only see users show page, not the al ...
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
before(:each) do
@user = Factory(:user)
end
describe "for not signed-in us ...
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 "PUT" request to update user profile
c. we need to make sure only cur ...
9.4 sign out
- 博客分类:
- ruby and rails
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
test_sign_in(Factory(:user))
delete :destroy
controller.should_not be_signed ...
1. we will first finish the create action:
def create
user = User.authenticate(params[:session][:email],
params[:session][:password])
if user.nil?
flash.now[:error] = "Invalid email/password combination."
@title = "Sign in" ...
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, and disappears when you click on another link. Unfortunately, this means that if we don’t redirect, ...