添加和删除收藏夹不起作用(Adding and remove from favorites isn't working)

编程入门 行业动态 更新时间:2024-10-24 01:52:21
添加和删除收藏夹不起作用(Adding and remove from favorites isn't working)

我的应用程序上有评论家和电影,我正在添加crtics功能,以便将电影添加到他们的收藏夹中。 现在,添加或删除都不起作用。 在尝试添加时,它会尝试发布补丁请求而不是帖子。 当试图删除时,它无法使用delete方法,因为我认为它没有找到要删除的对象。 不知道为什么会这样。 我将在下面添加所有代码和一些服务器日志。 真的需要一些帮助来解决这个问题。

浏览次数:

<%= form_for(current_critic.favorites.create, remote: true) do |f| %> <%= hidden_field_tag :movie_id, @movie.id %> <%= f.submit "Favorite" %> <% end %>

其他

<%= form_for(current_critic.favorites.find_by(movie_id: @movie.id), html: { method: :delete }, remote: true) do |f| %> <%= f.submit "Un-Favorite" %> <% end %>

调节器

class FavoritesController < ApplicationController def create movie = Movie.find(params[:movie_id]) current_critic.favorite(movie) redirect_to current_critic end def destroy movie = Movie.find(params[:id]) current_critic.un_favorite(movie) redirect_to current_critic end end

来自评论家模型的适用代码

has_many :favorites, dependent: :destroy has_many :movies, through: :favorites def favorite(movie) favorites.create(movie_id: movie.id) end def un_favorite(movie) favorites.find_by(movie_id: movie.id).destroy end

适用于电影模型的代码

has_many :favorites, dependent: :destroy has_many :critics, through: :favorites

最喜欢的模特

class Favorite < ActiveRecord::Base belongs_to :critic belongs_to :movie end

适用路线

resources :favorites, only: [:create, :destroy]

尝试添加到收藏夹时的服务器日志

Started PATCH "/favorites/28" for 99.39.164.184 at 2015-12-01 02:42:17 +0000 ActionController::RoutingError (No route matches [PATCH] "/favorites/28"):

尝试取消收藏时的服务器日志

Started DELETE "/favorites/27" for 99.39.164.184 at 2015-12-01 03:18:24 +0000 Processing by FavoritesController#destroy as JS Parameters: {"utf8"=>"✓", "commit"=>"Un-Favorite", "id"=>"27"} Movie Load (0.1ms) SELECT "movies".* FROM "movies" WHERE "movies"."id" = ? LIMIT 1 [["id", 27]] Completed 404 Not Found in 2ms (ActiveRecord: 0.1ms) ActiveRecord::RecordNotFound (Couldn't find Movie with 'id'=27): app/controllers/favorites_controller.rb:10:in `destroy'

提前致谢。 如果任何其他代码有帮助,请告诉我。

更新:我刚刚在控制台中测试了最喜欢和不喜欢的方法,它们都有效; 然而,一些奇怪的事情正在发生,其中id似乎没有增加一个而是一个看似随机的数字。

I have Critics and Movies on my app and I am adding the function for crtics to add movies to their favorites. Neither add nor remove is working right now. When trying to add, it's trying to issue a patch request rather than a post. When trying to remove, it's not able to use the delete method, because I don't think it's finding the object to delete. Not sure why this is happening. I'll add all code and some server logs below. Really need some help going over this.

Views:

<%= form_for(current_critic.favorites.create, remote: true) do |f| %> <%= hidden_field_tag :movie_id, @movie.id %> <%= f.submit "Favorite" %> <% end %>

other

<%= form_for(current_critic.favorites.find_by(movie_id: @movie.id), html: { method: :delete }, remote: true) do |f| %> <%= f.submit "Un-Favorite" %> <% end %>

controller

class FavoritesController < ApplicationController def create movie = Movie.find(params[:movie_id]) current_critic.favorite(movie) redirect_to current_critic end def destroy movie = Movie.find(params[:id]) current_critic.un_favorite(movie) redirect_to current_critic end end

applicable code from the critic model

has_many :favorites, dependent: :destroy has_many :movies, through: :favorites def favorite(movie) favorites.create(movie_id: movie.id) end def un_favorite(movie) favorites.find_by(movie_id: movie.id).destroy end

applicable code from the movie model

has_many :favorites, dependent: :destroy has_many :critics, through: :favorites

favorite model

class Favorite < ActiveRecord::Base belongs_to :critic belongs_to :movie end

Applicable routes

resources :favorites, only: [:create, :destroy]

Server logs when trying to add to favorites

Started PATCH "/favorites/28" for 99.39.164.184 at 2015-12-01 02:42:17 +0000 ActionController::RoutingError (No route matches [PATCH] "/favorites/28"):

Server logs when trying to un-favorite

Started DELETE "/favorites/27" for 99.39.164.184 at 2015-12-01 03:18:24 +0000 Processing by FavoritesController#destroy as JS Parameters: {"utf8"=>"✓", "commit"=>"Un-Favorite", "id"=>"27"} Movie Load (0.1ms) SELECT "movies".* FROM "movies" WHERE "movies"."id" = ? LIMIT 1 [["id", 27]] Completed 404 Not Found in 2ms (ActiveRecord: 0.1ms) ActiveRecord::RecordNotFound (Couldn't find Movie with 'id'=27): app/controllers/favorites_controller.rb:10:in `destroy'

Thanks in advance. If any other code will be helpful let me know.

UPDATE: I just tested both the favorite and un_favorite methods in the console, and they both work; however something odd is happening where the id's don't seem to be incrementing by one but instead by a seemingly random number.

最满意答案

出现第一个RoutingError是因为您在视图中使用current_critic.favorites.create ,它返回一个已经有id的记录,因此form_for会将其解析为update而不是create 。

=>解决方案:用current_critic.favorites.new替换

发生第二个RecordNotFound是因为您传递了Favorite的ID但用于查找Movie

=>解决方案:

def destroy favorite = current_critic.favorites.find(params[:id]) favorite.destroy redirect_to current_critic end The first RoutingError occurs because you use current_critic.favorites.create in your view, which returns a record already had an id, so the form_for will resolve it to update rather than create.

=> Solution: replace with current_critic.favorites.new

The second RecordNotFound occurs because you pass an id of Favorite but used to find a Movie

=> Solution:

def destroy favorite = current_critic.favorites.find(params[:id]) favorite.destroy redirect_to current_critic end

更多推荐

本文发布于:2023-08-06 23:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1456592.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:收藏夹   不起作用   Adding   working   favorites

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!