ActiveRecord的:: RecordNotFound在ArticlesController#秀 [英] ActiveRecord::RecordNotFound in ArticlesController#show

查看:163
本文介绍了ActiveRecord的:: RecordNotFound在ArticlesController#秀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在我的Rails应用程序来实现Ajax,但我收到此错误。

I want to implement ajax in my rails application but I am getting this error.

我的ERB code其中AJAX调用是低于

my erb code where ajax call is made is below

<% @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有code

my articles.controller has code

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

我不知道莫名其妙的要求也越来越在演艺方法,它是给异常 找不到文章有'身份证'= showcomment

I dont know somehow the request is getting in show method and it is giving the exception Couldn't find Article with 'id'=showcomment

推荐答案

在你看来,尝试传:

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

在错误被删除,请尝试检查,如果你的JS模板是在正确的目录。

Once the error is removed, try checking if your js template is in the right directory.

PS:一件小事值得注意:代替showcomment使用show_comment。 Ruby的方式。

P.S: A little thing worth noticing: instead of showcomment use show_comment. Ruby way.

这篇关于ActiveRecord的:: RecordNotFound在ArticlesController#秀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