通过带有carrierwave的控制器提供文件,并在Post模型

时间:2017-05-31 22:57:05

标签: ruby-on-rails-4 rspec carrierwave slim pundit

我正在尝试使用carrierwave上传照片文件并限制(为了保护其他不允许观看该文件的人,并在发布帖子之后执行此操作)与pundit一起使用该文件。

所以我为附件创建了另一个控制器:

class AttachmentsController < ApplicationController

  def show
    attachment = Attachment.find(params[:id])
    authorize attachment, :show?
    send_file attachment.file.path, disposition: :inline
  end

end

我的附件政策:

class AttachmentPolicy < ApplicationPolicy
  class Scope < Scope
    def resolve
      scope
    end
  end

  def show?
    user.try(:admin?) || record.post.has_member?(user)
  end
end

我的路线:

resources :attachments, only: [:show]

我在my_project / uploads上创建了一个文件夹,然后配置attachment_uploader.rb:

 def store_dir
    Rails.root.join "uploads/#{model.class.to_s.underscore}/" + \
      "#{mounted_as}/#{model.id}"
  end

创建文件config/initializers/carrierwave.rb并输入:

CarrierWave.configure do |config|
  config.root = Rails.root
end

所以现在当我创建一个带有文件附件的新帖子时,不再显示图像了。在它显示之前(在Slim中):

 - if @post.attachments.any?
    .attachments
        - @post.attachments.each do |attachment|
          = link_to image_tag(attachment.file.url, class: "img-responsive img-thumbnail"), attachment_path(attachment)

上传的新图片不再显示在我的帖子上了。只有当我点击该图像的链接并且只是因为人们被pundit包含在限制保护中时,它才是可能的, 在AttachmentsController的路径中。例如:

http://localhost:3000/attachments/3

我正在尝试做这个场景规范:

require "rails_helper"

RSpec.feature "Users can view a Post's attached files" do
  let(:user) { create(:user) }
  let(:post) { create(:post, author: user) }
  let!(:attachment) { create(:attachment, post: post, file_to_attach: "spec/fixtures/photo03.jpg") }

  before do
    assign_role!(user, :viewer, post)
    login_as(user)
  end

  scenario "successfully" do
    visit post_path(post)
    find("img[src*='photo03.jpg']").click

    expect(current_path).to eq attachment_path(attachment)
    expect(page).to have_css("img[src*='photo03.jpg']")
  end
end

并抱怨此错误:

$ rspec
...........................................F.............................

Failures:

  1) Users can view a Post's attached files successfully
     Failure/Error: expect(current_path).to eq attachment_path(attachment)

       expected: "/attachments/1"
            got: "/posts/1"

       (compared using ==)

所以有人可以帮我这个吗?我正在尝试做类似电子书Rails 4的例子,但在这种情况下显示图像。

我尝试使用rails console:

ap p = Post.last
  Post Load (0.2ms)  SELECT  "posts".* FROM "posts"  ORDER BY "posts"."id" DESC LIMIT 1
#<Post:0x007f9d6c036b08> {
            :id => 4,
         :title => "meus amores",
      :subtitle => "meus amores familia!!",
       :content => "meus amores familia!!",
    :created_at => Wed, 31 May 2017 23:18:56 UTC +00:00,
    :updated_at => Wed, 31 May 2017 23:18:56 UTC +00:00,
     :author_id => 1
}
=> nil

>> at = p.attachments.each do |attachment|
?> attachment.file.url
>> end
=> [#<Attachment id: 4, file: "15036286_1281339961886906_146920697557141175_n.jpg", post_id: 4, created_at: "2017-05-31 23:18:56", updated_at: "2017-05-31 23:18:56">]
>> ap at
[
    [0] #<Attachment:0x007f9d6eadadf0> {
                :id => 4,
              :file => #<AttachmentUploader:0x007f9d6eadaa58 @model=#<Attachment id: 4, file: "15036286_1281339961886906_146920697557141175_n.jpg", post_id: 4, created_at: "2017-05-31 23:18:56", updated_at: "2017-05-31 23:18:56">, @mounted_as=:file, @storage=#<CarrierWave::Storage::File:0x007f9d6eada9b8 @uploader=#<AttachmentUploader:0x007f9d6eadaa58 ...>>, @file=#<CarrierWave::SanitizedFile:0x007f9d6ead97e8 @file="/Users/romenigld/workspace/projects/news_city/uploads/attachment/file/4/15036286_1281339961886906_146920697557141175_n.jpg", @original_filename=nil, @content_type=nil>, @versions={}>,
           :post_id => 4,
        :created_at => Wed, 31 May 2017 23:18:56 UTC +00:00,
        :updated_at => Wed, 31 May 2017 23:18:56 UTC +00:00
    }
]
=> nil
>>

因此文件上传到uploads文件夹中。但不再显示了!

0 个答案:

没有答案