back to

Ruby on Rails To-do List

ARCHIVED

Archived: This project has been archived. Cards can no longer be completed.

Editing Todo Items

Stefy
Stefy completed this card.
In order to not have to tie the todo_list every single time while we are on a controller or in a view, we can create this URL options method:

>todo_items_controller.rb: 
def url_options
  { todo_list_id: params[:todo_list_id] }.merge(super)
end

Every time we are looking for todo_list_id we can give it the id of the todo_list because we already have it. 


>edit.spec.rb:
require 'spec_helper'

describe "Editing todo items" do
	let!(:todo_list) { TodoList.create(title: "Grocery List",
		description: "Groceries")}
	let!(:todo_item) { todo_list.todo_items.create(content: "Milk")}

	def visit_todo_list(list)
		visit "/todo_lists"
		within "#todo_list_#{list.id}" do
			click_link "List Items"
		end
	end


	it "is successful with valid content" do
		visit_todo_list(todo_list)
		within("#todo_items_#{todo_item.id}")
		click_link "Edit"
	end
	fill_in "Content", with: "Lots of Milk"
	click_button "Save"
	expect(page).to have_content ("Saved todo list item.")
	todo_item.reload
	expect(todo_item.title).to eq("Lots of Milk")
end

	it "is unsuccessful with no content" do
		visit_todo_list(todo_list)
		within("#todo_items_#{todo_item.id}")
		click_link "Edit"
	end
	fill_in "Content", with: ""
	click_button "Save"
	expect(page).to_not have_content ("Saved todo list item.")
	expect(page).to have_content("Content can't be blank")
	todo_item.reload
	expect(todo_item.title).to eq("Milk")

	it "is unsuccessful with not enough content" do
		visit_todo_list(todo_list)
		within("#todo_items_#{todo_item.id}")
		click_link "Edit"
	end
	fill_in "Content", with: "1"
	click_button "Save"
	expect(page).to_not have_content ("Saved todo list item.")
	expect(page).to have_content("Content is too short")
	todo_item.reload
	expect(todo_item.title).to eq("Milk")

end