An integration test is returning a 'Missing Template' error. The microposts_interface_test.rb
errors on /members
. It may be because the /members view uses the MicropostsController but I'm unsure how to fix the test.
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>'
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
Update:
# 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
I confirm there is a specific index view at app/views/members/index.html.erb
.
Update 2:
# 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
In the create action in the MicropostController use render "members/index"
instead of render members_path
. Note that render does not load the variables needed in members/index, it only loads the template so add @micropost = []
just like you did for @feed_items = []
so that if you are calling those variables from the view you don't get an error of can't find variable @microposts. Or you can simply redirect_to members_path
.