- Build a RESTful
index
action - Build a RESTful
show
action - Build a RESTful
new
action - Build a RESTful
create
action - Link pages using route helpers
- Use route helpers in a
redirect_to
- Build a new form with a
form_tag
This will be a pretty extensive lab that will combine a number of the concepts that we have reviewed, including:
-
Drawing multiple route types
-
Integrating route helper methods
-
Building out a form and wiring it up to the
create
action -
Linking pages together
In this lab, the application you will be starting out with will be completely blank. There are no models, views, controllers, et cetera. It has a number of RSpec and Capybara tests that will all need to pass to complete the lab. The tests can be found in the spec
directory, in the models
, features
, and controllers
sub-directories. Feel free to walk through the specs to see what behavior the application should have when you're done.
Note: Like many production applications, we've included the config/secrets.yml
file in the .gitignore
. This means that you are going to have to create your own config/secrets.yml
file for the application to run. Don't worry- we've given you a template. Just rename config/secrets-template.yml
to config/secrets.yml
, and you should be able to get the application to run.
The application you will be building is a Coupon app. Below is a high-level overview of the features you'll be building out:
-
You will need to create a
coupons
table withcoupon_code
andstore
columns, which should both be of thestring
data type. -
Your
index
page should show all of the coupons in the database. -
The coupon codes on the
index
page should link to their corresponding couponshow
page. You should use thelink_to
method and route helper methods instead of hard-coding an HTML<a>
tag. -
Your
show
page should render the specific coupon passed to the route. E.g.,coupons/4
should show the coupon with an ID of 4. -
The
new.html.erb
view template should render a form that uses theform_tag
method. -
The form should be wired up to the
create
action in the controller and, when submitted, should create a new record in thecoupons
table with the parameters passed through the form. -
The controller should use the
redirect_to
helper method to redirect the user to theshow
page template for the newly-created coupon.
describe 'Route to view' do it 'has an index page' do visit coupons_path expect(page.status_code).to eq(200) end end
describe 'Multiple coupons are shown' do it 'on the index page' do Coupon.create(coupon_code: "ASD123", store: "Chipotle") Coupon.create(coupon_code: "XYZ098", store: "Jamba") visit coupons_path expect(page).to have_content(/Chipotle|Jamba/) end end
describe 'form page' do it 'form renders with the new action' do visit new_coupon_path expect(page).to have_content("Coupon Form") end
it 'new form submits content and renders form content' do visit new_coupon_path
fill_in 'coupon[coupon_code]', with: "YAYFREE"
fill_in 'coupon[store]', with: "Hobby Lobby"
click_on "Submit Coupon"
expect(page).to have_content("YAYFREE")
end
it 'creates a record in the database' do visit new_coupon_path
fill_in 'coupon[coupon_code]', with: "FREEITEM"
fill_in 'coupon[store]', with: "Quip"
click_on "Submit Coupon"
expect(Coupon.last.store).to eq("Quip")
end end
describe 'Show page' do before do @coupon = Coupon.create(coupon_code: "FREESTUFF", store: "Chipotle") end
it 'renders properly' do visit coupon_path(@coupon) expect(page.status_code).to eq(200) end
it 'renders the coupon code in a h1 tag' do visit coupon_path(@coupon) expect(page).to have_css("h1", text: "FREESTUFF") end
it 'renders the store name in a h1 tag' do visit coupon_path(@coupon) expect(page).to have_css("h1", text: "Chipotle") end end
describe 'linking from the index page to the show page' do it 'index page coupon code text is hyperlinked to coupon page' do linked_coupon = Coupon.create(coupon_code: "FREESTUFF", store: "Chipotle") visit coupons_path expect(page).to have_link(linked_coupon.coupon_code, href: coupon_path(linked_coupon)) end end