Skip to content

Commit

Permalink
User story (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
chsweet committed Jul 17, 2021
1 parent c607ac2 commit 0cd1c55
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 29 deletions.
10 changes: 8 additions & 2 deletions app/controllers/applications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ def new
end

def create
application = Application.create(application_params)
redirect_to "/applications/#{application.id}"
application = Application.new(application_params)

if application.save
redirect_to "/applications/#{application.id}"
else
redirect_to '/applications/new'
flash[:alert] = "Error: #{error_message(application.errors)}"
end
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/views/applications/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
<br>
<%= f.hidden_field :status, value: 'In Progress' %>

<%= f.submit "Submit Application" %>
<%= f.submit 'Submit Application' %>
<% end %>
70 changes: 44 additions & 26 deletions spec/features/applications/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,52 @@
@pet_3 = Pet.create!(adoptable: true, age: 4, breed: 'chihuahua', name: 'Elle', shelter_id: @shelter.id)
end

it 'links to the new page from the pet index page' do
visit '/pets'
describe 'new application' do
it 'links to the new page from the pet index page' do
visit '/pets'

click_link 'Start an Application'

expect(current_path).to eq('/applications/new')
end

it 'can create a new application when all fields are filled out' do
visit '/applications/new'

fill_in 'Name', with: 'Ciara'
fill_in 'Address', with: '6831 39th Street'
fill_in 'City', with: 'Denver'
fill_in 'State', with: 'CO'
fill_in 'Zip Code', with: 80223
fill_in 'Tell us why you would make a good home', with: 'I will be the best dog mom!'
click_button 'Submit Application'

@application = Application.first

expect(current_path).to eq("/applications/#{@application.id}")
expect(page).to have_content("Ciara")
expect(page).to have_content("Address: 6831 39th Street")
expect(page).to have_content("City: Denver")
expect(page).to have_content("State: CO")
expect(page).to have_content("Zip Code: 80223")
expect(page).to have_content("Description: I will be the best dog mom!")
expect(page).to have_content("Application Status: In Progress")
end
end

click_link 'Start an Application'
describe 'not all fields are filled out' do
it 're-renders the new form' do
visit '/applications/new'

expect(current_path).to eq('/applications/new')
end
fill_in 'Name', with: 'Ciara'
fill_in 'Address', with: '6831 39th Street'
fill_in 'Zip Code', with: 80223
fill_in 'Tell us why you would make a good home', with: 'I will be the best dog mom!'
click_button 'Submit Application'

it 'can create a new application when all fields are filled out' do
visit '/applications/new'

fill_in 'Name', with: 'Ciara'
fill_in 'Address', with: '6831 39th Street'
fill_in 'City', with: 'Denver'
fill_in 'State', with: 'CO'
fill_in 'Zip Code', with: 80223
fill_in 'Tell us why you would make a good home', with: 'I will be the best dog mom!'
click_button 'Submit Application'

@application = Application.all.first

expect(current_path).to eq("/applications/#{@application.id}")
expect(page).to have_content("Ciara")
expect(page).to have_content("Address: 6831 39th Street")
expect(page).to have_content("City: Denver")
expect(page).to have_content("State: CO")
expect(page).to have_content("Zip Code: 80223")
expect(page).to have_content("Description: I will be the best dog mom!")
expect(page).to have_content("Application Status: In Progress")
expect(page).to have_current_path('/applications/new')
expect(page).to have_content("Error: City can't be blank, State can't be blank")
end
end

end

0 comments on commit 0cd1c55

Please sign in to comment.