Witam, chciałem spróbować swoich sił z polimorficznymi łączeniami i utknąłem na pierwszym teście
Modele:
class Comment < ActiveRecord::Base
belongs_to :comment_owner, polymorphic: true
end
class Announcement < ActiveRecord::Base
has_many :comments, as: :comment_owner
end
Routes:
resources :announcements do
resources :comments
end
Comment controller new action:
class CommentsController < ApplicationController
before_filter :find_comment_owner
def new
@comment = @comment_owner.comments.new
end
private
def find_comment_owner
# Switch comment_owner_string into constant
@klass = params[:comment_owner_type].capitalize.constantize
@comment_owner = @klass.find(params[:comment_owner_id])
end
end
I sam test:
describe 'New action test' do
let(:dummy_post) do
create(:announcement)
end
before :each do
get :new, comment_owner_id: dummy_post.id, comment_owner_type: dummy_post.class.name
end
it 'return redirect http status' do
expect(response).to have_http_status(:success)
end
end
No i oczywiście błąd:
ActionController::UrlGenerationError:
No route matches {:action=>"new", :comment_owner_id=>"27", :comment_owner_type=>"Announcement", :controller=>"comments"}
Z tego co biegam po internecie to w zasadzie zamiast podawać w teście comment_owner_id i comment_owner_type, powinienem podać comment_owner: dummy_post, ale wtedy wyłapuje mi z tego tylko id. Ktoś ma jakiś pomysł ?