我想最小化一个嵌套的资源控制器,我不断收到以下错误:
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>'
我尝试了什么
我试着弄乱我的路线(名称空间,模块等),并在我的测试中明确写入get请求,但没有找到解决方案。我甚至尝试生成一个脚手架,看看应用程序如何创建测试,但无济于事。我还浏览了博客&amp;书籍(Makandra的“成长Rails应用程序”,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
答案 0 :(得分:2)
由于Post
嵌套在Blog
下,因此网址方案有点像这样:
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
将成为必需参数