【问题标题】:ActiveRecord::RecordNotFound (Couldnt find Story with 'id'="namegoeshere")ActiveRecord::RecordNotFound(找不到带有 'id'="name going here" 的故事)
【发布时间】:2016-10-04 20:25:26
【问题描述】:

所以我已经搜索了一段时间的表格,到目前为止,我尝试过的很多修复都让我陷入了困境。话虽这么说,我正在尝试查看我刚刚创建的故事,但是当我去看它时,我得到了错误 “ActiveRecord::RecordNotFound(找不到带有 'id'="namegoeshere" 的故事)”。

然后它指向我的 stories_controller 用于“显示”

  def show
    @story = Story.find(params[:id])
    @responses = @story.responses.includes(:user)
    @related_stories = @story.related_stories
    if request.path != story_path(@story)
      redirect_to @story, status: 301
    end
  end

我不清楚这里会发生什么,因为我认为

   @story = Story.find(params[:id])

会列出所有的故事。这是我的路线

  resources :users, only: [:show, :edit, :update] do
    resources :recommended_stories, only: [:index]
  end

  resources :stories, except: [:index] do
    resources :responses, only: [:create]
  end

此 show.html.erb 文件中的部分

 <div class="sidebar-top-stories">
   <ul>
     <% @dashboard.top_articles.each_with_index do |story, index| %>
       <li class="top-articles-list-item">
         <div class="count-button-wrapper">
           <span class="count-button"><%= index + 1 %></span>
         </div>
         <div class="top-articles-links">
           <%= link_to story.title, story, class: 'story-title' %><br/>
           <small>
             <%= react_component("PopoverLink", { user_id: story.user.id, url: user_path(story.user), children: story.username }) %>
           </small>
         </div>
       </li>
     <% end %>
   </ul>
 </div>

在模型故事中

  extend FriendlyId
  friendly_id :title, use: [ :slugged, :history, :finders ]

这是 cmd Story.find_each(&:save) 的日志 logs

感谢您帮助解决这个难题。

如果需要更多信息,请告诉我。

【问题讨论】:

  • 您使用什么 URL 来访问故事页面?
  • where I go to see view 处的链接可能不正确。您可以将视图的相关部分发布在该链接所在的位置吗?
  • 添加了我点击@SteveTurczyn的视图
  • @AlexandreAngelim 添加了查看文件
  • @ChrisLewis 您是否在 Story 模型上覆盖了 #to_param?

标签: ruby-on-rails activerecord heroku elasticsearch rails-activerecord


【解决方案1】:

所以错误是双重的,第一部分友好的 id gem 是导致我收到 ActiveError 消息的原因。第二部分是我在我的应用程序中使用弹性搜索,我的用户/故事不是索引。因此,每次我去发布一个故事时,我的应用程序都找不到弹性搜索试图找到友好 ID 的用户/故事 ID(又名友好 ID)。所以我对方法添加了友好,

def show
@story = Story.friendly.find(params[:id])
end

然后我为每个想要搜索的表手动运行 elasticsearch 索引。

bundle run rake elasticsearch:import:model Class='User' Force=y

一旦这两件事都完成了,一切都运行正常了。

值得注意的是,我在 Windows 平台上构建了该应用程序,因此如果我要在苹果或 Linux 上运行它,我可能不得不做更多必要的事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    相关资源
    最近更新 更多