38
loading...
This website collects cookies to deliver better user experience
/subscribers
# app/controllers/subscribers_controller.rb
def index
@subscribers = Subscriber.all
end
# app/controllers/subscribers_controller.rb
def index
end
undefined method `each' for nil:NilClass
.each
method on something that doesn’t exist.@subscribers = Subscriber.all
, our view did not know what to do.# app/views/subscribers/index.html.erb
<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @subscribers.each do |subscriber| %>
<tr>
<td><%= subscriber.name %></td>
<td><%= subscriber.email %></td>
<td><%= link_to 'Show', subscriber %></td>
<td><%= link_to 'Edit', edit_subscriber_path(subscriber) %></td>
<td><%= link_to 'Destroy', subscriber, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
rm app/views/subscribers/index.html.erb
rm app/views/subscribers/edit.html.erb
# app/controllers/subscribers_controller.rb
class SubscribersController < ApplicationController
before_action :set_subscriber, only: %i[show]
# GET /subscribers/1 or /subscribers/1.json
def show
end
# GET /subscribers/new
def new
@subscriber = Subscriber.new
end
# POST /subscribers or /subscribers.json
def create
@subscriber = Subscriber.new(subscriber_params)
respond_to do |format|
if @subscriber.save
format.html { redirect_to @subscriber, notice: "Subscriber was successfully created." }
format.json { render :show, status: :created, location: @subscriber }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @subscriber.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_subscriber
@subscriber = Subscriber.find(params[:id])
end
# Only allow a list of trusted parameters through.
def subscriber_params
params.require(:subscriber).permit(:name, :email)
end
end
/subscribers
, you will get a new error.The action 'index' could not be found for SubscribersController
resources :subscribers, only: %i[new create show]
# app/views/subscribers/show.html.erb
<%= link_to 'Edit', edit_subscriber_path(@subscriber) %> |
<%= link_to 'Back', subscribers_path %>
Ensure that users can’t see who’s also subscribed
resources :subscribers, only: %i[new create]
# app/controllers/subscribers_controller.rb
class SubscribersController < ApplicationController
# GET /subscribers/new
def new
@subscriber = Subscriber.new
end
# POST /subscribers or /subscribers.json
def create
@subscriber = Subscriber.new(subscriber_params)
respond_to do |format|
if @subscriber.save
format.html { redirect_to @subscriber, notice: "Subscriber was successfully created." }
format.json { render :show, status: :created, location: @subscriber }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @subscriber.errors, status: :unprocessable_entity }
end
end
end
private
# Only allow a list of trusted parameters through.
def subscriber_params
params.require(:subscriber).permit(:name, :email)
end
end
# test/controllers/subscribers_controller_test.rb
require "test_helper"
class SubscribersControllerTest < ActionDispatch::IntegrationTest
setup do
@subscriber = subscribers(:one)
end
test "should get new" do
get new_subscriber_url
assert_response :success
end
test "should create subscriber" do
assert_difference('Subscriber.count') do
post subscribers_url, params: { subscriber: { email: @subscriber.email, name: @subscriber.name } }
end
assert_redirected_to subscriber_url(Subscriber.last)
end
end
rails test test/controllers/subscribers_controller_test.rb
Running via Spring preloader in process 58423
Run options: --seed 48833
# Running:
.F
Failure:
SubscribersControllerTest#test_should_create_subscriber [/Users/williamkennedy/projects/teaching_rails/subscriber_app/test/controllers/subscribers_controller_test.rb:14]:
"Subscriber.count" didn't change by 1.
Expected: 3
Actual: 2
rails test test/controllers/subscribers_controller_test.rb:13
Finished in 0.306320s, 6.5291 runs/s, 6.5291 assertions/s.
2 runs, 2 assertions, 1 failures, 0 errors, 0 skips
test "should create subscriber" do
assert_difference('Subscriber.count') do
post subscribers_url, params: { subscriber: { email: "[email protected]", name: @subscriber.name } }
end
assert_redirected_to subscriber_url(Subscriber.last)
end
Error:
SubscribersControllerTest#test_should_create_subscriber:
NoMethodError: undefined method `subscriber_url' for #<SubscribersController:0x000000000088e0>
Did you mean? subscribers_url
app/controllers/subscribers_controller.rb:14:in `block (2 levels) in create'
app/controllers/subscribers_controller.rb:12:in `create'
test/controllers/subscribers_controller_test.rb:15:in `block (2 levels) in <class:SubscribersControllerTest>'
test/controllers/subscribers_controller_test.rb:14:in `block in <class:SubscribersControllerTest>'
resources :subscribers, only: %i[new create] do
collection do
get :thank_you
end
end
rails routes | grep subscriber
thank_you_subscribers GET /subscribers/thank_you(.:format) subscribers#thank_you
subscribers POST /subscribers(.:format) subscribers#create
new_subscriber GET /subscribers/new(.:format) subscribers#new
# test/controllers/subscribers_controller_test.rb
assert_redirected_to thank_you_subscribers_path
rails test test/controllers/subscribers_controller_test.rb
rails test test/controllers/subscribers_controller_test.rb
# app/controllers/subscribers_controllers.rb
class SubscribersController < ApplicationController
# POST /subscribers or /subscribers.json
def create
@subscriber = Subscriber.new(subscriber_params)
respond_to do |format|
if @subscriber.save
format.html { redirect_to thank_you_subscribers_path, notice: "Subscriber was successfully created." }
format.json { render :show, status: :created, location: @subscriber }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @subscriber.errors, status: :unprocessable_entity }
end
end
end
def thank_you
end
end
test "should get thank_you" do
get thank_you_subscribers_path
assert_response :success
end
touch app/views/thank_you.html.erb
bundle add gibbon
app/workers/mailchimp_subscribe_worker.rb
test/workers/mailchimp_subscribe_worker_test.rb
# app/workers/mailchimp_subscriber_worker.rb
class MailchimpSubscribeWorker
include Sidekiq::Worker
def perform(subscriber_id)
gibbon = Gibbon::Request.new(api_key: "API_KEY")
subscriber = Subscriber.find subscriber_id
gibbon.lists("YOUR_LIST_ID").members.create(body: {email_address: subscriber.email, status: "subscribed", merge_fields: {FNAME: subscriber.name, LNAME: ""}})
end
end
# app/controllers/subscribers_controller.rb
# POST /subscribers or /subscribers.json
def create
@subscriber = Subscriber.new(subscriber_params)
respond_to do |format|
if @subscriber.save
MailchimpSubscribeWorker.perform_async(@subscriber.id)
format.html { redirect_to thank_you_subscribers_path, notice: "Subscriber was successfully created." }
format.json { render :show, status: :created, location: @subscriber }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @subscriber.errors, status: :unprocessable_entity }
end
end
end