使用Ruby on Rails从MySql中找不到ID的数据

时间:2016-12-02 11:44:21

标签: mysql ruby-on-rails ruby

def show
    @item = Item.find(params[:id])
     rescue ActiveRecord::RecordNotFound #customer with that id cannot be found
     redirect_to action: :index
  end

/////////////////////////////////////////////////////////////////////////

def edit
    @item = Item.find(params[:id])
    rescue ActiveRecord::RecordNotFound #customer with that id cannot be found
    redirect_to action: :index
  end

现在的问题是它找不到数据库中存在的数据。当我放置新数据然后它工作正常,但只有当我需要更新或显示用户要求的数据时。然后它说记录没有找到,但同时新的输入数据可以像我之前告诉你的那样放入数据库。

1 个答案:

答案 0 :(得分:0)

<h1>Show Item</h1>
<p>
  <strong>Mobile: </strong>
  <%= @item.mobile %>
</p>

<p>
  <strong>Company: </strong>
  <%= @item.company %>
</p>

这是show.html.erb文件代码。

控制台输出:

开始GET“/items.%23%3CItem::ActiveRecord_Relation:0xb515b124%3E”for 127.0.0.1 at 2016-12-04 23:43:09 +0500   ActiveRecord :: SchemaMigration Load(0.4ms)SELECT schema_migrations。* FROM schema_migrations 按ItemsController处理#index为   项目加载(0.4ms)SELECT items。* FROM items   在布局/应用程序中呈现items / index.html.erb(13.4ms) 在309ms完成200 OK(浏览次数:291.7ms | ActiveRecord:2.0ms)