链接到PDF文件时,Rails呈现404页面

时间:2018-07-17 13:43:20

标签: html ruby-on-rails ruby

问题

我在我的网站上具有此搜索功能,但可以使用Meetings,但只有Meetings不能使用。当我尝试使用Meeting链接到asset_path包含的pdf文件时,我得到了404页面。

这是我的会议模型

    class Admin::Meeting < ApplicationRecord

        # Image/File Uploader
        mount_uploader :agenda, FileUploader
        mount_uploader :minutes, FileUploader

        has_many :meeting_translations, dependent: :destroy
        accepts_nested_attributes_for :meeting_translations

     end

我的文件上传器如下所示,我使用的是CarrierWave

class FileUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
  # require 'RMagick'

  # include CarrierWave::RMagick
  # include CarrierWave::MiniMagick

  # before :cache, :setup_available_sizes
  # before :cache

  # Choose what kind of storage to use for this uploader:
  storage :file
  permissions 0777  
  # storage :fog

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.name.demodulize.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  # Provide a default URL as a default if there hasn't been a file uploaded:
  # def default_url(*args)
  #   # For Rails 3.1+ asset pipeline compatibility:
  #   # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
  #
  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  # end

  # Process files as they are uploaded:
  # process scale: [200, 300]
  #
  # def scale(width, height)
  #   # do something
  # end

  # Create different versions of your uploaded files:
  # version :thumb do
  #   process resize_to_fit: [200, 200]
  # end

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
  def extension_whitelist
    %w(pdf doc docx xls xlsx)
  end

  def size_range
    1..10.megabytes
  end

end

这是我尝试链接到agendaagenda是文件)的视图

<% @meeting_results.each do |result| %>
       <% unless result.agenda.nil? %>
         <div class="col-sm-12">
           <div class="col-sm-1"><i class="far fa-file-pdf fa-2x" aria-hidden="true"></i></div>
             <div class="col-sm-10">
               <div class="results-link">
                 <a href="<%= asset_path result.agenda %>"><%= result.agenda.file.original_filename %></a>
               </div>
             </div>
           </div>
        <% end %>
      <% end %>

这是我得到的错误

404_error

当我链接到PDF时,通常不会出现此问题,我不知道是否是因为还有minutes的另一个文件Meeting吗?无论如何,感谢您的帮助。

修改

我还注意到,从asset_path返回的路径不包含Meeting的ID。当文件确实位于uploads/meeting/agenda/Bathurst_Regular_Public_Meeting_Agenda_May_15-2017.pdf

中时,这是给我uploads/meeting/agenda/ID/Bathurst_Regular_Public_Meeting_Agenda_May_15-2017.pdf的路径

1 个答案:

答案 0 :(得分:0)

我觉得自己是个白痴,我对查找与搜索字词匹配的会议的查询是错误的。

我所拥有的

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script> <script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"></script> <script> $(document).ready(function() { var markersInfo = $('.ia-card').map(function() { var info = { id: $(this).data('map-id'), address: $(this).data('map-address'), title: $(this).data('map-title'), price: $(this).data('map-price'), latitude: $(this).data('map-latitude'), longitude: $(this).data('map-longitude'), html: "<img src=" + $(this).data('map-image') + ">", link: $(this).data("map-link"), contentHtml: "<div class='image'>" + "<img src=" + $(this).data('map-image') + ">" + "</div>" + '<b>' + $(this).data('map-title') + '</b><br>' + "<div class='changeprice'><div style='display: none;' class='currency-selector'></div>" + $(this).data('map-price') + "</div>" + "<br><a href='" + $(this).data("map-link") + "'>More>></a>" }; return info; }).get(); var distinctMarkerInfo = []; markersInfo.forEach(function(item) { if (!distinctMarkerInfo.some(function(distinct) { return distinct.id == item.id; })) distinctMarkerInfo.push(item); }); initGoogleMap(distinctMarkerInfo); // GMAP ON SEARCH RESULTS PAGE function initGoogleMap(markersInfo) { var mapOptions = { // zoom: 2, // center: new google.maps.LatLng(53.334430, -7.736673) }, bounds = new google.maps.LatLngBounds(), mapElement = document.getElementById('stm_map_results'), map = new google.maps.Map(mapElement, mapOptions); markerList = []; // create an array to hold the markers var geocoder = new google.maps.Geocoder(); var iconBase = '../assets/images/'; $.each(markersInfo, function(key, val) { var marker = new google.maps.Marker({ //map: map, position: {lat: parseFloat(val.latitude), lng: parseFloat(val.longitude)}, title: val.title, icon: iconBase + 'single.png', info: new google.maps.InfoWindow({ content: val.contentHtml }) }); markerList.push(marker); // add the marker to the list google.maps.event.addListener(marker, 'click', function() { marker.info.open(map, marker); }); loc = new google.maps.LatLng(val.latitude, val.longitude); bounds.extend(loc); }); map.fitBounds(bounds); map.panToBounds(bounds); var markerCluster = new MarkerClusterer(map, markerList, { imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m' }); }; }); </script> <div id="stm_map_results" style="width:100%; height:600px;"></div>

我替换为

@meeting_results = Admin::Meeting.select(:agenda).where("meetings.agenda LIKE ? OR meetings.minutes LIKE ?", "%#{query}%", "%#{query}%").distinct

由于进行了更改,因此我输入了错误的查询,因此决定不进行更改。希望有一天能对某人有所帮助:(