ActionController::UrlGenerationError: No route matches {:action=>"edit", :controller=>"posts"} - rake test fails but app works correctly
ActionController::UrlGenerationError: No route matches {:action=>"edit", :controller=>"posts"} - rake test fails but app works correctly
当我 运行 我的 rake 测试出现了两个错误。但是,当我 运行 我的博客时,索引、显示、新建和编辑视图都可以正常工作,并且不会在浏览器中产生错误。为什么我的测试失败但我的博客却没有?我已经查看了关于这个主题的其他答案,但大多数似乎与方法中的链接或嵌套元素有关。我两者都没有。
1) Error:
PostsControllerTest#test_should_get_edit:
ActionController::UrlGenerationError: No route matches {:action=>"edit", :controller=>"posts"}
test/controllers/posts_controller_test.rb:25:in `block in <class:PostsControllerTest>'
2) Error:
PostsControllerTest#test_should_get_show:
ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"posts"}
test/controllers/posts_controller_test.rb:15:in `block in <class:PostsControllerTest>'
这是我的 show.html.erb
<h1><%= @post.title %></h1>
<h3> Posted: <%= @post.created_at.strftime("%I:%M %p") %> by Tarrence</h3>
<p><%= @post.body %></p>
</div>
<%= render 'sidebar' %>
这是我的 edit.html.erb
<h1>Edit</h1>
<%= render 'form' %>
</div>
这是我的_form.html.erb
<%= form_for(@post) do |f| %>
<%= f.label :title %>
<%= f.text_field :title, class: "form-control" %>
<%= f.label :topic %>
<%= f.text_field :topic, class: "form-control" %>
<%= f.label :body %>
<%= f.text_area :body, class: "form-control" %>
<%= f.submit "Post", class: "btn btn-primary submit" %>
<% end %>
这是我的 index.html.erb
<div class="blog-post">
<% @posts.each do |post| %>
<h3><%= post.title %></h3>
<span>Posted by Tarrence <%= post.created_at.strftime("at %I:%M %p") %></span><br>
<span>Topic: <%= post.topic %></span>
<% if post.body.length > 100 %>
<p><%= truncate(post.body, length: 100, separator: ' ') %>
<%= link_to "Read More", post_path(post) %></p>
<% else %>
<p><%= post.body %></p>
<% end %>
<hr>
<% end %>
</div>
<nav class="pager">
<%= will_paginate @posts %>
</nav>
</div>
这是我的routes.rb
root 'posts#index'
#get 'post' => 'posts#show'
#get 'post/edit' => 'posts#edit'
resources :posts
这是我的posts_controller.rb
class PostsController < ApplicationController
def index
# @posts = Post.all
@posts = Post.paginate(:page => params[:page], :per_page => 2)
end
def show
@post = Post.find(params[:id])
end
def new
@post = Post.new
end
def create
logger.info "---params ==> #{params.inspect}---"
@post = Post.new(post_params)
if @post.save
redirect_to root_path
else
render 'new'
end
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update_attributes(post_params)
redirect_to post_path(@post)
else
render 'edit'
end
end
def destroy
end
private
def post_params
params.require(:post).permit(:title, :topic, :body)
end
end
这是我的测试
require 'test_helper'
class PostsControllerTest < ActionController::TestCase
def setup
@post = Post.create(title: "This is a post", topic: "Random", body: "This is some text in the body.")
end
test "should get index" do
get :index
assert_response :success
end
test "should get show" do
get :show, id: @post.id
assert_response :success
end
test "should get new" do
get :new
assert_response :success
end
test "should get edit" do
get :edit, id: @post.id
assert_response :success
end
test "should be valid" do
assert @post.valid?
end
end
看来你唯一没有显示的是错误的地方 - 你的测试!
...不过没关系,因为很清楚问题出在哪里,您没有在测试调用中提供 :id
,即。你会在 test/controllers/posts_controller_test.rb
做这样的事情
get :show
get :edit
...而你应该做的是这样的:
post = Post.create attributes: "required", for: "a post" # or maybe you use FactoryGirl, you get the idea though - create a post
get :show, id: post.id
当我 运行 我的 rake 测试出现了两个错误。但是,当我 运行 我的博客时,索引、显示、新建和编辑视图都可以正常工作,并且不会在浏览器中产生错误。为什么我的测试失败但我的博客却没有?我已经查看了关于这个主题的其他答案,但大多数似乎与方法中的链接或嵌套元素有关。我两者都没有。
1) Error:
PostsControllerTest#test_should_get_edit:
ActionController::UrlGenerationError: No route matches {:action=>"edit", :controller=>"posts"}
test/controllers/posts_controller_test.rb:25:in `block in <class:PostsControllerTest>'
2) Error:
PostsControllerTest#test_should_get_show:
ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"posts"}
test/controllers/posts_controller_test.rb:15:in `block in <class:PostsControllerTest>'
这是我的 show.html.erb
<h1><%= @post.title %></h1>
<h3> Posted: <%= @post.created_at.strftime("%I:%M %p") %> by Tarrence</h3>
<p><%= @post.body %></p>
</div>
<%= render 'sidebar' %>
这是我的 edit.html.erb
<h1>Edit</h1>
<%= render 'form' %>
</div>
这是我的_form.html.erb
<%= form_for(@post) do |f| %>
<%= f.label :title %>
<%= f.text_field :title, class: "form-control" %>
<%= f.label :topic %>
<%= f.text_field :topic, class: "form-control" %>
<%= f.label :body %>
<%= f.text_area :body, class: "form-control" %>
<%= f.submit "Post", class: "btn btn-primary submit" %>
<% end %>
这是我的 index.html.erb
<div class="blog-post">
<% @posts.each do |post| %>
<h3><%= post.title %></h3>
<span>Posted by Tarrence <%= post.created_at.strftime("at %I:%M %p") %></span><br>
<span>Topic: <%= post.topic %></span>
<% if post.body.length > 100 %>
<p><%= truncate(post.body, length: 100, separator: ' ') %>
<%= link_to "Read More", post_path(post) %></p>
<% else %>
<p><%= post.body %></p>
<% end %>
<hr>
<% end %>
</div>
<nav class="pager">
<%= will_paginate @posts %>
</nav>
</div>
这是我的routes.rb
root 'posts#index'
#get 'post' => 'posts#show'
#get 'post/edit' => 'posts#edit'
resources :posts
这是我的posts_controller.rb
class PostsController < ApplicationController
def index
# @posts = Post.all
@posts = Post.paginate(:page => params[:page], :per_page => 2)
end
def show
@post = Post.find(params[:id])
end
def new
@post = Post.new
end
def create
logger.info "---params ==> #{params.inspect}---"
@post = Post.new(post_params)
if @post.save
redirect_to root_path
else
render 'new'
end
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update_attributes(post_params)
redirect_to post_path(@post)
else
render 'edit'
end
end
def destroy
end
private
def post_params
params.require(:post).permit(:title, :topic, :body)
end
end
这是我的测试
require 'test_helper'
class PostsControllerTest < ActionController::TestCase
def setup
@post = Post.create(title: "This is a post", topic: "Random", body: "This is some text in the body.")
end
test "should get index" do
get :index
assert_response :success
end
test "should get show" do
get :show, id: @post.id
assert_response :success
end
test "should get new" do
get :new
assert_response :success
end
test "should get edit" do
get :edit, id: @post.id
assert_response :success
end
test "should be valid" do
assert @post.valid?
end
end
看来你唯一没有显示的是错误的地方 - 你的测试!
...不过没关系,因为很清楚问题出在哪里,您没有在测试调用中提供 :id
,即。你会在 test/controllers/posts_controller_test.rb
get :show
get :edit
...而你应该做的是这样的:
post = Post.create attributes: "required", for: "a post" # or maybe you use FactoryGirl, you get the idea though - create a post
get :show, id: post.id