私は現在立ち往生していますレールのマイケル・ハートル(railstutorial.org)からチュートリアル、第13章経路が一致しません:: "/ microposts"、:コントローラ=> "microposts"、:params => {:micropost => {:content => "Lorem ipsum"}}}
と、次の2つのエラー取得:私の知る限りでは、「アクション」の削除、GET、POSTのようなものでなければなりません
1) Error:
MicropostsControllerTest#test_should_redirect_create_when_not_logged_in:
ActionController::UrlGenerationError: No route matches {:action=>"/microposts", :controller=>"microposts", :params=>{:micropost=>{:content=>"Lorem ipsum"}}}
test/controllers/microposts_controller_test.rb:11:in 'block (2 levels) in <class:MicropostsControllerTest>'
test/controllers/microposts_controller_test.rb:10:in 'block in <class:MicropostsControllerTest>'
2) Error:
MicropostsControllerTest#test_should_redirect_destroy_when_not_logged_in:
ActionController::UrlGenerationError: No route matches {:action=>"/microposts/499495288", :controller=>"microposts"}
test/controllers/microposts_controller_test.rb:18:in 'block (2 levels) in <class:MicropostsControllerTest>'
test/controllers/microposts_controller_test.rb:17:in 'block in <class:MicropostsControllerTest>'
を しかし、私はここに「マイクロポスト」と言っているのか分かりません。 microposts_controller_test.rbの
内容:micropost_controller.rbの
require 'test_helper'
class MicropostsControllerTest < ActionController::TestCase
def setup
@micropost = microposts(:orange)
end
test 'should redirect create when not logged in' do
assert_no_difference 'Micropost.count' do
post microposts_path, params: {micropost: {content: 'Lorem ipsum'}}
end
assert_redirected_to login_url
end
test 'should redirect destroy when not logged in' do
assert_no_difference 'Micropost.count' do
delete micropost_path(@micropost)
end
assert_redirected_to login_url
end
end
内容:routes.rbをの
class MicropostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
flash[:success] = 'Micropost created!'
redirect_to root_url
else
render 'static_pages/home'
end
end
def destroy
end
private
def micropost_params
params.require(:micropost).permit(:content)
end
end
内容:すべてのヘルプは高く評価され
Rails.application.routes.draw do
root 'static_pages#home'
get '/help' => 'static_pages#help'
get '/about' => 'static_pages#about'
get '/contact' => 'static_pages#contact'
get '/signup' => 'users#new'
get '/login' => 'sessions#new'
post '/login' => 'sessions#create'
delete '/logout' => 'sessions#destroy'
resources :users
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :microposts, only: [:create, :destroy]
end
。
おかげ
編集:
micropost_controller_test.rbで、それはしてきたはずです。
class MicropostsControllerTest < ActionDispatch::IntegrationTest
代わり
class MicropostControllerTest < ActionCntroller::TestCase
は、テストケースを書く前に、あなたがあなたの上でそれを確認しなかった.... UIで働いて、このシナリオですUI? – SnehaT
テストで問題が発生しました:コントローラにルートがありません:アクション=> "/ microposts"でテストがヒットしようとしていて、ルートファイルが表示されていればリソースを確認します:マイクロポストのみ:[:create、 :破壊]、そのためのルートはありません。第二に、それはリダイレクトする必要がありますあなたのコントローラの方法でリダイレクトする必要があります破棄後です。最終的にテストは失敗します。 –