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
Related
I want to upload images to a place in active admin dashboard by paperclip, but When i pressed "add new place picture", nothing happened. Image as below:
model/place.rb
class Place < ActiveRecord::Base
has_many :place_pictures, :dependent => :destroy,
:autosave => true
accepts_nested_attributes_for :place_pictures,
:allow_destroy => true
end
model/place_picture.rb
class PlacePicture < ActiveRecord::Base
belongs_to :place
attr_accessible :picture, :logo
has_attached_file :picture, :styles => { :medium => "300x300>", :thumb => "100x100>" },
:default_url => "/images/:style/missing.png"
validates_attachment_content_type :picture, :content_type => /\Aimage\/.*\Z/
end
admin/place.rb
ActiveAdmin.register Place do
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Place details" do
f.input :name
f.input :about
f.has_many :place_pictures do |ff|
ff.input :picture, :as => :file, :hint => ff.template.image_tag(ff.object.picture.url(:thumb))
ff.input :logo, as: :boolean, :label => "isLogo"
end
end
f.actions
end
index do
column :id
column :name
column :city
column :about
column "Images" do |place|
if !(place.place_pictures.empty?)
ul do
place.place_pictures.each do |img|
li do
image_tag(img.picture.url(:thumb))
end
end
end
end
end
actions
end
show do |ad|
attributes_table do
row :name
row :city
row :about
row :image do
if !(place.place_pictures.empty?)
ul do
place.place_pictures.each do |img|
li do
image_tag(img.picture.url(:thumb))
end
end
end
end
end
end
#active_admin_comments
end
end
Gemfile:
gem 'rails'#version 4.1.8
gem 'activeadmin', github: 'activeadmin'
gem "paperclip", "~> 4.2"
It worked well before on rails 3.x and active_admin 0.6.x. Any help? thanks!
UPDATE:
Finally change active_admin.js to below, it works! don't know why! hope help anyone if has this situation.
// = require jquery
// = require jquery_ujs
// = require active_admin/base
It seems the javascript that appends the fields (has_many), doesn't work. Do you have any JavaScript runtime on your system? Node.js for example.
https://github.com/sstephenson/execjs
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.
I am using Rails 4.1.1, ruby 2.1, mongodb, mongoid as a wrapper, rails_admin for creating admin interfaces
I know that 'attr_accessible' no longer works for Rails4. So i have installed 'protected_attributes' gem. But still no success i am still getting warning in my console
[RailsAdmin] Could not load model Company, assuming model is non existing. (undefined method `attr_accessible' for Company:Class)
So, rails admin do not load the class Company because i have defined attr_accessible in the model. Here is my company model.
class Company
include Mongoid::Document
##employees_strength = {0 => '0-10', 1 => '11-50', 2 => '51-100', 3 => '101-500', 4 => '501-1000', 5 => '1000+', 6 => '5000+'}
field :name, type: String
field :website, type: String
field :domain_name, type: String
field :strength, type: Integer
has_many :employees
has_one :admin, :class_name => 'Employee', :dependent => :destroy, :inverse_of => :organization
#attr_accessible :name, :website, :domain_name, :strength#, :admin_attributes, :allow_destroy => true
attr_accessible :admin_attributes
accepts_nested_attributes_for :admin, :allow_destroy => true
end
Please any can body can help?
Thanks
Mongoid 4 (<= 4.0.2 at the time of writing) does not know about the ActiveModel::MassAssignmentSecurity module provided by protected_attributes gem.
As such you must include the behaviour in your models manually e.g.
class SomeDocument
include Mongoid::Document
include ActiveModel::MassAssignmentSecurity
field :some_field
attr_accessible :some_field
end
However, this gets tedious pretty quickly so a reasonable alternative is to include the module into the Mongoid::Document module before any of your models are defined.
module Mongoid
module Document
include ActiveModel::MassAssignmentSecurity
end
end
I've been jumping between design patterns, firstly trying polymorphic, now landing on STI. The main goal is to implement a Server > Host > Guest model where a Server has Hosts, Hosts have Guests and each able to have Posts. Although not the main purpose of the question any ideas in the design matter would be helpful as this is my first rails or ruby project.
What I have now is:
class Device
has_may :children, :class_name => "Device", :foreign_key => "parent_id"
belongs_to :parent, :class_name => "Device"
has_many :posts
end
class Server,Host,Guest < Device
end
STI is used because Server,Host,Guest basically have the same attributes.
I'm having trouble setting up the routes and controllers so I could view a Server's children which would be of type Host or to create a new Server's Host.
First, a good thing would be to add the following things, making everything easier to use for you :
class Device < ActiveRecord::Base
has_many :children, :class_name => "Device", :foreign_key => "parent_id"
has_many :servers, -> { where type: "Server" }, :class_name => "Device", :foreign_key => "parent_id"
has_many :hosts, -> { where type: "Host" }, :class_name => "Device", :foreign_key => "parent_id"
has_many :guests, -> { where type: "Guest" }, :class_name => "Device", :foreign_key => "parent_id"
belongs_to :parent, :class_name => "Device"
has_many :posts
end
With that, you will be able to do server.hosts, etc, which is quite convenient.
Then, you should move each subclass (Server, Host, Guest) to its own file due to Rails loading system. You can try to access the model Server in the console, you will get an undefined error. To fix it, you need to load the model Device, or simply move each subclass in a different file.
Finally, for the routing/controller part, I will advise you to read this post I wrote about common controller for STI resources : http://samurails.com/tutorial/single-table-inheritance-with-rails-4-part-2/.
Note that this is the second part, for more details check out the other articles.
How to set the following association:
class Midatum < ActiveRecord::Base
# ..., diagn1, diagn2, diagn3
# sample data:
# ..., "0123", nil ,"0124"
# ..., "0123", nil ,"0124"
# ..., "0123", "1123", nil
belongs_to :icd9, :foreing_key => :diagn1
belongs_to :icd9, :foreing_key => :diagn2
belongs_to :icd9, :foreing_key => :diagn3
end
class icd9 < ActiveRecord::Base
# icd9, description
# sample data:(unique)
#"0123", "some text"
#"0124", "some other text"
#"1123", "description text"
#"1133", "description text"
has_many :midata, :foreing_key => :icd9, :primary_key => :icd9
end
This does not work. It may be obvious for someone but not for me. The database
is a legacy DB and readonly. I need to establish this assoc to able to work with the data.
This answer comes from a Rails expert and it does solve my problem. I am posting it in case someone else have the same problem.
belongs_to :icd9_a, :foreign_key => :diagn1, :class_name => "Icd9"
belongs_to :icd9_b, :foreign_key => :diagn2, :class_name => "Icd9"
belongs_to :icd9_c, :foreign_key => :diagn3, :class_name => "Icd9"
But that means you'll need to query the association using all three methods:
m = Midatum.first
m.icd9_a
m.icd9_b
m.icd9_c
In the same way, over in the Icd9 class you'll need three separate associations with unique names:
class Icd9 < ActiveRecord::Base
self.primary_key = :icd9
has_many :midata_a, :foreign_key => :diagn1, :class_name => "Midatum"
has_many :midata_b, :foreign_key => :diagn2, :class_name => "Midatum"
has_many :midata_c, :foreign_key => :diagn3, :class_name => "Midatum"
end
Note also that since the icd9 table doesn't have an 'id' column but uses the 'icd9' column as the primary key, you'll need to set it as I've done:
self.primary_key = :icd9