I'm on a video show page (which belongs to a post). The link_to delete works just fine until I create a comment on that video show page. I have a series of capybara tests that validate that the link_to delete video is working until I create a comment on that video at which point rails returns...
Failure/Error: <%= link_to "Delete Video", post_video_path(@video), method: :delete %>
ActionView::Template::Error:
No route matches
Not sure what's going on here or how to fix it. I stuck a pry in and the first two tests that hit it I checked the path...
post_video_path(@video)
which returned a valid path, e.g.
[1] pry(#<#<Class:0x007f891941dee0>>)> post_video_path(@video)
=> "/posts/1/videos/1"
When the spec instantiates a comment, the path reads as follows...
[1] pry(#<#<Class:0x007f891c2fd0a8>>)> post_video_path(@video)
ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"videos", :post_id=>#<Video id: 1, user_id: 1, post_id: 1, title: "18title", url: "https://www.youtube.com/watch?v=tYm_182oCVdSM", embed_id: "https://www.youtube.com/watch?v=tYm_182oCVdSM.spli...", tags: nil, created_at: "2016-10-16 22:12:30", updated_at: "2016-10-16 22:12:30">, :video_id=>"1"} missing required keys: [:id]
videos_controller.rb
def show
@video = Video.find(params[:id])
@user = @video.user
@post = @video.post
@comment = Comment.new
@comments = @video.comments
end
videos/show.html.erb
<%= @video.title %>
<%= content_tag(:iframe, nil, src: "//www.youtube.com/embed/#{@video.embed_id}") %>
<% binding.pry %>
<%= link_to "Delete Video", post_video_path(@video), method: :delete %>
<%= link_to('Back', user_post_path(@user, @post)) %>
<h3>Comments</h3>
<%= form_for [@video, @comment] do |f| %>
<%= f.label(:body, "Comment") %>
<%= f.text_area(:body) %>
<%= f.submit("Submit Comment") %>
<% end %>
<% @comments.each do |comment| %>
<%= comment.body %>
<%= comment.user %>
<% end %>
routes.rb
Rails.application.routes.draw do
devise_for :users
resources :users, only: [] do
collection do
get '/show_profile', to: 'users#show_profile', as: 'my_profile'
get '/show_log', to: 'users#show_log', as: 'my_log'
end
resources :posts, only: [:new, :create, :show]
end
resources :posts do
resources :videos
end
resources :videos do
resources :comments
end
root 'home#index'
end
models/comment.rb
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :video
validates :body, presence: true
end
Please let me know if you would like to see any particular file or code.
Thanks!