Giter VIP home page Giter VIP logo

rails-create-action-lab's Introduction

Create Action Lab

  1. Build a working create action
  2. Use a form_tag to create a new form that submits to the index action
  3. Correctly name form fields so that they end up at the top level of the params hash
  4. Assign data from params to a Student object in the create action
  5. Save that Student object in the create action
  6. Create a redirect_to from the create action to the show action

Instructions

Many of the spec tests are already passing for this lab, but there are some additional ones –– including our first use of controller tests –– located here:

  • Controller Specs - specs/controllers/students_controller_spec.rb

  • Capybara Specs - specs/features/student_spec.rb:37

When this lab is complete, your application should have the following behavior:

  1. User fills out the form on students/new

  2. When the form is submitted, a new record is created in the database

  3. After the user submits the form, they are redirected to the show page that renders the Student record that they created

Make sure to use a route helper method to redirect to the show page at the end of the create method. You can find the correct syntax for this route by running rake routes in the terminal.

Resources

rails-create-action-lab's People

Contributors

annjohn avatar dakotalmartinez avatar danielseehausen avatar ihollander avatar jmburges avatar jordanhudgens avatar maxwellbenton avatar perpepajn25 avatar pletcher avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rails-create-action-lab's Issues

rails form is already created

Part of the instructions for this lab are to create the form code and it appears the master branch already includes this.

After completing the controller action, all tests pass.

Lab also makes use of partials which haven't been covered yet.

Receiving this error in Rspec when I begin the lab

DEPRECATION WARNING: You didn't set secret_key_base. Read the upgrade documentation to learn more about this new config option. (called from load at /Users/james/.rvm/gems/ruby-2.2.1/bin/rspec:23)

An error occurred in an after hook
NoMethodError: undefined method original_path_set' for nil:NilClass occurred at /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-rails-3.0.2/lib/rspec/rails/view_rendering.rb:98:inblock (2 levels) in module:ViewRendering'

An error occurred in an after hook
NoMethodError: undefined method each' for nil:NilClass occurred at /Users/james/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_controller/test_case.rb:58:inteardown_subscriptions'

F

Failures:

  1. StudentsController POST create with valid attributes creates a new student
    Failure/Error: Unable to find matching line from backtrace
    RuntimeError:
    Missing secret_token and secret_key_base for 'test' environment, set these values in config/secrets.yml

    /Users/james/.rvm/gems/ruby-2.2.1/gems/railties-4.2.3/lib/rails/application.rb:534:in `validate_secret_key_config!'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/railties-4.2.3/lib/rails/application.rb:246:in`env_config'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/testing/test_request.rb:17:in `initialize'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_controller/test_case.rb:200:in`initialize'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/testing/test_request.rb:13:in `new'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/testing/test_request.rb:13:in`new'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_controller/test_case.rb:679:in `build_request'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_controller/test_case.rb:668:in`setup_controller_request_and_response'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-rails-3.0.2/lib/rspec/rails/adapters.rb:111:in `block (2 levels) in setup'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/example.rb:294:in`instance_exec'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/example.rb:294:in `instance_exec'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/hooks.rb:349:in`run'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/hooks.rb:410:in `block in run'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/hooks.rb:410:in`each'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/hooks.rb:410:in `run'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/hooks.rb:485:in`run'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/example.rb:340:in `run_before_example'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/example.rb:147:in`block in run'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/example.rb:210:in `call'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/example.rb:210:in`block (2 levels) in class:Procsy'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-rails-3.0.2/lib/rspec/rails/example/controller_example_group.rb:174:in `block (2 levels) in module:ControllerExampleGroup'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/example.rb:294:in`instance_exec'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/example.rb:294:in `instance_exec'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/hooks.rb:430:in`block (2 levels) in run'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/example.rb:210:in `call'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/example.rb:210:in`block (2 levels) in class:Procsy'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-rails-3.0.2/lib/rspec/rails/adapters.rb:72:in `block (2 levels) in module:MinitestLifecycleAdapter'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/example.rb:294:in`instance_exec'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/example.rb:294:in `instance_exec'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/hooks.rb:430:in`block (2 levels) in run'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/example.rb:210:in `call'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/example.rb:210:in`block (2 levels) in class:Procsy'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/hooks.rb:432:in `run'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/hooks.rb:485:in`run'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/example.rb:303:in `with_around_example_hooks'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/example.rb:145:in`run'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/example_group.rb:494:in `block in run_examples'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/example_group.rb:490:in`map'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/example_group.rb:490:in `run_examples'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/example_group.rb:457:in`run'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/example_group.rb:458:in `block in run'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/example_group.rb:458:in`map'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/example_group.rb:458:in `run'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/example_group.rb:458:in`block in run'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/example_group.rb:458:in `map'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/example_group.rb:458:in`run'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:112:in `block (2 levels) in run_specs'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:112:in`map'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:112:in `block in run_specs'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/reporter.rb:54:in`report'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:108:in `run_specs'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:86:in`run'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:70:in `run'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:38:in`invoke'

    /Users/james/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/exe/rspec:4:in `<top (required)>'

    /Users/james/.rvm/gems/ruby-2.2.1/bin/rspec:23:in`load'

    /Users/james/.rvm/gems/ruby-2.2.1/bin/rspec:23:in `'

    /Users/james/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in`eval'

    /Users/james/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `'

    Showing full backtrace because every line was filtered out.

    See docs for RSpec::Configuration#backtrace_exclusion_patterns and

    RSpec::Configuration#backtrace_inclusion_patterns for more information.

Finished in 0.06258 seconds (files took 2.08 seconds to load)
1 example, 1 failure

form params not nested under student key

Is there a reason we're not expecting the form params to be nested underneath the student key in the controller spec?

i.e.

      it "redirects to the new student" do
        post :create, { :first_name => "Sam", :last_name => "Smith" }
        expect(response).to redirect_to Student.last
      end

instead of

      it "redirects to the new student" do
        post :create, { :student => { :first_name => "Sam", :last_name => "Smith" } }
        expect(response).to redirect_to Student.last
      end

Had a student remember doing it in Sinatra with the nested hash asking why these tests were expecting it to come in differently.

student_controller_spec is not consistent with the rest of the tests

I'd like to add that you don't have to change these specs per say but if you want the pattern for params to be { student: { first_name: 'Sam', last_name: 'Wise' } } then the Fill In test for capybara for the form will be looking for only last_name not student[last_name]. I wonder what Learn.co's defacto standard is for params. should we always nest the data? and if so it would be nice if there was more consistency with it across some labs where possible. Thank you.

As it is now with this pattern the student object will be nil.

# ./spec/controllers/student_controller_spec.rb
  describe "POST create" do
    context "with valid attributes" do
      it "creates a new student" do
        expect{
          post :create, { :student => { :first_name => "Sam", :last_name => "Smith" } }
        }.to change(Student,:count).by(1)
      end

      it "redirects to the new student" do
        post :create, { :student => { :first_name => "Sam", :last_name => "Smith" } }
        expect(response).to redirect_to Student.last
      end
    end
  end

with these changes the spec is consistent with whats expected to work with the integration tests.

require 'rails_helper'

describe StudentsController do

  describe "POST create" do
    context "with valid attributes" do
      it "creates a new student" do
        expect{
          post :create, { :first_name => "Sam", :last_name => "Smith" }
        }.to change(Student,:count).by(1)
      end

      it "redirects to the new student" do
        post :create, { :first_name => "Sam", :last_name => "Smith" }
        expect(response).to redirect_to Student.last
      end
    end
  end

end

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.