默寫groups_controller.rb

每次寫都沒預習
https://gist.github.com/xdite/fe1da2982e08cc964640a3d88cb4dd65

第四次 4/5
class GroupsController < ApplicationController
  def index
    @groups = Group.all
  end

  def new
    @group = Group.new
  end

  def show
    @group = Group.find(params[:id])
  end

  def edit
    @group = Group.find(params[:id])
  end

  def create
    @group = Group.new(group_params)
    if @group.save
      redirect_to groups.path
    else
      render :new
    end
  end

  def update
    @group = Group.find(params[:id])
    if @group = Group.update
      redirect_to groups.path, notice 'Update success'
    else
      render :edit
    end
  end

  def destroy
    @group.destroy
    redirect_to groups_path, warning 'group deleted'
  end

private

  def group_params
    params.require(:group).permit(:title, :description)
  end
end

第三次 4/4
Class GroupsController < ApplicationController
    def index
    @groups= Group.all
  end
  
  def new
    @group = Group.new
  end
  
  def show
    @group = Group.find(params[:id])
  end
  
  def edit
   @group = Group.find(params[:id])
    end
  
  def create
   @group = Group.new(group_parms)
         if group.save
        redirect_to groups_path
     else
        render :new
     end
  end
  
  def update
   @group = Group.find(param[:id])
    if @group = Group.update, notice 'update Success'
    else
        render :edit
    end
  end
  
  def destroy
   group.destroy
     redirect_to groups_path, alert 'group deleted'
  end
  
private

    def group_params
        require(:group).permit(:title, :description)
    end
end  

第二次 4/1
Class GroupController > ApplicationController

    def index
    @groups= Group.all
  end
  
  def new
    @group = Group.new
  end
  
  def show
    @group = group.find(params[:id])
  end 
  
  def edit
    @group = group.find(params[:id])
  end
  
  def create
    @group = group.new(group_params)
            if @group.save
         redirect_to groups_path
      else
       render :new
      end
  end
  
  def update
    @group = group.find(params[:id])
        if @group.update, notice 'update success'
       redirect_to groups_path
      else
       render :edit
      end
   end
   
   def destroy
    @group = group.find(params[:id])
    @group = group.destroy, alert 'group deleted'
     redirect_to groups_path
   end
private

    def group_params
    group.require(:group).permit(:title, :description)
  end
end   

第一次 3/29
GroupController > ApplicaionController

    def index
        @groups= Group.all
  end
  
  def new
    @group= Group.new
  end
  
  def create
    @group= Group.param(group)
  end
  
  def show
    @group= Group.find(params[:id])
  end  
  
  def edit
    @group= Group.find(params[:id])
    redirect_to groups_path
  end  
  
  def update
      @group= Group.find(params[:id])
    @group= 
    redirect_to groups_path
    end
  
  def destroy
    @group.destroy
    redirect_to groups_path
  end
  
 private
 
    def params
    .permit
  end

正解
class GroupsController < ApplicationController

  def index
    @groups = Group.all
  end


  def new
    @group = Group.new
  end

  def show
    @group = Group.find(params[:id])  #找出要show的id

  end

  def edit
    @group = Group.find(params[:id]) #找出要edit的id

  end

  def create
    @group = Group.new(group_params) #用new去create一個group_params


    if @group.save                   #save之後

      redirect_to groups_path        #回到目錄

    else
      render :new                    #如果沒save就跑new

    end
  end

  def update
    @group = Group.find(params[:id])   #找出要update的id

    if @group.update(group_params)     #如果有update

      redirect_to groups_path, notice: 'Update Success' #提示success回到目錄

    else
      render :edit                     #否則跑edit 

    end
  end

  def destroy
    @group = Group.find(params[:id])    #找出要destroy的id


    @group.destroy
    redirect_to groups_path, alert: 'Group deleted'
  end

  private



  def group_params
    params.require(:group).permit(:title, :description) #建立group,裡面要提供title,description

  end
end
comments powered by Disqus