Restaurantly#edit #update #destroy controller actions
John Davison photo
Get notifications about new tutorials, code and events.

click: follow along with the source code

RESTAURANTS#EDIT

git checkout -b restaurants-edit

modify spec/requests/resaurants_spec.rb

  context "GET /restaurants/:id/edit" do
    before do
      @restaurant = FactoryGirl.create :restaurant
    end

    context "resource exists" do
      subject { get "/restaurants/#{@restaurant.id}/edit" }
      it {expect(subject).to render_template(:edit)}
    end

    context "resource doesn't exist" do
      subject {get "restaurants/#{@restaurant.id + 1}/edit"}
      it { expect(subject).to redirect_to(:root) }
    end
  end

rspec spec/requests/restaurant_spec.rb

expect green and red

modify app/controllers/restaurants_controller.rb

  def edit
    @restaurant = Restaurant.find_by_id params[:id]
    if @restaurant
      render :edit
    else
      redirect_to root_path
    end
  end

modify app/views/restaurants/edit.html.haml

  .row
    .large-8.columns.large-centered
      = form_for @restaurant do |restaurant|
        = restaurant.text_field :name
        = restaurant.submit "update", class: "button"

modify index.html.haml

  = link_to "edit", edit_restaurant_path(restaurant)

modify spec/requests/resaurants_spec.rb

  context "PUT /restaurants/:id" do
    context "complete params" do
      before do
        @restaurant = FactoryGirl.create :restaurant
        @restaurant_info = {id: @restaurant.id, restaurant: {name: "mcrails"}}
      end
      subject{ put "/restaurants/#{@restaurant.id}", @restaurant_info }
      it { expect(subject).to redirect_to restaurant_path(id:1) }
    end

    context "incomplete params" do
      subject{ put "/restaurants/1", {id: 1} }
      it { expect(subject).to redirect_to root_path }
    end
  end

rspec spec/requests/restaurant_spec.rb

modify app/controllers/restaurants_controller.rb

  def update
    @restaurant = Restaurant.find_by_id params[:id]
    if @restaurant
      @restaurant.update_attributes(name: params[:restaurant][:name])
      flash[:success] = "Name Updated!"
      redirect_to restaurant_path @restaurant
    else
      flash[:alert] = "Houston, we have a problem"
      redirect_to root_path
    end
  end

modify spec/requests/resaurants_spec.rb

  context "DELETE /restaurants/:id" do
    context "complete params" do
      before do
        @restaurant = FactoryGirl.create :restaurant
      end
      subject{ delete "/restaurants/#{@restaurant.id}"}
      it { expect(subject).to redirect_to root_path }
    end
  end

modify app/controllers/restaurants_controller.rb

  def destroy
    @restaurant = Restaurant.find_by_id params[:id]
    @restaurant.destroy
    redirect_to root_path
  end

modify index.html.haml

  = link_to "destroy", restaurant_path(restaurant), method: :delete

verify in browser, visit http://localhost:3000/

Get notifications about new tutorials, code and events.

Get notifications about new tutorials, code and events.