rails migrating from paperclip to carrierwave_direct - file-upload

I have migrated from Paperclip to Carrierwave, and using Carrierwave_direct to upload images directly to S3.
class User < ActiveRecord::Base
mount_uploader :profile_picture, ProfilePictureUploader, :mount_on => :profile_picture_file_name
So, in my schema, I don't have column profile_picture but profile_picture_file_name in my users table.
This creates problem when I am trying to create the #uploader instance variable
class ProfilePictureController < ApplicationController
def show
#user=current_user
#uploader = #user.profile_picture_file_name
#uploader.success_action_redirect = crop_url
end
This throws an error, when a user is trying to upload the Profile Image,
undefined method `success_action_redirect=' for nil:NilClass

I guess this could fix it:
#uploader = #user.profile_picture

Related

Devise sign_in_params method missing

I'm attempting to use Devise (2.2.4), which I'm new to, with the Rails 3.2.13/Ruby 2.0.0p195 app I'm building. I turned scoped_views on because I want to have my own separate users and admins views. And I created my own Users::RegistrationsController which seems to be doing what I want it to. I've just added my own Users::SessionsController, which is where I've hit problems.
I straight copied over a couple of action methods from the Devise::SessionsController source as a first step, planning to modify them once they were working (my controller code is at the bottom of this post). But my 'new' method is failing, when called, with a NameError because `sign_in_params' is apparently undefined.
Well, that seems pretty strange because I'm inheriting from Devise::SessionsController, and when I look at the source for that on GitHub, there's the sign_in_params defined in the protected section at the bottom. So I decided to investigate whether my controller is inheriting correctly from Devise::SessionsController - and it certainly seem to be. I can list out all the inherited methods, just not that one missing one. So I ended up running the following piece of code in the Rails Console:
(Devise::SessionsController.new.methods - DeviseController.new.methods).each {|m| puts m}
And it produces the following output:
_one_time_conditions_valid_68?
_one_time_conditions_valid_72?
_callback_before_75
_one_time_conditions_valid_76?
new
create
destroy
serialize_options
auth_options
If I ignore the underscored methods, the remainder are all those methods defined in the Devise::SessionsController source except sign_in_params. I can't see how anything I've written can be deleting that method, and I can't think what else to try. Google is silent on this problem, so I assume I'm doing something uniquely foolish, but I can't work out what. Any suggestions please? And might someone else try running that bit of Rails Console code to see what they get?
class Users::SessionsController < Devise::SessionsController
prepend_before_filter :require_no_authentication, :only => [ :new, :create ]
prepend_before_filter :allow_params_authentication!, :only => :create
prepend_before_filter { request.env["devise.skip_timeout"] = true }
# GET /resource/sign_in
def new
self.resource = resource_class.new(sign_in_params)
clean_up_passwords(resource)
respond_with(resource, serialize_options(resource))
end
# POST /resource/sign_in
def create
self.resource = warden.authenticate!(auth_options)
set_flash_message(:notice, :signed_in) if is_navigational_format?
sign_in(resource_name, resource)
respond_with resource, :location => after_sign_in_path_for(resource)
end
end
I think you are using code from a devise version compatible with Rails 4 on a rails 3 application.
sign_in_params is a method to be used with strong parameters. A gem used in rails 4.
If you check the controller on devise version 2.2. https://github.com/plataformatec/devise/blob/v2.2/app/controllers/devise/sessions_controller.rb
You will see that there is no sign_in_params method.
Check which version of devise you are using and copy the code based on that devise version in your controller, rather than the latest code from github.

uninitialized constant error in ruby on rails

Iam working in ruby on rails for fetching the existing db tables from remote server mechine(SQL SERVER) .Actually i don't know how to do this.Am following this way.please rectify me
My problem is while tying to run uninitialized constant TrDeviceDetailsController::TRDeviceDetail is getting.
I set the following in database.yml file.
development:
adapter: sqlserver
mode: odbc
database: BObd
dsn: newdb_64
username: ush
password: Ushu
host: ws1a20\SQLEXPRESS
The table exist in the db BOdb is TRDeviceDetails.I created models and controller using the command
rails generate model `TRDeviceDetail`
rails generate controller `TRDeviceDetails`
And in controller i put the following
class TrDeviceDetailsController < ApplicationController
def show
#devices = TRDeviceDetail.find(:all)
end
end
model file
class TrDeviceDetail < ActiveRecord::Base
# attr_accessible :title, :body
attr_accessible :UniqueDeviceID
end
where UniqueDeviceID is the existing column in table TrDeviceDetails
and created a show.html.erb file for displaying the UniqueDeviceID
<h1>TrDeviceDetails#show</h1>
<p>Find me in app/views/tr_device_details/show.html.erb</p>
<%#device.inspect%>
what i need is,get existing tables from remote machine.How it is possible and why this error is occurring?
you should use
#devices = TrDeviceDetail.find(:all) # small 'r'
as the generated class is
class TrDeviceDetail < ActiveRecord::Base
UPDATE:
If the table name is not something that follows convention, you should set the table_name explicitly
class TrDeviceDetail < ActiveRecord::Base
set_table_name 'TRDeviceDetails'
end

