切换到多态关联后assert_template失败

时间:2016-09-05 11:08:43

标签: ruby-on-rails polymorphic-associations

这可能是一件非常简单的事情,但我简直无法理解。这是我的情况:在添加一个新的数据库后,我意识到最好更改已经存在的数据库,以便它现在使用多态关联。当然,在我改变之后,其他一切都变成了地狱。我设法解决了一个小问题。集成测试现在失败了,我简直不知道为什么,因为页面本身仍然完美无缺......

def setup
  @user=users(:michael)
  @clip=clips(:Michi_clip_one)
  log_in_as(@user)
end

test "successful edit" do
  get edit_clip_path(@clip)
  assert_template "clips/edit"
  ....


FAIL["test_successful_edit", ClipEditTest, 2.686249095015228]
test_successful_edit#ClipEditTest (2.69s)
expecting <"clips/edit"> but rendering with <[]>
test/integration/clip_edit_test.rb:13:in `block in <class:ClipEditTest>'

由于我真的不知道问题出在哪里,我只想添加一些我认为可能与之相关的内容。

Michi_clip_one:
  adress: "nfWlot6h_JM"
  owner: michael
  description: "MyText"

class Clip < ApplicationRecord
  belongs_to :owner, polymorphic: true
  default_scope -> { order(created_at: :desc) }
  validates :adress, presence: true, uniqueness: {scope: :owner}
  before_save { self.adress=adress.split("=").last }
  validates :owner, presence: true
end

class ClipsController < ApplicationController

  before_action :logged_in_user, only: [:new, :destroy, :edit, :update, :create]
  before_action :correct_user, only: [:destroy, :edit, :update]

  def edit
    @clip=current_user.clips.find_by(id: params[:id])
  end

如果您需要其他代码行来解决问题,我们很乐意添加它。

1 个答案:

答案 0 :(得分:0)

好的,明白了。使用多态关联时,需要通过添加相应的模型来稍微更改灯具。

Michi_clip_one:
  adress: "nfWlot6h_JM"
  owner: michael (User)
  description: "MyText"