0

我刚刚添加resources: :favorites,嵌套在resources: :deals我的 Rails 应用程序中。突然之间,访问deals/deal_slug/favorites/new,在它呈现之后,会触发一个 GET 请求deals/favicon.ico,当然会激活该show路由,它无法找到任何带有favicon.ico.

它似乎favorites/new只是导致此请求的路线,我已经尝试注释掉所有视图favoritescontroller#new以及new视图,没有任何变化。

link href="../../favicon.ico" rel="icon"当然,在我的修复中注释掉layout它,但我很想把它保留在我的布局中,并弄清楚为什么这个问题刚刚开始!

我的路线文件:

Rails.application.routes.draw do

  # what are these??
  get 'regions/index'
  get 'regions/show'

  root "deals#index"

  resources :deals, param: :slug do
    resources :favorites
  end
  resources :favorites

  get 'my-deals', to: 'deals#my_deals', as: :my_deals_path
  resources :regions, param: :slug, only: [:index, :show] do
    resources :deals, only: [:index, :show], param: :slug
    # resources :deals, only: [:index, :show, :edit, :destroy]
  end

  resource :preferences
  ActiveSupport::Inflector.inflections {|inflect| inflect.irregular 'preferences', 'preferences'} # fix route helper paths so that form_for works
  get '/preferences/delete_airport/:airport_id', to: 'preferences#destroy', as: 'delete_home_airport'

  resources :vacations

  get 'pry', to: 'application#pry'

  # ------- DEVISE STUFF --------

  devise_for :users, controllers: { 
    omniauth_callbacks: 'users/omniauth_callbacks', 
    preferences: 'users/preferences',
  }

  devise_scope :user do 
    get "/sign_out", to: 'devise/sessions#destroy', as: 'user_sign_out'
    get "/sign_in", to: 'users/sessions#new', as: 'user_sign_in'
    get "/sign_up", to: 'devise/registrations#new', as: 'user_sign_up'
    get "/user/preferences", to: 'users/preferences#index', as: 'user_preferences'
    get "/user/preferences/edit", to: 'users/preferences#edit', as: 'edit_user_preferences'
    patch "/user/preferences/:id/edit", to: 'users/preferences#update'
  end

  devise_for :admins, path: 'admin', controllers: { sessions: 'admins/sessions' }

  devise_scope :admin do
    get "/admin", to: 'admins/sessions#portal', as: 'admin_root'
    get "/admin/sign_out", to: 'devise/sessions#destroy', as: 'admin_sign_out'
  end

end

收藏夹控制器:

class FavoritesController < ApplicationController
  def new
    prefs = current_user.preferences
    @favorite = Favorite.new deal: Deal.find_by(slug: params[:deal_slug]), preference_id: prefs.id
  end

  def create
    @favorite = Favorite.create(favorite_params)
    redirect_to preferences_path
  end

  def favorite_params
    params.require(:favorite).permit :preference_id, :deal_id, :comment
  end
end
4

1 回答 1

0

对 favicon 图标的请求是由浏览器发出的,您的代码不会触发该请求。

问题是你的 favicon href,你让它相对于当前路径:“../../favicon.ico”而不是绝对的“/favicon.ico”。如果您有超过 2 个级别的其他路线,您会遇到同样的问题,因为路线是相对的。

您应该在公用文件夹的某处添加 favicon.icon 并使用绝对路径指向它。如果图标在其中,/public/favicon.ico则使用 配置布局的 favicon 标签href="/favicon.ico",如果将图标放在其他位置,/public/icons/favicon.ico则将链接标签更改为href="/icons/favicon.ico"

于 2019-02-11T17:10:41.613 回答