1

我想MINITEST嵌套資源控制器和我不斷收到以下錯誤:MINITEST嵌套的資源控制器

1) Error: 
PostsControllerTest#test_should_show_post: 
ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"posts", :id=>"980190962"} 
    test/controllers/posts_controller_test.rb:28:in `block in <class:PostsControllerTest>' 


我已經試過
我試着與我搞亂航線(名稱空間,模塊等),並在我的測試中明確寫入獲取請求,但尚未找到解決方案。我甚至試圖生成一個腳手架,看看應用程序如何創建測試,但無濟於事。我還瀏覽了博客&書籍(Makandra的「Growing Rails Applications」,Michael Hartl的教程),但也沒有發現任何內容。

配置/ routes.rb中

Rails.application.routes.draw do 
    resources :blogs do 
    resources :posts 
    end 
end 


控制器/ posts_controller.rb

class PostsController < ApplicationController 
    before_action :set_post, only: [:show, :edit, :update, :destroy] 

    # GET /posts 
    # GET /posts.json 
    def index 
    @posts = Post.all 
    end 

    # GET /posts/1 
    # GET /posts/1.json 
    def show 
    @post = Post.find(params[:id]) 
    end 

    # GET /posts/new 
    def new 
    @post = Post.new 
    end 

    # GET /posts/1/edit 
    def edit 
    end 

    # POST /posts 
    # POST /posts.json 
    def create 
    @post = Post.new(post_params) 

    respond_to do |format| 
     if @post.save 
     format.html { redirect_to @post, notice: 'Post was successfully created.' } 
     format.json { render :show, status: :created, location: @post } 
     else 
     format.html { render :new } 
     format.json { render json: @post.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /posts/1 
    # PATCH/PUT /posts/1.json 
    def update 
    respond_to do |format| 
     if @post.update(post_params) 
     format.html { redirect_to @post, notice: 'Post was successfully updated.' } 
     format.json { render :show, status: :ok, location: @post } 
     else 
     format.html { render :edit } 
     format.json { render json: @post.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /posts/1 
    # DELETE /posts/1.json 
    def destroy 
    @post.destroy 
    respond_to do |format| 
     format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_post 
     @blog = Blog.find(params[:blog_id]) 
     @post = @blog.posts.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def post_params 
     params.require(:post).permit(:post_title, :body, :blog_id) 
    end 
end 

測試/ posts_controller_test.rb

require 'test_helper' 

class PostsControllerTest < ActionController::TestCase 
    setup do 
    @post = posts(:one) 
    end 

    test "should get index" do 
    get :index 
    assert_response :success 
    assert_not_nil assigns(:posts) 
    end 

    test "should get new" do 
    get :new 
    assert_response :success 
    end 

    test "should create post" do 
    assert_difference('Post.count') do 
     post :create, post: { blog_id: @post.blog_id, body: @post.body, post_title: @post.post_title } 
    end 

    assert_redirected_to post_path(assigns(:post)) 
    end 

    test "should show post" do 
    get :show, id: @post 
    assert_response :success 
    end 

    test "should get edit" do 
    get :edit, id: @post 
    assert_response :success 
    end 

    test "should update post" do 
    patch :update, id: @post, post: { blog_id: @post.blog_id, body: @post.body, post_title: @post.post_title } 
    assert_redirected_to post_path(assigns(:post)) 
    end 

    test "should destroy post" do 
    assert_difference('Post.count', -1) do 
     delete :destroy, id: @post 
    end 

    assert_redirected_to posts_path 
    end 
end 


有什麼建議嗎?如果我添加了一個文件夾結構,我把帖子文件放在博客文件夾下面(下面),我是否需要以不同方式編寫我的控制器測試?我知道這可能是這樣一個簡單的應用程序矯枉過正,但我​​也想實現這些原則在另一個應用程序的整體設計模式。

app/controllers/blogs 
app/controllers/blogs_controllers 
app/controllers/blogs/posts_controllers 

回答

2

由於PostBlog下築巢,網址方案會有點像這樣:

POST /blogs/:blog_id/posts - CREATE 
GET /blogs/:blog_id/posts - INDEX 
DELETE /blogs/:blog_id/posts - DESTROY 
SHOW /blogs/:blog_id/posts/:id - SHOW 

上面的並不詳盡列表(rake routes的完整列表),但我認爲這會給你有一個想法,params[:blog_id]現在是一個必需的參數來訪問任何職位。根據您的這個新獲得的洞察力,你很快就會發現,你將需要重寫test_should_show_post像這樣(在blog_id PARAM傳遞)

test "should show post" do 
    get :show, id: @post, blog_id: @post.blog_id 
    assert_response :success 
end 

因此,與嵌套資源的想法是,孩子的資源不能沒有存在對父資源的引用,則blog_id將能夠被要求參數PostsController

+0

謝謝!就是這樣。我試着專門調用這個url,但它沒有工作:get:show,「/blog/#{@post.blog_id}/posts/#{@post.id}」 – eternal44