お世話になっております。
下記の事象で知見がある方がいらっしゃいましたらご教示お願いします。
#起きている事象
@post
に:image
の値を渡すことができません。
23: def confirm 24: @post = Post.new(post_params) => 25: binding.pry 26: end [1] pry(#<PostsController>)> @post => #<Post:0x00007fa381f35580 id: nil, store_name: "cafe", address: "address", image: nil, time_start: "12:00", (以下省略)
:image
カラム以外のカラムには正常に値を渡すことができています。
また、post_params
には:image
の値が渡すことができています。
23: def confirm 24: @post = Post.new(post_params) => 25: binding.pry 26: end [1] pry(#<PostsController>)> post_params => <ActionController::Parameters {"store_name"=>"cafe", "address"=>"address", "image"=>#<ActionDispatch::Http::UploadedFile:0x00007fa387a62750 @tempfile=#<Tempfile:/var/folders/0b/9kq594rn2dsc6r_1fk7nbzgm0000gn/T/RackMultipart20200405-17014-4xegd1.png>, @original_filename="business_man_macho.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"post[image]\"; filename=\"business_man_macho.png\"\r\nContent-Type: image/png\r\n">, "time_start"=>"12:00", "time_end"=>"18:00", "wifi"=>"true", "plug"=>"true", "evaluation2"=>"1"} permitted: true>
##関連ファイル
views/post/new.html.erb
<% provide(:title, '投稿画面') %> <div class="row"> <div class="col-md-4"> </div> <div class="col-md-4"> <%= form_for @post ,html: { class: "form-group" } do |form| %> <%= form.label :"カフェ" %> <%= form.text_field :store_name, id: :store_name ,class: "form-control",placeholder: "カフェの名前をご入力ください"%> <%= form.label :"住所" %> <%= form.text_field :address, id: :address ,class: "form-control",placeholder: "住所をご入力ください"%> <%= form.label :"カフェの画像" %><%= form.file_field :image %> <%= form.label :"営業時間"%><br> <%= form.time_field :time_start ,clsss: "bootstrap-timepicker"%> <%= form.label :"〜" %> <%= form.time_field :time_end %> <br><%= form.label :"環境" %> <div class="environment center"> <div class="btn-group" data-toggle="buttons"> <label class="btn btn-default"> <%= form.check_box :wifi, {} , true, false %><%= image_tag 'wifi.png' , :size => '50x40'%> </label> </div> <div class="btn-group" data-toggle="buttons"> <label class="btn btn-default"> <%= form.check_box :plug , {} , true, false %><%= image_tag 'plug.png' , :size => '50x40'%> </label> </div> <div class="btn-group" data-toggle="buttons"> <label class="btn btn-default"> <%= form.check_box :tabacco , {} , true, false %><%= image_tag 'tabacco.png' , :size => '50x40'%> </label> </div> <div class="btn-group" data-toggle="buttons"> <label class="btn btn-default"> <%= form.check_box :card , {} , true, false %><%= image_tag 'card.png' , :size => '50x40'%> </label> </div> </div> <%= form.label :"評価" %> <div class="evaluation center"> <div class="btn-group" data-toggle="buttons"> <label class="btn btn-default"> <%# <%= form.check_box :evaluation, {} , true, false %><%#<%= image_tag 'star.png' , :size => '50x40'%> <%= form.check_box :evaluation1, {} , '1', nil %><%= image_tag 'star.png' , :size => '50x40'%> </label> </div> <div class="btn-group" data-toggle="buttons"> <label class="btn btn-default"> <%= form.check_box :evaluation2, {} , '1', nil %><%= image_tag 'star.png' , :size => '50x40'%> </label> </div> <div class="btn-group" data-toggle="buttons"> <label class="btn btn-default"> <%= form.check_box :evaluation3, {} , '1', nil %><%= image_tag 'star.png' , :size => '50x40'%> </label> </div> <div class="btn-group" data-toggle="buttons"> <label class="btn btn-default"> <%= form.check_box :evaluation4, {} , '1', nil %><%= image_tag 'star.png' , :size => '50x40'%> </label> </div> <div class="btn-group" data-toggle="buttons"> <label class="btn btn-default"> <%= form.check_box :evaluation5, {} , '1', nil %><%= image_tag 'star.png' , :size => '50x40'%> </label> </div><br> </div> <br> <%= form.submit "投稿" %> </div> <% end %> </div> <div class="col-md-4"> </div> </div>
views/post/confirm.html.erb
<% provide(:title, '投稿確認画面') %> <div class="row"> <div class="col-md-4"> </div> <div class="col-md-4"> <%= form_for @post ,url: 'post/create' do |f|%> <table class="table"> <tr> <th>カフェの名前</th> <td> <%= @post.store_name %> <%= f.hidden_field :store_name %> </td> </tr> <tr> <th>住所</th> <td> <%= @post.address %> <%= f.hidden_field :address %> </td> </tr> <th>カフェの画像</th> <td> <%= image_tag @post.image.url, :size =>'300x250'%> <%= f.hidden_field :image %> </td> </tr> <th>営業時間</th> <td> <%= @post.time_start %> <%= f.hidden_field :time_start %> 〜 <%= @post.time_end %> <%= f.hidden_field :time_end %> </td> </tr> <tr> <th>環境</th> <td> <% if @post.wifi.present? %> <%= image_tag 'wifi.png' , :size => '50x40'%> <%= f.hidden_field :wifi %> <%end%> <% if @post.plug.present? %> <%= image_tag 'plug.png' , :size => '50x40'%> <%= f.hidden_field :plug %> <%end%> <% if @post.tabacco.present? %> <%= image_tag 'tabacco.png' , :size => '50x40'%> <%= f.hidden_field :tabacco %> <%end%> <% if @post.card.present? %> <%= image_tag 'card.png' , :size => '50x40'%> <%= f.hidden_field :card %> <%end%> </td> </tr> <tr> <th>評価</th> <td> <% if @post.evaluation1.present? %> <%= image_tag 'star.png' , :size => '50x40'%> <%= f.hidden_field :evaluation1 %> <%end%> <% if @post.evaluation2.present? %> <%= image_tag 'star.png' , :size => '50x40'%> <%= f.hidden_field :evaluation2 %> <%end%> <% if @post.evaluation3.present? %> <%= image_tag 'star.png' , :size => '50x40'%> <%= f.hidden_field :evaluation3 %> <%end%> <% if @post.evaluation4.present? %> <%= image_tag 'star.png' , :size => '50x40'%> <%= f.hidden_field :evaluation4 %> <%end%> <% if @post.evaluation5.present? %> <%= image_tag 'star.png' , :size => '50x40'%> <%= f.hidden_field :evaluation5 %> <%end%> </td> </tr> </table> <%= f.submit '投稿する'%> <% end %> <%= form_for @post, url: new_post_path do |f|%> <%= f.hidden_field :store_name %> <%= f.hidden_field :address %> <%= f.hidden_field :image %> <%= f.hidden_field :time_start %> <%= f.hidden_field :time_end %> <%= f.hidden_field :wifi %> <%= f.hidden_field :plug %> <%= f.hidden_field :tabacco %> <%= f.hidden_field :card %> <%= f.hidden_field :evaluation1 %> <%= f.hidden_field :evaluation2 %> <%= f.hidden_field :evaluation3 %> <%= f.hidden_field :evaluation4 %> <%= f.hidden_field :evaluation5 %> <%= f.submit '入力画面に戻る'%> <% end %> </div> <div class="col-md-4"> </div> </div>
controllers/posts_controller.rb
class PostsController < ApplicationController before_action :post_params, only: %i[confirm create] before_action :correct_user, only: :destroy def new @post = Post.new end def create @post = Post.new(post_params) @post.user = current_user @post.save! flash[:success] = '投稿されました。' redirect_to action: 'index' end def show @post = Post.find(params[:id]) end def confirm @post = Post.new(post_params) binding.pry end def index @posts = Post.all @posts = Post.paginate(page: params[:page], per_page: 6) @like = Like.new end def destroy @post.destroy flash[:success] = '投稿が削除されました。' redirect_to request.referrer || root_url end private def post_params params.require(:post).permit(:store_name, :address, :image, :time_start, :time_end, :wifi, :plug, :tabacco, :card, :evaluation1, :evaluation2, :evaluation3, :evaluation4, :evaluation5) end def correct_user @post = current_user.posts.find_by(id: params[:id]) redirect_to root_url if @post.nil? end end
model/post.rb
class Post < ApplicationRecord belongs_to :user, optional: true has_many :likes has_many :liked_users, through: :likes, source: :user default_scope -> { order(created_at: :desc) } validates :user_id, presence: true validates :store_name, presence: true, length: { maximum: 20 } validates :address, presence: true, length: { maximum: 50 } validates :time_start, presence: true validates :time_end, presence: true mount_uploader :image, ImageUploader end
##開発環境
ruby 2.7.0
Rails 5.1.6
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。