jendiamond
jendiamond

Reputation: 69

Rails 5 204 No Content

No template found for ChefsController#create, rendering head :no_content Completed 204 No Content

This is an issue in Rails 5 that helps with APIs

I've seen this post and several others but I am still stuck. https://www.rubyplus.com/articles/2771-Rails-5-Basics-View-to-Model


This is my process of trying to debug this:

Given that I am running the local server
And I am on the localhost:3000/signup
When I enter in the name, email, password & password_confirmation
Then I expect to create a new Chef

Instead I receive a 204 No Content error:

Started GET "/signup" for 33.33.33.1 at 2017-05-09 16:54:38 +0000
Cannot render console from 33.33.33.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
  ActiveRecord::SchemaMigration Load (1.1ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by ChefsController#new as HTML
  Rendering chefs/new.html.erb within layouts/application
  Rendered shared/_page_title.html.erb (0.9ms)
  Rendered shared/_errors.html.erb (1.6ms)
  Rendered chefs/new.html.erb within layouts/application (61.2ms)
  Rendered layouts/_navigation.html.erb (1.7ms)
  Rendered layouts/_messages.html.erb (2.0ms)
  Rendered layouts/_footer.html.erb (1.0ms)
Completed 200 OK in 2750ms (Views: 2688.6ms | ActiveRecord: 10.1ms)
Started POST "/chefs" for 33.33.33.1 at 2017-05-09 16:55:31 +0000
Cannot render console from 33.33.33.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by ChefsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"IyvuDTHgojOl5TbzhbFUIdoKuPAgT8bNRZbNUXMSu1vW0O6Adv9m11lvUNyiyxZ66TGPHS5QIc4sv1k+D73z4g==", "chef"=>{"chefname"=>"Rosey", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create my account"}
No template found for ChefsController#create, rendering head :no_content
Completed 204 No Content in 119ms (ActiveRecord: 0.0ms)

Given that I am running the local server
And I am on the localhost:3000/signup
When I enter in the name, NO email, password & password_confirmation
Then I expect to see the errors

Instead I receive a 204 No Content error & see no errors on the local view

Started GET "/signup" for 33.33.33.1 at 2017-05-09 16:47:26 +0000
Cannot render console from 33.33.33.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
  ActiveRecord::SchemaMigration Load (1.0ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by ChefsController#new as HTML
  Rendering chefs/new.html.erb within layouts/application
  Rendered shared/_page_title.html.erb (0.6ms)
  Rendered shared/_errors.html.erb (1.4ms)
  Rendered chefs/new.html.erb within layouts/application (55.2ms)
  Rendered layouts/_navigation.html.erb (2.0ms)
  Rendered layouts/_messages.html.erb (1.6ms)
  Rendered layouts/_footer.html.erb (1.3ms)
Completed 200 OK in 2715ms (Views: 2658.1ms | ActiveRecord: 10.2ms)
Started POST "/chefs" for 33.33.33.1 at 2017-05-09 16:47:44 +0000
Cannot render console from 33.33.33.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by ChefsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"iemTuDyzszxzGbyGIJebE4PVcNyqPByufC4UPfH4dc98EpM1e6x32I+T2qkH7dlIsO5HMaQj+60VB4BSjVc9dg==", "chef"=>{"chefname"=>"hilma", "email"=>"", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create my account"}
No template found for ChefsController#create, rendering head :no_content
Completed 204 No Content in 123ms (ActiveRecord: 0.0ms)

When I run my tests I receive this error:

$ rails test

Running via Spring preloader in process 3082
Run options: --seed 41146
# Running:
........................F
Failure:
ChefsSignupTest#test_accept_valid_signup [/vagrant/ketokitchen/test/integration/chefs_signup_test.rb:22]:
"Chef.count" didn't change by 1.
Expected: 1
  Actual: 0
bin/rails test test/integration/chefs_signup_test.rb:20
F
Failure:
ChefsSignupTest#test_reject_an_invalid_signup [/vagrant/ketokitchen/test/integration/chefs_signup_test.rb:15]:
expecting <"chefs/new"> but rendering with <[]>
bin/rails test test/integration/chefs_signup_test.rb:9
30 runs, 73 assertions, 2 failures, 0 errors, 0 skips

When I run the $ rails console the Chef can be created

> @chef = Chef.new
 => #<Chef id: nil, chefname: nil, email: nil, created_at: nil, updated_at: nil, password_digest: nil> 
> @chef = Chef.new(chefname: "test", email: "[email protected]", password: "secretpswd", password_confirmation: "secretpswd")
 => #<Chef id: nil, chefname: "test", email: "[email protected]", created_at: nil, updated_at: nil, password_digest: "$2a$10$oyThgQldfqvhIlLvDhMRLOoCQZXW81qneuXjzDKYxTH..."> 
 > @chef
 => #<Chef id: nil, chefname: "test", email: "[email protected]", created_at: nil, updated_at: nil, password_digest: "$2a$10$oyThgQldfqvhIlLvDhMRLOoCQZXW81qneuXjzDKYxTH..."> 
> @chef.save
   (0.8ms)  BEGIN
  Chef Exists (6.0ms)  SELECT  1 AS one FROM "chefs" WHERE LOWER("chefs"."email") = LOWER($1) LIMIT $2  [["email", "[email protected]"], ["LIMIT", 1]]
  SQL (4.8ms)  INSERT INTO "chefs" ("chefname", "email", "created_at", "updated_at", "password_digest") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["chefname", "test"], ["email", "[email protected]"], ["created_at", 2017-05-09 17:05:58 UTC], ["updated_at", 2017-05-09 17:05:58 UTC], ["password_digest", "$2a$10$oyThgQldfqvhIlLvDhMRLOoCQZXW81qneuXjzDKYxTHNMUj5kVPAm"]]
   (1.4ms)  COMMIT
 => true 
> @chef.chefname
 => "test" 

AH HA, I just noticed that the chef ids are all nil. Why? Oh, they are not, :(

> Chef.all
  Chef Load (4.2ms)  SELECT "chefs".* FROM "chefs"
 => #<ActiveRecord::Relation [#<Chef id: 1, chefname: "Dean", email: "[email protected]", 
created_at: "2017-04-15 18:59:09", updated_at: "2017-05-01 22:59:14", password_digest: nil>, 
#<Chef id: 2, chefname: "jen diamond", email: "[email protected]", 
created_at: "2017-04-29 03:57:22", updated_at: "2017-05-05 18:57:25", 
password_digest: "$2a$10$YVJthUQoAU2kD.O3zECHMu/74BmOPzmjRSPupJJXe.n...">, 
#<Chef id: 3, chefname: "Toby", email: "[email protected]", created_at: "2017-05-08 23:10:55", 
updated_at: "2017-05-08 23:10:55", 
password_digest: "$2a$10$4FEv0WCKHpmWGD4Iwxh5mOrRSRNULA2bZlRsvDUDsSs...">, 
#<Chef id: 4, chefname: "test", email: "[email protected]", created_at: "2017-05-09 17:05:58", 
updated_at: "2017-05-09 17:05:58", 
password_digest: "$2a$10$oyThgQldfqvhIlLvDhMRLOoCQZXW81qneuXjzDKYxTH...">]> 

I deleted the first Chef with no password just in case it was causing issues but it made no change.

> @chef = Chef.find(1)
  Chef Load (0.7ms)  SELECT  "chefs".* FROM "chefs" WHERE "chefs"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
 => #<Chef id: 1, chefname: "Dean", email: "[email protected]", 
 created_at: "2017-04-15 18:59:09", updated_at: "2017-05-01 22:59:14", 
 password_digest: nil> > @chef
> @chef.destroy
   (1.0ms)  BEGIN
  SQL (0.7ms)  DELETE FROM "chefs" WHERE "chefs"."id" = $1  [["id", 1]]
   (0.8ms)  COMMIT
 => #<Chef id: 1, chefname: "Dean", email: "[email protected]", created_at: "2017-04-15 18:59:09", updated_at: "2017-05-01 22:59:14", password_digest: nil> 
> Chef.all
  Chef Load (1.0ms)  SELECT "chefs".* FROM "chefs"
 => #<ActiveRecord::Relation [
 #<Chef id: 2, chefname: "jen diamond", 
 email: "[email protected]", created_at: "2017-04-29 03:57:22", 
 updated_at: "2017-05-05 18:57:25", 
 password_digest: "$2a$10$YVJthUQoAU2kD.O3zECHMu/74BmOPzmjRSPupJJXe.n...">, 
 #<Chef id: 3, chefname: "Toby", email: "[email protected]", 
 created_at: "2017-05-08 23:10:55", updated_at: "2017-05-08 23:10:55", 
 password_digest: "$2a$10$4FEv0WCKHpmWGD4Iwxh5mOrRSRNULA2bZlRsvDUDsSs...">, 
 #<Chef id: 4, chefname: "test", email: "[email protected]", 
 created_at: "2017-05-09 17:05:58", updated_at: "2017-05-09 17:05:58", 
 password_digest: "$2a$10$oyThgQldfqvhIlLvDhMRLOoCQZXW81qneuXjzDKYxTH...">]> 

I thought I was about to solve this issue but I didn't.

I remembered that debugger can make your server hang if you don't close it properly. I thought that may be my issue but it wasn't. I restarted my virtual-box and ran the server again and received the same error.

I had put the debugger in the create method but my server never reached the debugger. It was stuck on the new method.

In my local view: local::3000/chefs/3

enter image description here

My code:

config/routes.rb

Rails.application.routes.draw do
  root 'pages#home'
  get 'pages/home', to: 'pages#home'
  resources :recipes
  get '/signup', to: 'chefs#new'
  resources :chefs, except: [:new]
end

app/models/chef.rb

class Chef < ApplicationRecord
  has_many :recipes
  validates :chefname, presence: true, length: { maximum: 30 }
  VALID_EMAIL_REGEX = /\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 50 }, uniqueness: {case_sensitive: false},
                    format: { with: VALID_EMAIL_REGEX}
  validates :password, presence: true, length: { minimum: 8 }
  has_secure_password
end

app/controllers/chefs_controller.rb

class ChefsController < ApplicationController
  def index
    @chefs = Chef.all
  end
  def new
    @chef = Chef.new
  end
  def create
    @chef = Chef.new(chef_params)
    if @chef.save
      flash[:success] = "Welcome #{@chef.chefname} to MyRecipes App!"
      redirect_to chef_path(@chef)
    else
      render 'new'
    end
  end
  def show
    @chef = Chef.find(params[:id])
  end
  private
  def chef_params
    params.require(:chef).permit(:chefname, :email, 
                                 :password, :password_confirmation)
  end
end

I added in the index and the show method just to be sure there was a template available.

app/views/chefs/new.html.erb

<%= render 'shared/page_title', title: 'Sign up' %>
<%= render 'shared/errors', obj: @chef %>
<div class="row">
  <div class="col-md-12">
    <%= form_for(@chef, :html => {class: "form-horizontal", role: "form"}) do |f| %>
      <div class="form-group">
        <div class="control-label col-md-2">
          <%= f.label :chefname, "Choose Chefname" %>
        </div>
        <div class="col-md-8">
          <%= f.text_field :chefname, class: "form-control", 
                           placeholder: "Enter your name", autofocus: true %>
        </div>
      </div>
      <div class="form-group">
        <div class="control-label col-md-2">
          <%= f.label :email %>
        </div>
        <div class="col-md-8">
          <%= f.email_field :email, class: "form-control", 
                            placeholder: "Enter your email" %>
        </div>
      </div>
      <div class="form-group">
        <div class="control-label col-md-2">
          <%= f.label :password %>
        </div>
        <div class="col-md-8">
          <%= f.password_field :password, class: "form-control", 
                               placeholder: "Choose a password" %>
        </div>
      </div>
      <div class="form-group">
        <div class="control-label col-md-2">
          <%= f.label :password_confirmation, "Confirm Password" %>
        </div>
        <div class="col-md-8">
          <%= f.password_field :password_confirmation, class: "form-control", 
                               placeholder: "Confirm password" %>
        </div>
      </div>
      <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
          <%= f.submit "Create my account", class: "btn btn-primary btn-lg btn-xlarge" %>
        </div>
      </div>
    <% end %>
    <div class="col-md-4 col-md-offset-4 colormatch">
      [ <%= link_to "Cancel request and return to recipes listing", recipes_path %> ]
    </div>
  </div>
</div>

app/views/chefs/show.html.erb

<p><%= @chef.chefname %></p>

test/integration/chef_signup_test.rb

require 'test_helper'
class ChefsSignupTest < ActionDispatch::IntegrationTest
  test 'should get the signup path' do
    get signup_path
    assert_response :success
  end
  test 'reject an invalid signup' do
    get signup_path
    assert_no_difference "Chef.count" do
      post chefs_path, params: { chef: { chefname: " ", email: " ",
                                 password: "password", password_confirmation: " " } }
    end
    assert_template 'chefs/new'
    assert_select 'h2.panel-title'
    assert_select 'div.panel-body'
  end
  test 'accept valid signup' do
    get signup_path
    assert_difference 'Chef.count', 1 do
      post chefs_path, params: {chef: {chefname: 'Jen', email: '[email protected]',
        password: 'secretpswd', password_confirmation: 'secretpswd'}}
    end
    follow_redirect!
    assert_template 'chefs/show'
    assert_not flash.empty?
  end
end

db/schema.rb

ActiveRecord::Schema.define(version: 20170505183354) do
  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"
  create_table "chefs", force: :cascade do |t|
    t.string   "chefname"
    t.string   "email"
    t.datetime "created_at",      null: false
    t.datetime "updated_at",      null: false
    t.string   "password_digest"
  end
  create_table "recipes", force: :cascade do |t|
    t.string   "name"
    t.text     "description"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.integer  "chef_id"
  end
end

Upvotes: 0

Views: 2213

Answers (1)

jendiamond
jendiamond

Reputation: 69

This ended up happening because I didn't exit out of Pry. I was stuck in an endless loop.

Upvotes: 2

Related Questions