前提・実現したいこと
Rails初心者です。
現在、いわゆるTwitterのようなWEBサービスを作成しています。
ひとつの投稿に、複数画像やファイルをアップロードさせる機能をcarrierwaveで実装しました。
発生している問題・エラーメッセージ
アップロードファイルをpublicディレクトリ配下に置いても、app/assets/配下に置いても、URL直打ちで未ログインでも閲覧できてしまいます。
これを、ログインユーザのみが閲覧できるようにしたいと思っていますが、どう設定すれば良いのかわかりません。
before_action :authenticate_user!
などを定義できればいいのですが、ページではなくファイルなので、それが可能なのかもわからず…
どなたかお力を貸していただけますと幸いです。
該当のソースコード
routes.rb
1Rails.application.routes.draw do 2 get 'users/index' 3 get 'users/show' 4 get 'posts/index' 5 6 devise_for :users, module: :users 7 resources :users, :only => [:index, :show] 8 9 get "/" => "posts#index" 10 get "posts/like_ranking" => "posts#like_rank" 11 get "posts/post_count_ranking" => "posts#post_count" 12 get "posts/tags_search" => "posts#tags_search" 13 get "posts/new" => "posts#new" 14 get "posts/:id/reply" => "posts#new" 15 post "posts/create" => "posts#create" 16 get "posts/:id" => "posts#show" 17 get "posts/:id/edit" => "posts#edit" 18 post "posts/:id/update" => "posts#update" 19 post "posts/:id/destroy" => "posts#destroy" 20 21 get 'tags/:tag', to: 'posts#index', as: :tag 22 23 get "users/:id/likes" => "users#likes" 24 get "users/:id/reply" => "users#reply" 25 26 resources :posts, only: %w(index) 27 28 resources :posts, shallow: true do 29 resources :likes, only: [:create, :destroy] 30 end 31end
models/uploaders/image_uploder.rb
1class ImageUploader < CarrierWave::Uploader::Base 2 # Choose what kind of storage to use for this uploader: 3 storage :file 4 5 # Override the directory where uploaded files will be stored. 6 # This is a sensible default for uploaders that are meant to be mounted: 7 def store_dir 8 "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 9 end 10end
保存の際は、uploads/post/images/1234(post_id)/sample.pngのようになります。
views/posts/_post.html.erb
1 <div data-link="/posts/<%= post.id %>" class="posts-index-item"> 2 <div class="post-left"> 3 <img src="<%= post.user.avatar %>"> 4 </div> 5 <div class="post-right"> 6 <div class="post-user-name"> 7 <%= link_to(post.user.user_name, "/users/#{post.user.id}") %> 8 </div> 9 <div class="post-time"> 10 <%= post.created_at %> 11 </div> 12 <div class="post-content"> 13 <% if post.html_content.present? %> 14 <%= post.html_content.html_safe %> 15 <% else %> 16 <%= post.content.html_safe %> 17 <% end %> 18 19 <% if post.images? %> 20 <% post.images.each do |img| %> 21 <% if img.content_type.index("image") %> 22 <div class="post-img"> 23 <%= link_to img.url, target: "_blank" do %> 24 <%= image_tag img.url%> 25 <% end %> 26 </div> 27 <% else %> 28 <div class="post-file"> 29 <%= link_to img.file.identifier, img.url(query: { 'response-content-disposition' => 'attachment' }), target: "_blank" %> 30 </div> 31 <% end %> 32 <% end %> 33 <% end %> 34 </div> 35 <% if post.tag_list.present? %> 36 <ul class="post-tag"> 37 <% post.tags.each do |tag| %> 38 <li> 39 <%= link_to tag, tag_path(tag.name) %> 40 </li> 41 <% end %> 42 </ul> 43 <% end %> 44 <div class="post-reply"> 45 <%= link_to "/posts/#{post.id}/reply" do %> 46 <span class="fa fa-reply"></span> 47 <% end %> 48 </div> 49 <div id="like-buttons-<%= post.id %>"> 50 <%= render partial: 'like', locals: { post: post, like: @like} %> 51 </div> 52 <% if post.user_id == current_user.id %> 53 <div class="post-menus"> 54 <%= link_to("編集", "/posts/#{post.id}/edit") %> 55 <%= link_to("削除", "/posts/#{post.id}/destroy", {method: "post"}) %> 56 </div> 57 <% end %> 58 </div> 59 </div>
試したこと
ルートディレクトリ配下に置くから閲覧できてしまうのかも…と思い、ルートディレクトリと同階層にuploadsディレクトリを作り、そこに保存するようにもしてみましたが、urlがおかしいことになってしまい表示できませんでした…。
補足情報
サーバ環境はnginx、puma、mysqlです。
Railsのバージョンは5.1.5を使用しています。

回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。