gpt4 book ai didi

ruby-on-rails - 如何在Ruby on Rails中使用相同的.js更新重新创建远程链接?

转载 作者:行者123 更新时间:2023-12-04 05:46:24 25 4
gpt4 key购买 nike

基本上,我希望使链接能够从“收藏夹”动态刷新为“已删除”,同时让用户可以选择重新单击新按钮以返回上一步。但是确实会发生该操作,因为刷新页面时会显示更新的按钮。为什么单击“收藏此教室”链接无效? “删除此教室”链接似乎有效。谢谢你的帮助

favorite_controller.rb:

  def create
current_classroom.add_to_favorites(@classroom)
current_classroom.save
respond_to do |format|
format.js { }
format.html {redirect_to @classroom}
end
end

def destroy
current_classroom.remove_from_favorites(@classroom)
current_classroom.save
respond_to do |format|
format.js { }
format.html {redirect_to @classroom}
end
end


收藏夹/create.js.erb

$("#favorite_classroom").html("<%= escape_javascript(link_to 'Remove the Classroom', classroom_favorite_path(@classroom), :remote => true, :method => :delete) %>");


教室/_classroom_details.html.erb

<div id="favorite_classroom">
<% if loggedin_user.favorite_classroom?(@classroom) %>
<%= link_to 'Remove this Classroom', classroom_favorite_path(@classroom), :remote => true, :method => :delete %>
<% else %>
<%= link_to 'Favorite this Classroom', classroom_favorites_path(@classroom), :remote => true, :method => :post %>
<% end %>


路线:

classroom_favorites POST   /classrooms/:classroom_id/favorites(.:format)  {:action=>"create", :controller=>"favorites"}
classroom_favorite DELETE /classrooms/:classroom_id/favorites/:id(.:format) {:action=>"destroy", :controller=>"favorites"}


当我点击“收藏此教室”链接时出现错误:
ActionView::Template::Error (No route matches {:action=>"destroy", :controller=>"favorites"

谢谢!

最佳答案

您在js文件中两次使用了双引号。你不能那样做。您需要这样重写它-

$("#favorite_classroom").html("<%= escape_javascript(link_to 'Remove the Classroom', classroom_favorite_path(@classroom), :remote => true, :method => :delete) %>");


现在注意,“删除教室”用单引号引起来,而不是双引号引起来。

此外,您的路线不正确,因为您在教室中嵌套了收藏夹。编写链接时,需要添加要删除的@favorite对象:

= link_to 'Remove Favorite', classroom_favorite_path([@classroom, @favorite]), :remote => true, :method => :delete


现在,您尝试访问的路由有效。那应该摆脱错误。

关于ruby-on-rails - 如何在Ruby on Rails中使用相同的.js更新重新创建远程链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11925615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com