I want to save the full url of my uploaded images in a rails app using the CarrierWave gem. I update the entry just after the create action but it does not work. However, when I puts my variable, the full url appears but it only saves the identifier...
The Card controller, after_action method:
class CardsController < ApplicationController
before_action :set_card, only: [:show, :edit, :update, :destroy]
after_action :save_full_image_url, only: [:create, :update]
include Shortener
require 'carrierwave/orm/activerecord'
def save_full_image_url
c = @card
puts "URL: "+c.image_url.url
@card.update(image_url: c.image_url.url)
Rails.logger.info(@card.errors.inspect)
end
My Card model:
class Card < ApplicationRecord
validates :cta_button_url, :format => URI::regexp(%w(http https)), presence: { message: '%{value} : please enter valid url' }
validates :title, :subtitle, :cta_button_text, :cta_button_url, :presence => true
mount_uploader :image_url, ImageUploader
has_one :short_url
end
My full logs:
SQL (0.3ms) INSERT INTO "cards" ("letter_id", "title", "subtitle", "image_url", "cta_button_text", "cta_button_url", "share_button") VALUES (?, ?, ?, ?, ?, ?, ?) [["letter_id", 49], ["title", "fsddssd"], ["subtitle", "fsdfsdfsd"], ["image_url", "10298593.jpg"], ["cta_button_text", "Visit site"], ["cta_button_url", "http://www.testurl.com"], ["share_button", "t"]] (1.1ms) commit transaction (0.1ms) begin transaction Card Load (0.2ms) SELECT "cards".* FROM "cards" WHERE "cards"."id" = ? LIMIT ? [["id", 44], ["LIMIT", 1]] SQL (0.4ms) INSERT INTO "short_urls" ("card_id", "token", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["card_id", 44], ["token", "PmK5vnuCU9Dczz995fz8Ddbf"], ["created_at", "2017-08-08 21:08:57.625325"], ["updated_at", "2017-08-08 21:08:57.625325"]]
(0.8ms) commit transaction Letter Load (0.2ms) SELECT "letters".* FROM "letters" WHERE "letters"."id" = ? LIMIT ? [["id", 49], ["LIMIT", 1]] (0.1ms) begin transaction CoreBot Load (0.2ms) SELECT "core_bots".* FROM "core_bots" WHERE "core_bots"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] (0.1ms) commit transaction Redirected to http://localhost:3000/dashboard/test URL: http://localhost:3000/uploads/card/image_url/44/10298593.jpg
(0.1ms) begin transaction SQL (0.4ms) UPDATE "cards" SET "image_url" = ? WHERE "cards"."id" = ? [["image_url", "10298593.jpg"], ["id", 44]] (2.5ms) commit transaction"10298593.jpg", cta_button_text: "Visit site", cta_button_url: "http://www.testurl.com", share_button: true>, @messages={}, @details={}> Completed 302 Found in 60ms (ActiveRecord: 7.2ms)
When I log in the console "c.image_url.url" I get the full URL (http://localhost:3000/uploads/card/image_url/44/10298593.jpg) but when I update my entry, it only saves the end (10298593.jpg).
Any idea why I am not able to save the entire url even if I can get it in a variable before?