Devise + Declarative_authorization + role_model + different users model name : undefined method `current_user'

I do have this famous error : "undefined method `current_user'" with declarative authorization, though I set up this variable in the application_controller.rb :
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :set_current_user
protected
def set_current_user
Authorization.current_user = current_admin_utilisateur
end
end
I'm using a table called "admin_utilisateurs" instead of "users". Which was activated in Devise with : "rails generate devise admin_utilisateur"
Devise is working great.
For info, I customized my users table (admin_utilisateurs) with "roles_model" gem, So that I do have an attribut roles_mask that allows me to manage different roles while providing a role_symbols method for declarative authorization.
The problem is now that I got this strange error though the Authorization.current_user is set by the application_controller.rb.
This is the begning of one my resource controllers that procude the error :
class PubResponsablesController < ApplicationController
before_filter :authenticate_admin_utilisateur!
filter_resource_access
...
end
I search by google for this error, but none of the results provide a working solution.
Could anybody help me on this ?
Many Thanks
Ok this is the final answer.
I modified my app/controller/application_controller.rb because I don't use the #current_user instance variable in the views :
class ApplicationController < ActionController::Base
protect_from_forgery
# This is mandatory if you want to secure as well your app/models
before_filter :set_current_user
# This method is required by declarative_authorization on every controller
# that is using filter_resource_access (or any other declarative_auth.. mechanism)
def current_user
current_admin_utilisateur
end
protected
def set_current_user
Authorization.current_user = current_admin_utilisateur
end
end
As I said I'm using the following gem in collaboration :
gem devise for the authentication
The user-model-name is "admin_utilisateur" instead of "user", but it could have been : account, member, group or what you need.
gem role_model to provide a brillant role method "role_symbols" to my user model
*The method role_symbols was returning a "Set" subclass instead of an "Array" but after quick post on Github, the developer (martinrehfeld) fixed this compatibility issue in a lightning matter of minutes. Great !*
gem declarative_authorization to provide access management based on roles.
My will to use a different model name than "user" is confirmed to work by the following post.
The only thing that declarative_authorization needs is the current_user method on each controller. As I'm using a different model name with Devise (such as admin_utilisateur, account, member, ...) the helper created by devise have a different name. Instead of current_user, it is current_admin_utilisateur (or current_account, current_member). So I have to create my own current_user method.
The role_symbols method required by declarative_authorization is provided by role_model gem.
I hope this will help other developer cause I spent two days to sort out how all this fabric works together. Devise took me even more with routing issues.
My few cents to RoRrrr ;-)
Ok I managed to solve this error by modifying my app/controller/application_controller.rb :
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :set_current_user
def current_user
#current_user = current_admin_utilisateur
end
protected
def set_current_user
Authorization.current_user = current_admin_utilisateur
end
end
I just created my own current_user method that create an instance variable #current_user. This one is initialized with the value of current_admin_utilisateur which is based on Devise helpers and my customized user model called admin_utilisateur. (my model could as well be called acount, member, or whatever...)
I placed my current_user method in application_controller in order that it to be available in every controller of my application.
Now, I'm getting another error :
User.role_symbols doesn't return an Array of Symbols (#<RoleModel::Roles: {:developer, :admin, :coordinator, :manager, :assistant, :distributor, :exporter, :historian}>)
I don't understand because the roles_model gem provide an alias method 'role_symbols' to the admin_utilisateur model.

Removing Carrierwave previously uploaded file messes up new file processing

I'm using https://github.com/jnicklas/carrierwave with AWS3 to upload my app file to Amazon. I've got an Employee model with an image column (think of it as the employee profile picture) which has an uploader mounted to it:
class Employee < ActiveRecord::Base
mount_uploader :image, ProfileImageUploader
...
end
Whenever an employee updates its profile picture, I want the previous one to be deleted. In order to do this, I've got the following :after_update callback in my Employee model:
class Employee < ActiveRecord::Base
...
after_update :remove_changed_image, :if => 'self.image_changed?'
def remove_changed_image
self.image_was.remove!
end
end
This successfully deletes the previous file. But I'm also processing the pictures that get uploaded. In my Uploder I have the following:
class ProfileImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
# Create different versions of your uploaded files:
version :thumb do
process :resize_to_limit => [300, 300]
end
...
end
The problem is that the new files are not being processed at all. Only one version, the unprocessed one, gets uploaded, whereas if I don't remove the previous image, then all works as it should (many versions are uploaded).
Any help? Thanks!
The problem is with the after_update callback. It is called after the object is saved, thus deleting the newly attached file. You need to call #employee.remove_image before saving the object.

Duplicate mounted_uploader Carrierwave

So I have an model, Photo, I want to duplicate the whole model including the mounted image.
old_photo = Photo.find(id)
new_photo = Photo.new(old_photo.attributes)
class Photo
include Mongoid::Document
mount_uploader :image, ImageUploader
end
But how can I duplicate the mounted image and all it's versions? i.e create an exact copy of the photo, not use the same. (since they can be changed later)
This question is similar: https://stackoverflow.com/questions/7287905/duplicate-an-image-on-amazon-s3-that-was-uploaded-using-carrierwave-fog-and-rai
He's asking about AWS::S3::S3Object.copy from the aws-s3 gem, can this be used to achieve this? if so, how?
I'm using fog for the connection to S3, can it be done with fog? If so, how?
Update:
new_photo = Photo.new(old_photo.attributes)
new_photo.image.download!(old_photo.image_url)
new_photo.store_image!
raise new_photo
=> <#Photo _id: 4f1ff69566eaa70ed800001d, image: nil>
raise new_photo.image
=> https://x.s3.amazonaws.com/uploads/photo/image/4f1ff76566eaa70ed8000020/file.png
Any idea why image is nil on new_photo while I still can access it?