いつもお世話になります。
画像と文字を投稿し表示できるようなページをRailsで作成しております。いろいろなサイトを参考にcarrierwaveのインストールを行いコードを以下のように書いている状態です。
投稿者名(poster_name)、投稿内容(post_text)、イメージ用のパス(attachment_name)の3つをActiveRecordに保存したいのですが、attachment_nameの保存の部分が分かりません。
コントローラー側の処理について解説しているサイトが少なくこちらに質問させて頂きました。
ご教示のほどお願いいたします。
環境はローカルです。
以下パラメータ
{"utf8"=>"✓", "authenticity_token"=>"llXh以下略==", "poster_name"=>"", "post_text"=>"", "attachment_name"=>#<ActionDispatch::Http::UploadedFile:0x4dbd910 @tempfile=#<Tempfile:
C:/Users/ユーザー名/AppData/Local/Temp/RackMultipart20171113-6468-19agl2u.png>, @original_filename="detailMain_chihuahua.png", @content_typ
e="image/png", @headers="Content-Disposition: form-data; name="attachment_name"; filename="detailMain_chihuahua.png"\r\nContent-T
ype: image/png\r\n">, "commit"=>"送信", "id"=>"xxxxx"}
Ruby
1コントローラー側 2def update 3 newrecord = モデル名.new 4 newrecord.poster_name = params[:poster_name] 5 newrecord.post_text = params[:post_text] 6 ### ↓の部分が知りたい 7 newrecord.attachment_name = params[:???] 8 9 newrecord.save 10 if newrecord.save 11 redirect_to controller: 'home', action: 'show' 12 else 13 render "post/post" 14 end 15 end 16 17view側 18#投稿画面 19<%= form_with(model: @userinfo, local: true, html: {id: 'inform_save', class: 'form', autocomplete: 'off'}) do |f| %> 20 <div class="field"> 21 <%= f.label :poster_name, "投稿者名" %> 22 <%= f.text_field :poster_name, size: 25, value: "", id: "poster_name", class: "poster_name" %> 23 </div> 24 <div class="field"> 25 <%= f.label :post_text, "投稿内容" %> 26 <%= f.text_field :post_text, size: 25, value: "", id: "post_text", class: "post_text" %> 27 </div> 28 <div class="field"> 29 <%= f.file_field :attachment_name, class: 'attachment_name' %> 30 </div> 31 <div class="actions"> 32 <%= f.submit '送信' %> 33 </div> 34<% end %> 35 36モデル側 37 38#モデル名.rb 39class モデル名 < ApplicationRecord 40 mount_uploader :attachment_name, ImageUploader 41end 42 43#image_uploader.rb 44class ImageUploader < CarrierWave::Uploader::Base 45 46 # Include RMagick or MiniMagick support: 47 # include CarrierWave::RMagick 48 # include CarrierWave::MiniMagick 49 50 # Choose what kind of storage to use for this uploader: 51 storage :file 52 # storage :fog 53 process convert: 'jpg' 54 # Override the directory where uploaded files will be stored. 55 # This is a sensible default for uploaders that are meant to be mounted: 56 def store_dir 57 "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 58 end 59 60 # Provide a default URL as a default if there hasn't been a file uploaded: 61 # def default_url(*args) 62 # # For Rails 3.1+ asset pipeline compatibility: 63 # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) 64 # 65 # "/images/fallback/" + [version_name, "default.png"].compact.join('_') 66 # end 67 68 # Process files as they are uploaded: 69 # process scale: [200, 300] 70 # 71 # def scale(width, height) 72 # # do something 73 # end 74 75 # Create different versions of your uploaded files: 76 # version :thumb do 77 # process resize_to_fit: [50, 50] 78 # end 79 80 # Add a white list of extensions which are allowed to be uploaded. 81 # For images you might use something like this: 82 def extension_whitelist 83 %w(jpg jpeg gif png) 84 end 85 86 # Override the filename of the uploaded files: 87 # Avoid using model.id or version_name here, see uploader/store.rb for details. 88 def filename 89 "something.jpg" if original_filename 90 end 91 92end 93 94
あなたの回答
tips
プレビュー