rails只有生产中没有路由匹配

时间:2015-12-04 06:12:13

标签: ruby-on-rails postgresql database-migration

我有一个在dev env中工作正常的rails应用程序,但在推送到heroku后也会在生产中引发错误(db也会迁移)。

的routes.rb

devise_for :users
  resources :users do
    resources :notifications, only: [:create, :index] do
      collection do
        get :other_notifications, :chat_notifications
      end
    end
    resource :profile
    ....
    ....

heroku日志:

ActionView::Template::Error (No route matches {:action=>"show", :controller=>"users", :id=>nil} missing required keys: [:id]):
2015-12-04T05:54:08.351276+00:00 app[web.1]:     3: <% else %>
2015-12-04T05:54:08.351277+00:00 app[web.1]:     4:   <div class="notification-line-checked">
2015-12-04T05:54:08.351278+00:00 app[web.1]:     5: <% end %>
2015-12-04T05:54:08.351279+00:00 app[web.1]:     6:   <%= link_to user_path(notification.sender) do %>
2015-12-04T05:54:08.351280+00:00 app[web.1]:     7:     <% if notification.notification_type == "chat" %>
2015-12-04T05:54:08.351282+00:00 app[web.1]:     8:       <p class="subject"><%= notification.sender.profile.first_name %> <%= notification.sender.profile.last_name %> has sent you a message on chat.</p>
2015-12-04T05:54:08.351283+00:00 app[web.1]:     9:     <% else %>

渲染:

  <div class = "other-notifications-index">
    <%= render @other_notifications %>
  </div>

同样,它在dev中工作正常。可能是由未定义sender.id的旧架构版本引起的?所以rails试图获取那些旧记录,但却无法找到sender.id?在当前版本之前,我只使用了recipient.id,并且通过某些方法使用了发件人名称。

1 个答案:

答案 0 :(得分:2)

在这一行:

with()

在创建链接之前,您最好先检查<%= link_to user_path(notification.sender) do %> 是否为notification.sender。否则,您将获得nil并且无法匹配您的路线。