ユーザー投稿系のsnsを制作しています。
以前は画像を投稿できたものの。ユーザーと紐付けた後投稿できなくなりました。
createアクションのsave前でbinding.pryしたところ、imageに値が入っていませんでした。
参考にした記事では今のままのコードで表示されてるのでどこがおかしいかわかりません。
よろしくお願いします
Already linked: /usr/local/Cellar/imagemagick@6/6.9.9-42
と表示されるのでimagemagickは6系でrmagickは確認したところ2.16.0です。
なおパラメーターにはimageが渡ってきているものの、createアクション時のPost.newにはデータを渡せてないようです。
<%= form_for(@post, :html => {:multipart => true}) do |f| %>
を記述した後のパラメーターやコンソール
読んだ記事
https://qiita.com/ttaka66/items/264dcb85e41f9135685c
http://igarashikuniaki.net/rails_textbook/picture-upload.html
フォームのフロント部分
<%= form_with(model: post, local: true) do |form| %> <% if post.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2> <ul> <% post.errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <div class="form"> <table> <tbody> <%= form_for(@post, html: { multipart: true }) do |f| %> // ここ <div class="field"> <tr> <th><%= f.label :TOP画像 %></th> <td><%= f.file_field :image %></td> //画像投稿フォーム </tr> </div> <tr> <div class="field"> <th><%= f.label :サービス名 %></th> <td><%= f.text_field :name, id: :post_name %></td> </div> </tr> <tr> <div class="field"> <th><%= f.label :どんなサービス? %></th> <td><%= f.text_area :body, id: :post_body %></td> </div> </tr> <tr> <div class="field"> <th><%= f.label :どんな技術が使われている? %></th> <td><%= f.text_area :tec, size: "20×10", id: :post_tec %></td> </div> </tr> <tr> <div class="field"> <th><%= f.label :どこがオススメ? %></th> <td><%= f.text_area :point, id: :post_point %></td> </div> </tr> <tr> <div class="field"> <th><%= f.label :LINK %></th> <td><%= f.text_area :link, id: :post_link %></td> </div> </tr> <tr> <div class="actions"> <th></th> <td><%= form.submit %></td> </div> </tr> <% end %> </tbody> </table> <% end %>
<p id="notice"><%= notice %></p> <p class="name"> <strong></strong> <a><%= @post.name %></a> </p> <div class="image_body"> <p class="image"> <strong></strong> <%= image_tag @post.image_url.to_s, :size => "300x200" %> //画像表示 </p> <p class="body"> <strong></strong> <%= @post.body %> </p> <p> <strong>id:</strong> <%= @post.user_id %> </p> </div> <div class="tec_point_link"> <p> <strong>Tec:</strong> <%= @post.tec %> </p> <p> <strong>Point:</strong> <%= @post.point %> </p> <p> <strong>Link:</strong> <%= @post.link %> </p> </div> <%= link_to 'Edit', edit_post_path(@post) %> | <%= link_to 'Back', posts_path %>
class Post < ApplicationRecord mount_uploader :image, ImageUploader //関連付け belongs_to :user, optional: true end
class ImageUploader < CarrierWave::Uploader::Base # Include RMagick or MiniMagick support: include CarrierWave::RMagick # include CarrierWave::MiniMagick # Choose what kind of storage to use for this uploader: storage :file # Override the directory where uploaded files will be stored. # This is a sensible default for uploaders that are meant to be mounted: def store_dir "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" end # Provide a default URL as a default if there hasn't been a file uploaded: # def default_url # # For Rails 3.1+ asset pipeline compatibility: # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) # # "/images/fallback/" + [version_name, "default.png"].compact.join('_') # end # Process files as they are uploaded: # process scale: [200, 300] # # def scale(width, height) # # do something # end # Create different versions of your uploaded files: # version :thumb do # process resize_to_fit: [50, 50] # end # Add a white list of extensions which are allowed to be uploaded. # For images you might use something like this: # def extension_whitelist # %w(jpg jpeg gif png) # end # Override the filename of the uploaded files: # Avoid using model.id or version_name here, see uploader/store.rb for details. # def filename # "something.jpg" if original_filename # end end
ActiveRecord::Schema.define(version: 20180711174827) do create_table "posts", force: :cascade do |t| t.string "name" t.text "body" t.text "tec" t.text "point" t.text "link" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "image" //ここ t.integer "user_id" end create_table "users", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "provider" t.string "nickname" t.string "image_url" end end
class PostsController < ApplicationController before_action :set_post, only: [:show, :edit, :update, :destroy] before_action :current_user # GET /posts # GET /posts.json def index @posts = Post.all end # GET /posts/1 # GET /posts/1.json def show end # GET /posts/new def new @post = Post.new end # GET /posts/1/edit def edit end # POST /posts # POST /posts.json def create @post = Post.new(post_params) @post.user_id = @current_user.id binding.pry respond_to do |format| if @post.save format.html { redirect_to @post, notice: 'Post was successfully created.' } format.json { render :show, status: :created, location: @post } else format.html { render :new } format.json { render json: @post.errors, status: :unprocessable_entity } end end end # PATCH/PUT /posts/1 # PATCH/PUT /posts/1.json def update respond_to do |format| if @post.update(post_params) format.html { redirect_to @post, notice: 'Post was successfully updated.' } format.json { render :show, status: :ok, location: @post } else format.html { render :edit } format.json { render json: @post.errors, status: :unprocessable_entity } end end end # DELETE /posts/1 # DELETE /posts/1.json def destroy @post.destroy respond_to do |format| format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' } format.json { head :no_content } end end private # Use callbacks to share common setup or constraints between actions. def set_post @post = Post.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def post_params params.require(:post).permit(:name, :user_id, :image, :body, :tec, :point, :link) end def current_user @current_user = User.find_by(id: session[:user_id]) end end
# Image Uploader gem 'carrierwave' gem 'rmagick', require: 'RMagick'
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/08/01 07:19 編集
2018/08/01 07:52
2018/08/01 09:39
2018/08/01 12:05