ActionView::MissingTemplate: ActionView::MissingTemplate: 缺少模板/成员 {:locale=>[:en], :formats=>[:html],
ActionView::MissingTemplate: ActionView::MissingTemplate: Missing template /members with {:locale=>[:en], :formats=>[:html],
集成测试返回 'Missing Template' 错误。 /members
上的 microposts_interface_test.rb
错误。可能是因为 /members 视图使用了 MicropostsController 但我不确定如何修复测试。
ERROR["test_micropost_interface", MicropostsInterfaceTest, 2015-06-19 06:40:32 +0800]
test_micropost_interface#MicropostsInterfaceTest (1434667232.75s)
ActionView::MissingTemplate: ActionView::MissingTemplate: Missing template /members with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :haml, :jbuilder]}. Searched in:
* "/home/me/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates"
* "/home/me/Development/my_app/app/views"
* "/home/me/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/web-console-2.0.0.beta3/app/views"
* "/home/me/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/phrasing-4.0.0rc9/app/views"
app/controllers/microposts_controller.rb:14:in `create'
test/integration/microposts_interface_test.rb:16:in `block (2 levels) in <class:MicropostsInterfaceTest>'
test/integration/microposts_interface_test.rb:15:in `block in <class:MicropostsInterfaceTest>'
app/controllers/microposts_controller.rb:14:in `create'
test/integration/microposts_interface_test.rb:16:in `block (2 levels) in <class:MicropostsInterfaceTest>'
test/integration/microposts_interface_test.rb:15:in `block in <class:MicropostsInterfaceTest>'
微博接口测试:
require 'test_helper'
class MicropostsInterfaceTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
end
test "micropost interface" do
log_in_as(@user)
get members_path
assert_select 'div.pagination'
assert_select 'input[type=file]'
# Invalid submission
assert_no_difference 'Micropost.count' do
post microposts_path, micropost: { content: "" }
end
assert_select 'div#error_explanation'
# Valid submission
content = "This micropost really ties the room together"
picture = fixture_file_upload('test/fixtures/rails.png', 'image/png')
assert_difference 'Micropost.count', 1 do
post microposts_path, micropost: { content: content, picture: picture }
end
assert assigns(:micropost).picture?
assert_redirected_to root_url
follow_redirect!
assert_match content, response.body
# Delete a post.
assert_select 'a', text: 'delete'
first_micropost = @user.microposts.paginate(page: 1).first
assert_difference 'Micropost.count', -1 do
delete micropost_path(first_micropost)
end
# Visit a different user.
get user_path(users(:archer))
assert_select 'a', text: 'delete', count: 0
end
test "micropost sidebar count" do
log_in_as(@user)
get members_path
assert_match "#{@user.microposts.count} microposts", response.body
# User with zero microposts
other_user = users(:mallory)
log_in_as(other_user)
get members_path
assert_match "0 microposts", response.body
# Create a micropost.
other_user.microposts.create!(content: "A micropost")
get members_path
assert_match "1 micropost", response.body
end
end
更新:
# controller/members_controller.rb
class MembersController < ApplicationController
before_filter :logged_in_user
def index
@micropost = current_user.microposts.build
@feed_items = current_user.feed.paginate(page: params[:page])
end
end
我确认在 app/views/members/index.html.erb
有一个特定的索引视图。
更新二:
# controller/microposts_controller.rb
class MicropostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to members_path
else
@feed_items = []
render members_path
end
end
def destroy
@micropost.destroy
flash[:success] = "Micropost deleted"
redirect_to request.referrer || members_path
end
private
def micropost_params
params.require(:micropost).permit(:content, :picture)
end
def correct_user
@micropost = current_user.microposts.find_by(id: params[:id])
redirect_to members_path if @micropost.nil?
end
end
在 MicropostController 的创建操作中使用 render "members/index"
而不是 render members_path
。请注意,render 不会加载 members/index 中所需的变量,它只会加载模板,因此添加 @micropost = []
就像您对 @feed_items = []
所做的那样,这样如果您从视图中调用这些变量,您不要收到找不到变量@microposts 的错误。或者你可以简单地 redirect_to members_path
.
对我有用:
render template: 'template', formats: [:html]
集成测试返回 'Missing Template' 错误。 /members
上的 microposts_interface_test.rb
错误。可能是因为 /members 视图使用了 MicropostsController 但我不确定如何修复测试。
ERROR["test_micropost_interface", MicropostsInterfaceTest, 2015-06-19 06:40:32 +0800]
test_micropost_interface#MicropostsInterfaceTest (1434667232.75s)
ActionView::MissingTemplate: ActionView::MissingTemplate: Missing template /members with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :haml, :jbuilder]}. Searched in:
* "/home/me/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates"
* "/home/me/Development/my_app/app/views"
* "/home/me/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/web-console-2.0.0.beta3/app/views"
* "/home/me/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/phrasing-4.0.0rc9/app/views"
app/controllers/microposts_controller.rb:14:in `create'
test/integration/microposts_interface_test.rb:16:in `block (2 levels) in <class:MicropostsInterfaceTest>'
test/integration/microposts_interface_test.rb:15:in `block in <class:MicropostsInterfaceTest>'
app/controllers/microposts_controller.rb:14:in `create'
test/integration/microposts_interface_test.rb:16:in `block (2 levels) in <class:MicropostsInterfaceTest>'
test/integration/microposts_interface_test.rb:15:in `block in <class:MicropostsInterfaceTest>'
微博接口测试:
require 'test_helper'
class MicropostsInterfaceTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
end
test "micropost interface" do
log_in_as(@user)
get members_path
assert_select 'div.pagination'
assert_select 'input[type=file]'
# Invalid submission
assert_no_difference 'Micropost.count' do
post microposts_path, micropost: { content: "" }
end
assert_select 'div#error_explanation'
# Valid submission
content = "This micropost really ties the room together"
picture = fixture_file_upload('test/fixtures/rails.png', 'image/png')
assert_difference 'Micropost.count', 1 do
post microposts_path, micropost: { content: content, picture: picture }
end
assert assigns(:micropost).picture?
assert_redirected_to root_url
follow_redirect!
assert_match content, response.body
# Delete a post.
assert_select 'a', text: 'delete'
first_micropost = @user.microposts.paginate(page: 1).first
assert_difference 'Micropost.count', -1 do
delete micropost_path(first_micropost)
end
# Visit a different user.
get user_path(users(:archer))
assert_select 'a', text: 'delete', count: 0
end
test "micropost sidebar count" do
log_in_as(@user)
get members_path
assert_match "#{@user.microposts.count} microposts", response.body
# User with zero microposts
other_user = users(:mallory)
log_in_as(other_user)
get members_path
assert_match "0 microposts", response.body
# Create a micropost.
other_user.microposts.create!(content: "A micropost")
get members_path
assert_match "1 micropost", response.body
end
end
更新:
# controller/members_controller.rb
class MembersController < ApplicationController
before_filter :logged_in_user
def index
@micropost = current_user.microposts.build
@feed_items = current_user.feed.paginate(page: params[:page])
end
end
我确认在 app/views/members/index.html.erb
有一个特定的索引视图。
更新二:
# controller/microposts_controller.rb
class MicropostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to members_path
else
@feed_items = []
render members_path
end
end
def destroy
@micropost.destroy
flash[:success] = "Micropost deleted"
redirect_to request.referrer || members_path
end
private
def micropost_params
params.require(:micropost).permit(:content, :picture)
end
def correct_user
@micropost = current_user.microposts.find_by(id: params[:id])
redirect_to members_path if @micropost.nil?
end
end
在 MicropostController 的创建操作中使用 render "members/index"
而不是 render members_path
。请注意,render 不会加载 members/index 中所需的变量,它只会加载模板,因此添加 @micropost = []
就像您对 @feed_items = []
所做的那样,这样如果您从视图中调用这些变量,您不要收到找不到变量@microposts 的错误。或者你可以简单地 redirect_to members_path
.
对我有用:
render template: 'template', formats: [:html]