resourcesとは異なるpathへ移動してしまう
解決したいこと
Ruby on RailsでtwitterのようなWebアプリをつくっています。
https://www.youtube.com/watch?v=7xMwwnIGbBU&list=LL&index=23&t=506s
上記の動画からコメント機能を実装しています。
動画のphotoの配下にcommenetを置いているように、movieの配下にcommentを置き、form_withで送信するようにしているのですがルーティングエラーが発生します。
この場合、commentとphotoを切り離すように書けばいいのでしょうか?また、他の解決策がございましたらご教授お願いします。
発生している問題・エラー
ルーティングエラーが発生
No route matches [GET] "/comments/26/new"
該当するソースコード
routes
resources :movies do
resources :comments
collection do
get 'search'
end
end
movies/showhtml.erb
<div class="comment-show-item">
<% @comments.each do |comment| %>
<div class="comment-index-item">
<div class="comment-right">
<%= comment.content %>
</div>
</div>
<% end %>
</div>
<div class="comment-show-item">
<%= form_with model:[@movie,@comment],local:true do |f| %>
<%= f.text_field :content %>
<%= f.submit %>
<% end %>
</div>
movies_controller
def show
@movie =Movie.find(params[:id])
@user = User.find(@movie.user_id)
@likes_count = Like.where(movie_id: @movie.id).count
@comments = Comment.includes(:user).where(reply_comment: @movie.id)
@comments = Comment.all
@comment = Comment.new
end
comments_controller
def new
@comment = Comment.new
@movie =Movie.find(params[:movie_id])
@user = User.find(@movie.user_id)
end
def create
@movie=Movie.find(params[:movie_id])
@comment = @current_user.comment.new(comment_params)
if @comment.save
flash[:notice]= "コメントしました"
redirect_to ("/movies/#{@movie.id}")
else
flash[:notice]="コメントに失敗しました"
redirect_to("/comments/#{@movie.id}/new")
end
end
def destroy
@movie=Movie.find_by(id:params[:id])
@comment = Comment.find(params[:id]).destroy
redirect_to "/movies/#{@movie.id}"
end
private
def comment_params
comment_params = params.require(:comment).permit(:content).merge(user_id: :current_user,reply_comment: params[:movie_id])
end
rake routes
Prefix Verb URI Pattern Controller#Action
POST /likes/:movie_id/create(.:format) likes#create
POST /likes/:movie_id/destroy(.:format) likes#destroy
root GET / home#top
movie_comments GET /movies/:movie_id/comments(.:format) comments#index
POST /movies/:movie_id/comments(.:format) comments#create
new_movie_comment GET /movies/:movie_id/comments/new(.:format) comments#new
edit_movie_comment GET /movies/:movie_id/comments/:id/edit(.:format) comments#edit
movie_comment GET /movies/:movie_id/comments/:id(.:format) comments#show
PATCH /movies/:movie_id/comments/:id(.:format) comments#update
PUT /movies/:movie_id/comments/:id(.:format) comments#update
DELETE /movies/:movie_id/comments/:id(.:format) comments#destroy
search_movies GET /movies/search(.:format) movies#search
movies GET /movies(.:format) movies#index
POST /movies(.:format) movies#create
new_movie GET /movies/new(.:format) movies#new
edit_movie GET /movies/:id/edit(.:format) movies#edit
movie GET /movies/:id(.:format) movies#show
PATCH /movies/:id(.:format) movies#update
PUT /movies/:id(.:format) movies#update
DELETE /movies/:id(.:format) movies#destroy
schema
create_table "comments", force: :cascade do |t|
t.text "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.integer "reply_comment"
end
create_table "likes", force: :cascade do |t|
t.integer "user_id"
t.integer "movie_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "movies", force: :cascade do |t|
t.text "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.string "title"
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image_name"
t.string "password_digest"
t.boolean "admin", default: false
end
0