【问题标题】:ActionView::MissingTemplate: ActionView::MissingTemplate: Missing template /members with {:locale=>[:en], :formats=>[:html],ActionView::MissingTemplate: ActionView::MissingTemplate: 缺少模板/members with {:locale=>[:en], :formats=>[:html],
【发布时间】:2015-06-20 04:13:52
【问题描述】:

集成测试返回“缺少模板”错误。 /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>'

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 有一个特定的索引视图。

更新 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

【问题讨论】:

  • 您能否发布针对该特定网址的操作方法代码?您还可以确保特定的操作方法具有操作视图文件吗?
  • 你能发布你的微博控制器吗?
  • @LuisMenjivar,我已经添加了 MicropostsController。

标签: ruby-on-rails model-view-controller tdd


【解决方案1】:

在 MicropostController 的创建操作中,使用 render "members/index" 而不是 render members_path。请注意,render 不会加载成员/索引中所需的变量,它只会加载模板,因此添加 @micropost = [] 就像您为 @feed_items = [] 所做的那样,这样如果您从视图中调用这些变量,您就不会收到错误找不到变量@microposts。或者你可以简单地redirect_to members_path

【讨论】:

    【解决方案2】:

    这对我很有用:

    render template: 'template', formats: [:html]
    

    【讨论】:

      猜你喜欢
      • 2020-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多