gpt4 book ai didi

javascript - ArticlesController#show 中的 ActiveRecord::RecordNotFound

转载 作者:行者123 更新时间:2023-11-28 08:05:30 25 4
gpt4 key购买 nike

我想在我的 Rails 应用程序中实现 ajax,但收到此错误。

我进行ajax调用的erb代码如下

<% @articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Show Comment', :action => 'showcomment' ,method: :get, :id =>article, :remote => true ,:class=>'show_comment' %></td>
<td><%= link_to 'Destroy', article_path(article), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>

我的文章. Controller 有代码

class ArticlesController < ApplicationController
def new
@article = Article.new
end

def create
@article = Article.new(article_params)

if @article.save
redirect_to @article
else
render 'new'
end
end

def show
@article = Article.find(params[:id])
end

def edit
@article = Article.find(params[:id])
end
def index
@articles = Article.all
end

def destroy
@article = Article.find(params[:id])
@article.destroy

redirect_to articles_path
end

def showcomment
@article = Article.find(params[:article_id])
@comment = @article.comments.find(params[:id])
respond_to do |format|
format.js
end
end

def update
@article = Article.find(params[:id])

if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end
private
def article_params
params.require(:article).permit(:title, :text)
end

end

我不知道请求以某种方式进入显示方法并且它给出了异常找不到带有“id”=showcomment的文章

最佳答案

在你看来,尝试传递:

<td><%= link_to 'Show Comment', :action => 'showcomment' ,method: :get, :id =>article.id, :remote => true ,:class=>'show_comment' %>

错误消除后,尝试检查您的 js 模板是否位于正确的目录中。

P.S:有一点值得注意:使用 show_comment 代替 showcomment。 Ruby 方式。

关于javascript - ArticlesController#show 中的 ActiveRecord::RecordNotFound,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24830146/

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