Jquery 试着用“两个”;“远程真实”;用于Rails中的单独Ajax调用
我试图允许用户使用Ajax调用在同一页面上发表评论和删除评论。目前,用户可以发布评论和删除评论,但当我在两个视图中都输入remote true时,如果不重新加载页面,这两个视图都不会呈现。如果我去掉一个远程true,那么这意味着一个会自动重新加载,而另一个不会。关于如何使两个远程True同时工作,有什么建议吗?这是我第一次这样做 这是我的第一个视图,用户可以在其中发表评论Jquery 试着用“两个”;“远程真实”;用于Rails中的单独Ajax调用,jquery,ruby-on-rails,ajax,Jquery,Ruby On Rails,Ajax,我试图允许用户使用Ajax调用在同一页面上发表评论和删除评论。目前,用户可以发布评论和删除评论,但当我在两个视图中都输入remote true时,如果不重新加载页面,这两个视图都不会呈现。如果我去掉一个远程true,那么这意味着一个会自动重新加载,而另一个不会。关于如何使两个远程True同时工作,有什么建议吗?这是我第一次这样做 这是我的第一个视图,用户可以在其中发表评论 <%= form_for [@part, Comment.new], remote: true do |f|%>
<%= form_for [@part, Comment.new], remote: true do |f|%>
<div>
<%= f.text_area :content %>
<%= f.submit "Post Comment"%>
</div>
<%end%>
<%=link_to 'delete', part_comment_path(@part, comment), data: {confirm: "Are you sure you want to delete this comment?"}, remote: true, method: :delete%>
以防这有帮助
这是我的日志
ActionView::Template::Error (No route matches {:action=>"destroy", :controller=>"comments", :id=>"52", :part_id=>nil} missing required keys: [:part_id]):
1: <li><%= comment.content%> by: <%= comment.user.first_name %> </li>
2: <% if logged_in? %>
3: <%=link_to 'delete', part_comment_path(@part, comment), remote: true, data: {confirm: "Are you sure you want to delete this comment?"}, method: :delete%>
4: <%end%>
app/views/comments/_comment.html.erb:3:in `_app_views_comments__comment_html_erb___3749430301288060826_70216407028000'
app/views/comments/create.js.erb:1:in `_app_views_comments_create_js_erb___1584098095545825378_70216383980080'
ActionView::Template::Error(没有路由匹配{:action=>“destroy”,:controller=>“comments”,:id=>“52”,:part_id=>nil}缺少所需的键:[:part_id]):
1:作者:
2:
三:
4:
app/views/comments/_comment.html.erb:3:in`` app\u views\u comments\u html\u erb\u 3749430301288060826\u 70216407028000'
app/views/comments/create.js.erb:1:in`_app_views_comments_create_js_erb_15840; 158409809554545825378_702163839880080'
这似乎是问题所在……我不明白这怎么会缺少零件id
我日志中错误的屏幕截图
_comment.html.erb视图
<li><%= comment.content%> by: <%= comment.user.first_name %> </li>
<% if logged_in? %>
<%=link_to 'delete', part_comment_path(@part, comment), remote: true, data: {confirm: "Are you sure you want to delete this comment?"}, method: :delete%>
<%end%>
作者:
试试这个:
def destroy
@part = Part.find(params[:part_id])
@comment = Comment.find(params[:id])
if @comment.destroy
respond_to do |format|
format.html
format.js
end
end
end
如果找不到零件id
,请尝试以下操作:
<%=link_to 'delete', part_comment_path(id: comment.id), remote: true, data: {confirm: "Are you sure you want to delete this comment?"}, method: :delete%>
def destroy
@comment = Comment.find(params[:id])
if @comment.destroy
respond_to do |format|
format.html
format.js
end
end
end
def销毁
@comment=comment.find(参数[:id])
如果@comment.destroy
回应待办事项|格式|
format.html
format.js
结束
结束
结束
我的评论中的create和destroy方法都有respond|u to do | format|控制器是控制器和路由旁边的日志。您如何构建视图?@part
可以在您的_comment.html.erb中找到吗?让我们来看看。
<li><%= comment.content%> by: <%= comment.user.first_name %> </li>
<% if logged_in? %>
<%=link_to 'delete', part_comment_path(@part, comment), remote: true, data: {confirm: "Are you sure you want to delete this comment?"}, method: :delete%>
<%end%>
def destroy
@part = Part.find(params[:part_id])
@comment = Comment.find(params[:id])
if @comment.destroy
respond_to do |format|
format.html
format.js
end
end
end
<%=link_to 'delete', part_comment_path(id: comment.id), remote: true, data: {confirm: "Are you sure you want to delete this comment?"}, method: :delete%>
def destroy
@comment = Comment.find(params[:id])
if @comment.destroy
respond_to do |format|
format.html
format.js
end
end
end