I Have found a similar question:
NameError: uninitialized constant Article::ImageUploader when using Carrierwave on rails 4.1.5
But I have tried the suggestions there:
carrierwave is installed and bundle is complete
And I have double checked my models.
My user model has:
has_one :image, :as => :assetable, :class_name => "User::Image", :dependent => :destroy
And my user creation works find but as soon as I try to access the views it breaks by this code:
<%= link_to image_tag((current_user.image.nil? ? Settings.default_user_image : current_user.image.attachment.url(:thumbnail)) , :style=>'max-width:60px;', :alt=>current_user.full_name, :title=>current_user.full_name), user_path(current_user) %>
I am not sure why this broke, earlier it was working fine, only thing I can think of is, I did bundle once again when my gemfile.lock got conflicts.
This is how I replicate the issue on rails console:
2.1.1 :001 > u = User.new
2.1.1 :002 > u.build_image
NameError: uninitialized constant User::User::Image
from /home/aditya/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/inheritance.rb:133:in `compute_type'
from /home/aditya/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/reflection.rb:190:in `klass'
from /home/aditya/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/reflection.rb:207:in `build_association'
from /home/aditya/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/associations/association.rb:247:in `build_record'
from /home/aditya/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/associations/singular_association.rb:29:in `build'
from /home/aditya/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/associations/builder/singular_association.rb:18:in `build_image'
from (irb):2
from /home/aditya/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands/console.rb:90:in `start'
from /home/aditya/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands/console.rb:9:in `start'
from /home/aditya/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:69:in `console'
from /home/aditya/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /home/aditya/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
Please help. I am stuck at a critical point right now.
Path to the file where error is happening is:
/demo/app/views/home/index.html.erb
Image class resides in:
/demo/app/models/user/image.rb
Contents for image.rb
class User::Image < Asset
has_attached_file :attachment, {
fog_credentials: {:aws_access_key_id=>"***", :aws_secret_access_key=>"***", :provider=>"AWS"}, :fog_public=>true, :fog_directory=>"test",
styles: { :thumbnail => "60x60#", :profile => "165x165#" },
storage: :multiple,
path: ":compatible_rails_root/users/files/:id/:style.:extension",
url: "/uploads/posts/files/:id/:style.:extension",
multiple_if: lambda { |user| true },
display_from_s3: lambda { |user| true }
}
validates_attachment_content_type :attachment, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
end
User mode is simply in
/demo/app/models
Probably, your namespaces are wrong, try to write your classes as follows (note the introduced Users module):
# app/models/users/image.rb
module Users
class Image < Asset
has_attached_file :attachment, { # ...
# ....
end
end
and just modify a line referring to the Image class in User class (the only change is that it's Users::Image now:
# app/models/users.rb
class User < ActiveRecord::Base
has_one :image, :as => :assetable, :class_name => "Users::Image", #...
# ...
end
this (and maybe one restart of the server later) and everything should be fine.
Related
I’m using devise 4.2 with with Rails 4.2. I have this in my routes.rb file, within a namespace
devise_scope :user do
post 'sessions' => 'sessions#create'
delete 'sessions' => 'sessions#destroy'
end
Which I believe should be mapping to this controller
class Auth::SessionsController < Devise::SessionsController
layout false
skip_before_action :verify_authenticity_token
end
From the Rails console, I would like to confirm what the path will be for the "create" method of this controller. I tried this
my-app(dev)> url_for controller: :auth_sessions, only_path: true
NoMethodError: undefined method `url_for' for main:Object
from (irb):4
from /usr/share/rvm/gems/ruby-2.4.5#my-app/gems/railties-4.2.10/lib/rails/commands/console.rb:110:in `start'
from /usr/share/rvm/gems/ruby-2.4.5#my-app/gems/railties-4.2.10/lib/rails/commands/console.rb:9:in `start'
from /usr/share/rvm/gems/ruby-2.4.5#my-app/gems/railties-4.2.10/lib/rails/commands/commands_tasks.rb:68:in `console'
from /usr/share/rvm/gems/ruby-2.4.5#my-app/gems/railties-4.2.10/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /usr/share/rvm/gems/ruby-2.4.5#my-app/gems/railties-4.2.10/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
my-app(dev)>
But am getting the above error.
You can access the routes in the rails console by using either app or directly Rails.application.routes:
app.url_for(controller: "auth/sessions", action: :new, only_path: true)
# => "/auth/users/sign_in"
Rails.application.routes.url_for(controller: "auth/sessions", action: :new, only_path: true)
# => "/auth/users/sign_in"
Hi I have a normal setup of Paperclip and S3 for image uploads in my application, this is the model I use for attachments:
class Picture < ActiveRecord::Base
belongs_to :ofert, dependent: :destroy
has_attached_file :image, :styles => { :medium => "300x300#", :thumb => "100x100>", :large => "600x400#", :morethumb => "50x50#", :ultrathumb => "25x25#" },
:default_url => "https://s3-sa-east-1.amazonaws.com/:s3_bucket/ofert_defaults/:style/brown_hat.jpg"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
validates_attachment_presence :image, :if => :isProduction?
validates_attachment_size :image, :less_than => 5.megabytes
#process_in_background :image, processing_image_url: 'https://s3-sa-east-1.amazonaws.com/:s3_bucket/ofert_defaults/:style/brown_hat.jpg'
end
The above works very well, however, when I try to destoy a picture:
picture.destroy
I get the following error: stack level too deep
but if instead I do the following:
picture.delete
It works, however the above only deletes the record but not the file uploaded to my S3 bucket, any idea?
It is a bug in rails. Read here
Using
belongs_to :ofert, dependent: :destroy
will cause a circular loop (assuming you have a similar line in the associated model 'Ofert' as well)
You can try replacing it with dependent :delete in one of these models or write after_destroy methods in both to manually destroy the associated model.
Read this discussion here on stackoverflow
I am teaching myself RSpec (v3.1.7). I have installed rspec with rails g rspec:install into an existing rails app - freshly created.
I created a model: rails g rspec:model zombie. Ran the migration and all went well.
In: app/models/zombie.rb:
class Zombie < ActiveRecord::Base
validates :name, presence: true
end
In: app/spec/models/zombie_spec.rb:
require 'rails_helper'
RSpec.describe Zombie, :type => :model do
it 'is invalid without a name' do
zombie = Zombie.new
zombie.should_not be_valid
end
end
In terminal when I ran (in the app dir): rspec spec/models I get:
F
Failures:
1) Zombie is invalid without a name
Failure/Error: zombie.should_not be_valid
NoMethodError:
undefined method `name' for #<Zombie id: nil, created_at: nil, updated_at: nil>
# ./spec/models/zombie_spec.rb:6:in `block (2 levels) in <top (required)>'
Im following a video tutorial and I followed the video (Testing with RSpec) down to the latter. I'm like losing weight on the 2nd chapter. Am I missing something? Is the video using an older version of rspec for their video tutorial?
In my migration file:
class CreateZombies < ActiveRecord::Migration
def change
create_table :zombies do |t|
t.timestamps
end
end
end
Your model don't know what name is since you did not define the attribute in your migration:
class CreateZombies < ActiveRecord::Migration
def change
create_table :zombies do |t|
t.string :name
t.timestamps
end
end
end
Then run:
rake db:migrate
Then this should work fine:
z = Zombie.new(name: 'foo')
z.name
=> 'foo'
I think you are missing the name attribute. The following migration file will add name attribute to zombie model:
class AddNameToZombies < ActiveRecord::Migration
def change
add_column :zombies, :name, :string
end
end
finally run the following commands:
rake db:migrate
rake db:test:prepare
and that's it
I am attempting to create a file and image field on ActiveAdmin. Example.
What I have so far.
Created a Migration to add file and image columns to the database.
Created Active Admin and edited display form.
Created the model
Created 2 uploaders
My code results in "stack level too deep" error.
The weird thing is the code is exactly the same as my example which works fine.
admin/product.rb
ActiveAdmin.register Product do
permit_params :title, :image, :file
form(:html => { :multipart => true }) do |f|
f.inputs "Create Product..." do
f.input :title
f.input :image, :as => :file, :hint => f.template.image_tag(f.object.image.url(:thumb))
f.input :file, :as => :file
end
f.actions
end
end
models/product.rb
class Product < ActiveRecord::Base
mount_uploader :file, FileProductsUploader
mount_uploader :image, ImageProductsUploader
end
uploaders/file_products_uploader.rb
class FileProductsUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"#{Rails.root}/public/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
uploaders/image_products_uploader.rb
class ImageProductsUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
storage :file
def store_dir
"#{Rails.root}/public/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :thumb do
process :resize_to_fit => [250, 250]
end
end
Error Message
SystemStackError in Admin::ProductsController#update
stack level too deep
{"utf8"=>"✓",
"_method"=>"patch",
"authenticity_token"=>"ViiAmqH+S9XjP0wBSc7I2USEl1LXLw/N532Kd+uhNqc=",
"product"=>{"title"=>"First Product",
"image"=>#<ActionDispatch::Http::UploadedFile:0x50e6008 #tempfile=# <Tempfile:C:/Users/User/AppData/Local/Temp/RackMultipart20141016-7388-e5v6is>,
#original_filename="example.png",
#content_type="image/png",
#headers="Content-Disposition: form-data; name=\"product[image]\"; filename=\"example.png\"\r\nContent-Type: image/png\r\n">},
"commit"=>"Update Product",
"id"=>"1"}
Console Error message
Unpermitted parameters: utf8, _method, authenticity_token, commit, id
Completed 500 Internal Server Error in 138ms
SystemStackError (stack level too deep):
actionpack (4.1.5) lib/action_dispatch/middleware/reloader.rb:79
None of the online solutions worked for me. I solved the error changing the local temp directory
Uploader/*
def cache_dir
# should return path to cache dir
Rails.root.join 'tmp/uploads'
end
I'm using Rails 4.1.1 and I'm trying to create omniauth github with my devise gem in my application. But I get routing error, I'm following Railscast #235 revised on this topic.
Right now, I'm trying to do the same thing Ryan Bates is doing, which is raise omniauth.auth as a yaml to the screen, but I get the error:
No route matches [GET] "/auth/github/callback"
How do I fix this error?
Here you have my routes:
Rails.application.routes.draw do
devise_for :users, controllers: {omniauth_callbacks: "omniauth_callbacks"}
root to: 'questions#index'
resources :questions do
resources :answers, only: [:create]
end
resources :users, only: [:show]
#USERS CONTROLLER MY ROUTES
get "adding_likes/(:id)/(:like)/(:current_user_id)", to: "answers#adding_likes", as: :adding_likes
get "add_accept/(:answer_id)", to: "answers#accept", as: :accept
get "leaderboard", to: "users#leaderboard", as: :leaderboard
end
My controller:
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def all
raise request.env["omniauth.auth"].to_yaml
end
alias_method :github, :all
end
My user model:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :omniauthable
# :recoverable, :rememberable and :trackable
devise :database_authenticatable, :registerable, :validatable, :omniauthable
has_attached_file :avatar, :styles => { :small => "100x100>" }
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
# has_attached_file :superstarbadge, :styles => { :small => "100x100>" }
# validates_attachment_content_type :superstarbadge, :content_type => /\Aimage\/.*\Z/
has_many :questions
has_many :answers
def to_s
email
end
end
Important part of my devise.rb
config.omniauth :github,NOT-VISIBILE,IN-HERE
Basically, you haven't added the routes for it. I think you've missed Ryan add it to his routes file. Just add this:
get "/auth/github/callback" => "yourController#yourAction"
but since you're using omniauth, it's better this way
get "/auth/:provider/callback" => "yourController#yourAction
And add the necessary views for it.