Rails 5 204无内容

时间:2017-05-09 17:59:57

标签: ruby-on-rails-5 http-status-code-204

找不到ChefsController #create的模板,渲染头:no_content 已完成204无内容

这是Rails 5中的一个有助于API的问题

我看过这篇文章和其他几篇文章,但我仍然被困住了。 https://www.rubyplus.com/articles/2771-Rails-5-Basics-View-to-Model

这是我尝试调试此过程的过程:

鉴于我正在运行本地服务器
我在localhost:3000 /注册
当我输入姓名,电子邮件,密码和password_confirmation
然后我希望创建一个新的厨师

我收到了204 No Content错误:

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"=>"rosey@email.com", "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)

鉴于我正在运行本地服务器
我在localhost:3000 /注册
当我输入名称时,没有电子邮件,密码和password_confirmation
然后我希望看到错误

相反,我收到了204 No Content错误&看到本地视图没有错误

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)

当我运行测试时,我收到此错误:

$ 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

当我运行$ rails控制台时,可以创建Chef

> @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: "test@example.com", password: "secretpswd", password_confirmation: "secretpswd")
 => #<Chef id: nil, chefname: "test", email: "test@example.com", created_at: nil, updated_at: nil, password_digest: "$2a$10$oyThgQldfqvhIlLvDhMRLOoCQZXW81qneuXjzDKYxTH..."> 
 > @chef
 => #<Chef id: nil, chefname: "test", email: "test@example.com", 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", "test@example.com"], ["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", "test@example.com"], ["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,我刚注意到厨师ID都是零。为什么? 哦,他们不是,:(

> Chef.all
  Chef Load (4.2ms)  SELECT "chefs".* FROM "chefs"
 => #<ActiveRecord::Relation [#<Chef id: 1, chefname: "Dean", email: "deananes@gmail.com", 
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: "thejendiamond@gmail.com", 
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: "toby@email.com", 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: "test@example.com", created_at: "2017-05-09 17:05:58", 
updated_at: "2017-05-09 17:05:58", 
password_digest: "$2a$10$oyThgQldfqvhIlLvDhMRLOoCQZXW81qneuXjzDKYxTH...">]> 

我删除了第一个没有密码的Chef,以防它引起问题,但没有改变。

> @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: "deananes@gmail.com", 
 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: "deananes@gmail.com", 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: "thejendiamond@gmail.com", 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: "toby@email.com", 
 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: "test@example.com", 
 created_at: "2017-05-09 17:05:58", updated_at: "2017-05-09 17:05:58", 
 password_digest: "$2a$10$oyThgQldfqvhIlLvDhMRLOoCQZXW81qneuXjzDKYxTH...">]> 

我以为我即将解决这个问题,但我没有。

我记得如果你没有正确关闭它,调试器会让你的服务器挂起。我认为这可能是我的问题,但事实并非如此。我重新启动了我的虚拟机并再次运行服务器并收到了同样的错误。

我已将调试器放在create方法中,但我的服务器从未到达调试器。它被困在新方法上。

在我的本地视图中:local :: 3000 / chefs / 3

enter image description here

我的代码:

配置/ 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

应用/模型/ 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

应用/控制器/ 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

我在索引和show方法中添加了以确保有可用的模板。

应用/视图/厨师/ 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>

应用/视图/厨师/ show.html.erb

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

测试/集成/ 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: 'thejendiamond@gmail.com',
        password: 'secretpswd', password_confirmation: 'secretpswd'}}
    end
    follow_redirect!
    assert_template 'chefs/show'
    assert_not flash.empty?
  end
end

分贝/ 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

1 个答案:

答案 0 :(得分:2)

这最终发生了,因为我没有退出Pry。我被困在一个无尽的循环中。