問い合わせ機能を作成しています。
問い合わせした内容を一覧で表示させたいのですが、xlsxファイルが表示されません。
スクショ1枚目が登録画面、2枚目が一覧表示画面です。
2枚目の画面でファイル名で表示させたいのですがご教示いただけると幸いです。
ググったらFile.basename
を使えばできるとのことで色々試したのですが結局表示されずじまいでして。。。
require csv
を入れるとtmpフォルダに自動で保存されると書いてたのですが保存されていないので File.basename
以前の問題のような気がします。
こちらを参考にしました。(https://ruby-rails.hatenadiary.com/entry/20141120/1416483136)
Ruby
1<!-- app/views/mails/new.html.erb --> 2 3<%= form_tag("/mails/create",{multipart: true}) do %> 4 5<p>送信先</p> 6<input name="dest"> 7 8<p>タイトル</p> 9<input name="subject"> 10 11<p>問い合わせ内容</p> 12<input name="body"> 13 14<p>添付ファイル</p> 15<input name="attachments" type="file"> 16 17<p><input type="submit" value="送信"></p> 18 19<% end %> 20
Ruby
1# app/controllers/mails_controller.rb 2 3class MailsController < ApplicationController 4 5 def index 6 @mails = Mail.all 7 end 8 9 def new 10 end 11 12 def create 13 @mails = Mail.all 14 # gemをインストールしてxlsxデータを出力できるようにする https://qiita.com/necojackarc/items/0dbd672b2888c30c5a38 15 @mail = Mail.new(dest: params[:dest], subject: params[:subject], body: params[:body]) 16 @mail.save 17 18 if params[:attachments] 19 @mail.attachments = "#{@mail.id}.jpg" 20 image = params[:attachments] 21 File.binwrite("tmp/tests/#{@mail.id}.jpg", image.read) 22 end 23 end 24end 25
Ruby
1# config/application.rb 2# ここに require を追加 3require_relative 'boot' 4 5require File.expand_path('../boot', __FILE__) 6 7require 'rails/all' 8require 'csv' 9 10# Require the gems listed in Gemfile, including any gems 11# you've limited to :test, :development, or :production. 12Bundler.require(*Rails.groups) 13 14module ApiForm 15 class Application < Rails::Application 16 # Initialize configuration defaults for originally generated Rails version. 17 config.load_defaults 5.2 18 19 # Settings in config/environments/* take precedence over those specified here. 20 # Application configuration can go into files in config/initializers 21 # -- all .rb files in that directory are automatically loaded after loading 22 # the framework and any gems in your application. 23 end 24end 25
Ruby
1<!-- app/views/mails/index.html.erb --> 2 3<h1>送信内容一覧</h1> 4 <table border="1"> 5 <thead> 6 <tr> 7 <th>id</th> 8 <th>送信先</th> 9 <th>タイトル</th> 10 <th>内容</th> 11 <th>添付ファイル</th> 12 <th></th> 13 </tr> 14 </thead> 15<tbody> 16 <% @mails.each.with_index(1) do |mail, i| %> <!-- theadの上にあったのをここに移した--> 17 <tr> 18 <td><%= i %></td> 19 <td><%= mail.dest %></td> 20 <td><%= mail.subject %></td> 21 <td><%= mail.body %></td> 22 <td><%= mail.attachments %></td> 23 </tr> 24 <% end %> <!-- </table>の下にあったのをここに移した --> 25 </table>
回答2件
あなたの回答
tips
プレビュー