我试图添加一个删除按钮,允许用户删除已创建的列表中的电影,但实际上并没有能够从网站上删除电影。如何在没有从网站上实际删除项目的情况下删除项目(例如从愿望清单中删除产品)
这是我在我的列表显示页面
<%= link_to 'Destroy', @list, method: :destroy, data: { confirm: 'Are you sure?' } %>
明显@list破坏了整个列表,我不希望出现这种情况。我希望他们可以选择删除整个列表,但我也希望他们可以选择删除列表中的项目。
在我的名单控制器我只有
def destroy
@list.destroy
respond_to do |format|
format.html { redirect_to lists_url, notice: 'List was successfully destroyed.' }
format.json { head :no_content }
end
end
,并在我的电影控制器。我除了
@movie.destroy
同样的事情,我想
<%= link_to 'Destroy', @list[:movie_id], method: :destroy, data: { confirm: 'Are you sure?' } %>
但只给了我一个路由错误,因为我正在调用实际的电影ID,我只想要列表项的ID。
在我routes.db我有这个
Rails.application.routes.draw do
devise_for :users
resources :lists
resources :users, only: [:show, :edit, :update]
resources :movies, except: [:index] do
member do
put "like", to: "movies#upvote"
put "dislike", to: "movies#downvote"
end
end
get "discover", to: "movies#index"
get "movies", to: "movies#films"
get "tv_shows", to: "movies#tv_shows"
resources :users, only: [:index, :show]
resources :comments, only: [:create, :destroy]
devise_scope :user do
authenticated :user do
root 'movies#films', as: :authenticated_root
end
end
http://guides.rubyonrails.org/ association_basics.html#has-many-association-reference –