【问题标题】:ActiveRecord::RecordNotFound in ArticlesController#showActiveRecord::RecordNotFound in ArticlesController#show
【发布时间】:2014-07-18 16:51:21
【问题描述】:

我想在我的 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 %>

我的articles.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

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

【问题讨论】:

  • 对不起,问题是为什么会出现这个错误?
  • 在您看来,请尝试传递“:id =>article.id”。
  • 我这样做了,错误被删除了,但不知何故,请求没有到达 showcomment.js.erb
  • 你的 showcomment.html.erb 怎么样?

标签: javascript jquery ruby-on-rails ruby ajax


【解决方案1】:

在你看来,尝试通过:

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

一旦错误被删除,请尝试检查您的 js 模板是否在正确的目录中。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多