Reputation: 345
I am getting a "No route matches [POST]" when submitting a new form.
### /routes.rb ###
resources :artists, controller: 'artists/artists', only: [:show] do
member do
resources :videos, controller: 'artists/videos', only: [:index, :new, :create, :edit, :update]
end
end
### /artists/videos/videos_controller.rb ###
class Artists::VideosController < ApplicationController
before_action :set_artist
def new
@video = ArtistVideo.new
end
def create
@video = @artist.create_artist_video(video_params)
if @video.save
redirect_to current_artist
else
render 'new'
end
end
private
def set_artist
@artist = current_artist
end
end
### /artists/videos/new.html.erb ###
<%= form_for(@video, url: new_video_path) do |f| %>
<%= f.label :video_title, "title", class: "label" %>
<%= f.text_field :video_title, class: "text-field" %>
<%= f.label :video_description, "decription", class: "label" %>
<%= f.text_field :video_description, class: "text-field" %>
<%= f.label :youtube_video_url, "youtube url", class: "label" %>
<%= f.text_field :youtube_video_url, class: "text-field" %>
<%= f.submit "add video", class: "submit-button" %>
<% end %>
### rake routes ###
videos_path GET /artists/:id/videos(.:format) artists/videos#index
POST /artists/:id/videos(.:format) artists/videos#create
new_video_path GET /artists/:id/videos/new(.:format) artists/videos#new
edit_video_path GET /artists/:id/videos/:id/edit(.:format) artists/videos#edit
video_path PATCH /artists/:id/videos/:id(.:format) artists/videos#update
PUT /artists/:id/videos/:id(.:format) artists/videos#update
So not sure what I'm doing wrong here. I've tried taking :index completely out so videos_path uses the post method, but I still have the same problem.
I'm using the has_many method linking videos to artists, if that even matters.
Not sure if it's the routs or the controller code that's wrong. Any help would be appreciated.
Upvotes: 1
Views: 634
Reputation: 4171
You're specifying a path url: new_video_path
but that is for videos#new
and what you want is the create path, which is a post to videos_path(@artist)
. Since it's a nested resource, the path has to have the artist_id which it can get from the @artist
instance.
But, the simpler way to do this is like so:
form_for[@artist, @video] do |f|
Upvotes: 4