29
loading...
This website collects cookies to deliver better user experience
rails new subscriber_app
cd subscriber_app
rails s
bin/webpack-dev-server
rails new
command generates a new rails app with the default rails directory. Rails will help a lot in getting us started. The directory structure and default code come from the philosophy convention over configuration. This essentially means that the authors of Rails have decided to guide new projects to have the same directory structure and boilerplate code as any other Rails app.rails generate scaffold subscriber name:string email:string
subscriber_app git:(master) ✗ rails generate scaffold subscriber name:string email:string
Running via Spring preloader in process 53538
invoke active_record
create db/migrate/20210317173258_create_subscribers.rb
create app/models/subscriber.rb
invoke test_unit
create test/models/subscriber_test.rb
create test/fixtures/subscribers.yml
invoke resource_route
route resources :subscribers
invoke scaffold_controller
create app/controllers/subscribers_controller.rb
invoke erb
create app/views/subscribers
create app/views/subscribers/index.html.erb
create app/views/subscribers/edit.html.erb
create app/views/subscribers/show.html.erb
create app/views/subscribers/new.html.erb
create app/views/subscribers/_form.html.erb
invoke resource_route
invoke test_unit
create test/controllers/subscribers_controller_test.rb
create test/system/subscribers_test.rb
invoke helper
create app/helpers/subscribers_helper.rb
invoke test_unit
invoke jbuilder
create app/views/subscribers/index.json.jbuilder
create app/views/subscribers/show.json.jbuilder
create app/views/subscribers/_subscriber.json.jbuilder
invoke assets
invoke scss
create app/assets/stylesheets/subscribers.scss
invoke scss
create app/assets/stylesheets/scaffolds.scss
db/migrate
with the following:class CreateSubscribers < ActiveRecord::Migration[6.1]
def change
create_table :subscribers do |t|
t.string :name
t.string :email
t.timestamps
end
end
end
rails db:migrate
# config/routes.rb
Rails.application.routes.draw do
resources :subscribers
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
resources
method does something powerful. When a user navigates to /subscribers on your app, the router points towards the controller action which in turn renders the view.new
method.subscribers_controller.rb
file, we can see the new
method.app/views/subscribers
folder to render the correct HTML file. In this case, new.html.erb
app/models/subscriber.rb
Capture a valid email address and name
test/models/subscriber_test.rb
, write the following:require "test_helper"
class SubscriberTest < ActiveSupport::TestCase
test 'invalid if email is nil' do
subscriber = Subscriber.new(name: 'John Doe', email: nil)
assert subscriber.invalid?
end
end
rails test test/models/subscriber_test.rb
Running via Spring preloader in process 9287
Run options: --seed 17734
# Running:
F
Failure:
SubscriberTest#test_invalid_if_email_is_nil [/Users/williamkennedy/projects/teaching_rails/subscriber_app/test/models/subscriber_test.rb:7]:
Expected false to be truthy.
rails test test/models/subscriber_test.rb:5
Finished in 0.204865s, 4.8813 runs/s, 4.8813 assertions/s.
1 runs, 1 assertions, 1 failures, 0 errors, 0 skips
# app/models/subscriber.RB
validates :email, presence: true
rails test test/models/subscriber_test.rb
rails test test/models/subscriber_test.rb:5
# Running:
.
Finished in 0.204100s, 4.8996 runs/s, 4.8996 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
test/models/subscriber_test.RB
`` to ensure we are saving correct emails.
`app/models/subscriber.rb
, add the following:
`tests/models/subscriber_test.rb
`app/models/subscriber.rb
, add name to our first validation:
`