Start from your own version of roc-lab2, or get it from the repo: git clone git://github.com/kudelabs/roc-lab2.git
-
$ rails generate model user name:string email:string password:string
-
$ rails generate migration add_user_to_messages_and_replies
-
add following two lines to the newly created migration file:
add_column :messages, :user_id, :integer add_column :replies, :user_id, :integer
-
add associations:
-
in user model => has_many :messages, :replies in message and reply models => belongs_to :user
-
rake db:migrate
- $ rails console
- $ user = User.create(:name=>'pengyou',:email=>"[email protected]",:password=>'test')
- $ msg = user.messages.create(:body=>'hellow haha')
- $ reply = msg.replies.create(:body=>"reply to myself")
- $ reply.user = user
- $ reply
start server and then go to see the effect from console.
- $ rails generate controller users new
- $ rails generate controller sessions new
and add before_filter :require_logged_in
- match 'login' => "sessions#new"
- match 'logout' => "sessions#destroy"
- match 'signup' => "users#new"
<%=form_tag sessions_path do%>
<p>
Name: <%=text_field_tag :name%>
</p>
<p>
Password: <%=password_field_tag :password%>
</p>
<%=submit_tag "Login"%>
<%end%>
message.user = current_user
reply.user = current_user