Upload an image using carrierwave in Rails

Asked 1 years ago, Updated 1 years ago, 69 views

I'm developing an app with Ruby on Rails 4.

Carriewave has been introduced to implement the image upload function.

For image upload, the blog function has been added so that you can post blog posts and images (optional).

When posting, we have transitioned to new→confirm→cretae so that we can post through the confirmation screen.

Problems
I want to be able to register the image arbitrarily, but if I don't upload the image, I get an error.

#Error Messages
CarrierWave::InvalidParameter at/topics
invalid cache id

It works fine when uploading images.

The code is now written like this.

_form.html.erb

# omitted
  <%=f.label:Upload Photo%>
  <%=f.file_field:photo%>
  <%=f.hidden_field: photo_cache%>
# omission

confirm.html.erb

# omitted
  <%=image_tag(@topic.photo.url)[email protected] event?%>
  <%=hidden_field_tag: "cache [photo]", @topic.photo.cache_name%>
# omission

topics_controller

class TopicsController <ApplicationController

  def new
    if params [:back]
      @topic=Topic.new(topics_params)
    else
      @topic=Topic.new
    end
  end

  def create
    @topic=Topic.new(topics_params)
    @topic.photo.retrieve_from_cache!params[:cache][:photo]
    @topic.save!
    @topic.user_id=current_user.id

    response_to do | format |
     [email protected]
      format.html {redirect_to @topic,notice:' Posted!' }
      format.json {render:show, status::created, location:@topic}
     else
      format.html {render:new}
      format.json {render json:@topic.errors, status::unprocessable_entity}
     end
    end
  end


  def confirm
    @topic=Topic.new(topics_params)
    render —new [email protected] valid?
  end

# partial omission


  private
    deftopics_params
      param.require(:topic).permit(:title,:content,:photo_cache,:photo,:tag_list)
    end

    default_topic
      @topic=Topic.find (params[:id])
    end
end

Please let me know how to correct it.

·Additional
This is what the error log looks like.

Started POST "/topics" for::1 at 2017-04-09 14:16:58 +0900
Processing by TopicsController # create as HTML
Parameters: {"utf8"=>" "", "authenticity_token"=>"3YIrH2Y/s/011pnlah8L6LFvGGdDzUmIFhxw28WvHDHG04r8DLH4taS+WihUPAFapQrDZP1YzWcGWQgXv95Q;"==" top=">"title="
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users". "id" = $1 ORDER BY "users". "id" ASCLIMIT1 [[["id", 3]]
(0.2ms) SELECT COUNT(*) FROM "notifications" WHERE "notifications". "user_id" = $1 AND "notifications". "read" = $2[[["user_id", 3], ["read", "f"]]
Completed 500 Internal Server Error in 7 ms (ActiveRecord: 0.5 ms)

CarrierWave::InvalidParameter-invalid cache id:
 carrierwave(1.0.0)lib/carrierwave/uploader/cache.rb:193:in`cache_id='
 carrierwave(1.0.0)lib/carrierwave/uploader/cache.rb: 158: in `block in retrieve_from_cache!'
 carrierwave(1.0.0)lib/carrierwave/uploader/callbacks.rb:15:in`with_callbacks'
 carrierwave(1.0.0)lib/carrierwave/uploader/cache.rb: 157:in `retrieve_from_cache!'
 app/controllers/topics_controller.rb:31:in `create'
 actionpack(4.2.3)lib/action_controller/metal/implicit_render.rb:4:in`send_action'
 ・・・
  # omission

ruby-on-rails ruby rubygems

2022-09-30 19:40

1 Answers

Is it not possible to add processing only when there is an image like the one below?

@topic.photo.retrieve_from_cache!params[:cache][:photo]if params[:cache][:photo].present?


2022-09-30 19:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.