- Create a User Controller
rails g controller users
in the terminal
- Consider what you want your controller methods to do!
- Create a 'show' method def show @user = User.find(params[:id]) end
- Create a 'new' method def new @user = User.new end
- Create a 'create' method def create @user = User.new(params[:user]) if @user.save render: show else flash[:error] = @user.errors.full_messages render: new end
- Create an 'edit' method def edit @user = User.find(params[:id]) end
- Create an 'update' method def update @user.find(params[:id]) @user.update_attributes(params[:user]) render :show end
- Create a 'destroy' method def destroy user = User.find(params[:id]) user.delete redirect_to(users_path) end
- Create a User Controller
rails g controller lists
in the terminal
- Consider what you want your controller methods to do!
- Create a 'show' method def show @list = List.find(params[:id]) end
- Create a 'new' method def new @list = List.new end
- Create a 'create' method def create @list = User.new(params[:list]) if @list.save render: show else flash[:error] = @list.errors.full_messages render: new end
- Create an 'edit' method def edit @list = list.find(params[:id]) end
- Create an 'update' method def update @list.find(params[:id]) @list.update_attributes(params[:list]) render :show end
- Create a 'destroy' method def destroy list = List.find(params[:id]) list.delete redirect_to(users_path) end
- Create an 'index' method def index @list = List.all end
- Create an Item Controller
rails g controller items
in the terminal
- Consider what you want your controller methods to do!
- Create a 'show' method def show @item = Item.find(params[:id]) end
- Create a 'new' method def new @item = Item.new end
- Create a 'create' method def create @item = Item.new(params[:item]) if @item.save render: show else flash[:error] = @item.errors.full_messages render: new end
- Create an 'edit' method def edit @item = Item.find(params[:id]) end
- Create an 'update' method def update @item.find(params[:id]) @item.update_attributes(params[:item]) render :show end
- Create a 'destroy' method def destroy item = Item.find(params[:id]) item.delete redirect_to(item_path) end
- in config/routes.rb
resources :users, :lists, :items
`root to :'list#index'