Ruby on Rails で画像のアップロードのために
carrierwaveを使ってs3にアップロードしています。
しかし、すでにファイルがある場合にファイルの選択を行わず、空のまま編集▶︎更新をすると、
画像が消えてしまい表示できなくなります。
そこで image_cacheを持ち入ろうとしたのですが
if @blog.update(blog_params)の部分に
def update if @blog.update(blog_params) #⇦この一文に対して!!!!!!! redirect_to @blog, notice: 'ブログが更新されました' else render edit , notice: "更新にエラーがありました" end
undefined method `key' for nil:NilClass
と言うエラーが出てしまい更新ができず困っております。
以下にフォームとコントローラーです。
edit.html.erb
<%= form_for(@blog) do |f| %> <tbody> <form> <% if @blog.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@blog.errors.count, "error") %> prohibited this blog from being saved:</h2> <ul> <% @blog.errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <div class="blog-group"> <div class="blog-title-left blogs">タイトル</div> <%= f.text_field :title, placeholder: "ここにタイトルを記入", class:"blog-title blogs" %> </div> <div class="blog-group"> <%= f.text_area :content, class:"blog-text", placeholder: "本文を入力" %> </div> <div class="blog-group"> <div class="blog-title-left blogs">メイン画像を一枚</div> <output id="list"></output> <% if @blog.persisted? && @blog.image? %> <%= image_tag @blog.image.thumb.url %> <label> <%= f.check_box :remove_image %> 画像を削除 </label> <% end %> <%= f.file_field :image, id:"files", class:"blog-photo blogs" %> <%= f.hidden_field :image_cache %> </div> <div class="blog-btn"> <%= f.submit "投稿する", class:"btn btn-primary large-width-btn"%> </div> </form> </tbody> <% end %>
blogs.controller
class BlogsController < ApplicationController devise_group :blogger, contains: [:user, :admin] before_action :authenticate_admin!, only: [:new, :create, :edit, :update, :destroy] before_action :set_blog, only: [:show, :edit, :update, :destroy] before_action :set_admin, only: [:show, :create, :edit, :update, :destroy] # GET /blogs # GET /blogs.json def index #@blogs = Blog.includes(:blog_photos).all @q = Blog.includes(:blog_photos).ransack(params[:q]) @blogs_count = @q.result.count @blogs = @q.result.page(params[:page]).per(25).order(id: :desc) end # GET /blogs/1 # GET /blogs/1.json def show @blog_photos = @blog.blog_photos.all @blogcomments = @blog.blogcomments.includes(:admin, :user).all @blogcomment = @blog.blogcomments.build(admin_id: current_admin.id) if admin_signed_in? @blogcomment = @blog.blogcomments.build(user_id: current_user.id) if user_signed_in? impressionist(@blog, nil, :unique => [:session_hash]) end # GET /blogs/new def new @blog = Blog.new @blog_photo = @blog.blog_photos.build end # GET /blogs/1/edit def edit @blog.image.cache! unless @blog.image.blank? end # POST /blogs # POST /blogs.json def create @blog = current_admin.blogs.build(blog_params) if admin_signed_in? if @blog.save params[:blog_photos]['photo'].each do |p| @blog_photo = @blog.blog_photos.create!(:photo => p, :blog_id => @blog.id) end redirect_to @blog, notice: 'ブログが投稿されました' else render new , notice: "投稿にエラーがありました" end end # PATCH/PUT /blogs/1 # PATCH/PUT /blogs/1.json def update if @blog.update(blog_params) redirect_to @blog, notice: 'ブログが更新されました' else render edit , notice: "更新にエラーがありました" end end # DELETE /blogs/1 # DELETE /blogs/1.json def destroy @blog.destroy respond_to do |format| format.html { redirect_to @admin, notice: 'ブログを削除しました' } format.json { head :no_content } end end private # Use callbacks to share common setup or constraints between actions. def set_blog @blog = Blog.find(params[:id]) end def set_admin @admin = Admin.find_by(params[:admin_id]) end def correct_admin article = Article.find(params[:id]) if !current_admin?(article.admin) redirect_to root_path, alert: '許可されていないページです' end end # Never trust parameters from the scary internet, only allow the white list through. def blog_params params.require(:blog).permit(:title, :image, :image_cache, :admin_id, :content, blog_photos_attributes: [:id, :blog_id, :photo]) end end
uploader.rb
def filename "#{secure_token}.png" if original_filename.present? #super.gsub!(/[\[\]\"]/, "").gsub!(/,/, "\n") end protected def secure_token var = :"@#{mounted_as}_secure_token" model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.uuid) end
何卒よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。