我正在尝试在我的应用程序中添加书签功能。我已经在routes.rb文件中创建了路线,并且在我的显示页面上实现了一个链接,以使用户能够保存发型。当我单击它时,出现以下错误。 ActionController::UrlGenerationError in Hairstyles#show
和
No route matches {:action=>"update", :controller=>"saved_hairstyles", :id=>"21"}, missing required keys: [:hairstyle_id]
到目前为止,我正在关注this video。
为便于理解,这里是my repo。
saved_hairstyles_controller.rb
(仅针对更新方法定义):
class SavedHairstylesController < ApplicationController
def update
saved_hairstyle = SavedHairstyle.where(hairstyle:
Hairstyle.find(params[:hairstyle]), user: current_user)
if saved_hairstyle == []
SavedHairstyle.create(hairstyle: Hairstlye.find(params[:hairstyle]), user:
current_user)
@saved_hairstyle._exists = true
else
saved_hairstyle.destroy_all
@saved_hairstyle_exists = false
end
respond_to do |format|
format.html {}
format.js {}
end
end
end
我在views>hairstyles>show.html.erb
文件中的链接,这是给我错误的链接,
<%= link_to 'save hairstyle', hairstyle_saved_hairstyle_path(hairstyle: @hairstyle) %>
routes.rb
如下,
Rails.application.routes.draw do
root to: 'pages#home'
devise_for :users
resources :hairstyles do
member do
put "like", to: "hairstyles#upvote"
put "dislike", to: "hairstyles#downvote"
end
resources :comments, only: :create
resources :saved_hairstyles, only: [:update]
end
resources :saved_hairstyles, only: :destroy
resources :comments, only: :destroy
resources :hairdressers
end
问题的补充:我还检查了“耙路| grep saved_hairstyles`,我有以下路线:
hairstyle_saved_hairstyle PATCH /hairstyles/:hairstyle_id/saved_hairstyles/:id(.:format) saved_hairstyles#update
PUT /hairstyles/:hairstyle_id/saved_hairstyles/:id(.:format) saved_hairstyles#update
saved_hairstyle DELETE /saved_hairstyles/:id(.:format) saved_hairstyles#destroy```