質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Q&A

解決済

1回答

676閲覧

Carrierwaveで取得した画像の保存が上手くいかない

k10a

総合スコア35

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

0グッド

0クリップ

投稿2019/08/18 08:16

ご閲覧いただきまして、ありがとうございます。

Ruby on Railsで、Googleから認証したプロフィール画像を保存しようとしているのですが、なぜか remote_image_url でおかしな挙動をしてしまっていて困っています。

エラー内容は下記です。

[3] pry(User)> user.remote_image_url = auth.info.image NoMethodError: undefined method `[]' for nil:NilClass from /Users/xxxxxx/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/carrierwave-2.0.0/lib/carrierwave/downloader/remote_file.rb:28:in `filename_from_header' [4] pry(User)> user.remote_image_url => "https://lh5.googleusercontent.com/xxxxxxxxxxx/photo.jpg"

その後、 user.remote_image_url を叩くと表示されているのですが、その前にエラーが発生してしまうために実際のアプリケーションでは保存ができません。ユーザー認証はGoogleで、Deviseを使っています。

user.rb

ruby

1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 devise :database_authenticatable, :registerable, :confirmable, 5 :rememberable, :trackable, :timeoutable, :lockable, :recoverable, 6 :omniauthable, :timeoutable 7 8 mount_uploader :image, UserIconUploader 9 10 def self.find_for_google_oauth(auth) 11 where(provider: auth.provider, uid: auth.uid).first_or_create do |user| 12 user.email = auth.info.email 13 user.password = Devise.friendly_token[0,20] 14 user.username = auth.info.name 15 user.remote_image_url = auth.info.image 16 user.confirmed_at = Time.now.utc 17 end 18 end

user_icon_uploader.rb

ruby

1class UserIconUploader < CarrierWave::Uploader::Base 2 include CarrierWave::MiniMagick 3 4 if Rails.env.development? || Rails.env.test? 5 storage :file 6 else 7 storage :fog 8 end 9 10 def store_dir 11 "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 12 end 13 14 def default_url(*args) 15 'image.png' 16 end 17 18 process resize_to_fill: [300, 300] 19 process convert: 'png' 20 21 def extension_whitelist 22 %w[jpg jpeg gif png ico] 23 end 24 25 def filename 26 super.chomp(File.extname(super)) + '.jpg' if original_filename 27 end 28end

carrierwave.rb

ruby

1CarrierWave.configure do |config| 2 config.fog_provider = 'fog/aws' 3 config.fog_credentials = { 4 provider: 'AWS', 5 region: 'us-east-1', 6 aws_access_key_id: Rails.application.credentials.aws[:access_key_id], 7 aws_secret_access_key: Rails.application.credentials.aws[:secret_access_key], 8 } 9 config.fog_directory = Rails.application.credentials.aws[:bucket_name] 10 config.fog_public = false 11 config.fog_attributes = { cache_control: "public, max-age=#{365.days.to_i}" } 12end 13 14CarrierWave::SanitizedFile.sanitize_regexp = /[^[:word:].\-+]/

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

Carrierwave 2.0.0 が4時間前にリリースされていて、それの影響かと思われました。
一旦、バージョンを落としたら通過できました。

投稿2019/08/18 08:31

k10a

総合スコア35

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問