Paperclip - undefined method 'icon_file_name' - ruby-on-rails-3

I've just installed the Paperclip and trying to attach an icon to my model.
has_attached_file :icon,
:styles => { :normal => "100x100>", :format => 'png' },
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:url => "/icon/:slug.:extension"
:path => "icon/:slug.:extension"
s3.yml contains my bucket name and two keys.
slug interpolation is defined in the config/initializers/paperclip.rb as
Paperclip.interpolates('slug') do |attachment, style|
attachment.instance.cached_slug
end
When I call game.icon.url, I get this error:
undefined method `icon_file_name' for #<Game:0x4000f50>
What am I doing wrong?
I'm running rails 3.0.4 and ruby 1.9.2 on Windows 7 x64, if it makes any difference.

Did you create a migration for your Game model to add in the appropriate fields that Paperclip needs? From the Paperclip documentation on Github:
class AddAvatarColumnsToUser < ActiveRecord::Migration
def self.up
add_column :users, :avatar_file_name, :string
add_column :users, :avatar_content_type, :string
add_column :users, :avatar_file_size, :integer
add_column :users, :avatar_updated_at, :datetime
end
def self.down
remove_column :users, :avatar_file_name
remove_column :users, :avatar_content_type
remove_column :users, :avatar_file_size
remove_column :users, :avatar_updated_at
end
end
After you've created that migration, you need to run the rake task to update your db: rake db:migrate

You can simply run - rails generate paperclip game icon
and it will generate the migration for you.

I made the same mistake, forgot to add the database migrations.
Here is a great article on doing this
even though it is on Heroku.
You can run the migrations like so
Create the migration file
rails g migration AddAvatarToUser
Then edit the file to the following
class AddAvatarToUser < ActiveRecord::Migration
def self.up
add_attachment :users, :avatar
end
def self.down
remove_attachment :users, :avatar
end
end

I had a similar problem, but it was working when I ran it in the browser, yet some of my tests were failing. You helped me realize that I had migrated my main development database, but I had failed to do a rake db:migrate test. Once I did that - problem disappeared.

Related

How do I add columns to devise after the initial create_table rake?

I installed Devise, raked, and then realized afterwards, that I want to add :confirmable.
Can I go back to the same initial migration and just uncomment out the helper that I want and then rake db:migrate again?
I tried it and it didn't seem to work. But I haven't seen an example of how to create a follow-on migration.
Thanks!
This is what I tried:
1 class AddConfirmableToUsers < ActiveRecord::Migration
2 def self.up
3 change_table :users do |t|
4 t.confirmable
5 end
6 add_index :users, :confirmation_token, :unique => true
7 end
8
9 def self.down
10 remove_column :users, :confirmation_token
11 end
12
13 end
You can add the proper columns yourself like so:
class AddConfirmableToUsers < ActiveRecord::Migration
def self.up
change_table :users do |t|
t.string :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
end
add_index :users, :confirmation_token, :unique => true
end
def self.down
change_table :users do |t|
t.remove :confirmation_token, :confirmed_at, :confirmation_sent_at
end
remove_index :users, :confirmation_token
end
end
Your migration should work. Did you check your User model to make sure :confirmable is enabled? It's commented out by default.
If you don't mind losing your data you can just do
> rake db:drop
Otherwise you can just edit the initial migration and do a rollback.
# get the current migration version
> rake db:version
> Current version: ****************41
> rake db:rollback ****************40
Make your changes
> rake db:migrate

Heroku - Migration failing, how to change migration to run on Heroku?

I've got a migration file which does the following:
class ChangeLoginToUsername < ActiveRecord::Migration
def self.up
remove_column :users, :login, :string
add_column :users, :username, :string
end
def self.down
remove_column :users, :username, :string
add_column :users, :login, :string
end
end
This ran in fine on my local dev but I've now noticed the third parameter for the filed type on remove_column is erroring when I try and run this migration on Heroku. Is there a way to write/run specific migrations just for Heroku? There are 2 further migrations after this one that I need to run...
Any help hugely appreciated as always
It doesn't make sense for remove_column to have a data type:
class ChangeLoginToUsername < ActiveRecord::Migration
def self.up
remove_column :users, :login
....
end
def self.down
remove_column :users, :username
...
end
end

how to use auto_complete with rails3?

After searching on the web I got a problem :
I use rails3 and I do the following command
rails plugin install git://github.com/patshaughnessy/auto_complete.git
then i restart my rails server
my index.html.erb :
<%= text_field_with_auto_complete :departement, :nom, {}, {:method => :get} %>
my controller :
class AutocompController < ApplicationController
auto_complete_for :departement, :nom
end
I got a model like :
class CreateDepartements < ActiveRecord::Migration
def self.up
create_table :departements do |t|
t.column :region_id, :integer
t.column :num_dept, :string
t.column :nom, :string
end
end
def self.down
drop_table :departements
end
end
my routes.rb :
resources :autocomp, :collection => { :auto_complete_for_departement_nom => :get }
I get the following error :
No route matches {:controller=>"autocomp", :action=>"auto_complete_for_departement_nom"}
I don't get why it doesn't work? is auto_complete compatible with rails3? Or should I use jQuery?
You need to setup the route. Its missing, therefor you get this message.
Goto config/routes.rb and add your route, like:
resources : departements do
get : auto_complete_for_departement_nom, :on => :collection
end
Im using, autocomplete

How do I enable :confirmable in Devise?

The newest version of Devise doesn't have :confirmable enabled by default. I already added the respective columns to the User model but cannot find any code examples of how to enable :confirmable.
Where can I find a good example or what code do I need to enable it?
to "enable" confirmable, you just need to add it to your model, e.g.:
class User
# ...
devise :confirmable , ....
# ...
end
after that, you'll have to create and run a migration which adds the required columns to your model:
# rails g migration add_confirmable_to_devise
class AddConfirmableToDevise < ActiveRecord::Migration
def self.up
add_column :users, :confirmation_token, :string
add_column :users, :confirmed_at, :datetime
add_column :users, :confirmation_sent_at , :datetime
add_column :users, :unconfirmed_email, :string
add_index :users, :confirmation_token, :unique => true
end
def self.down
remove_index :users, :confirmation_token
remove_column :users, :unconfirmed_email
remove_column :users, :confirmation_sent_at
remove_column :users, :confirmed_at
remove_column :users, :confirmation_token
end
end
see:
Adding confirmable module to an existing site using Devise
I'd recommend to check the source code to see how Confirmable works:
https://github.com/plataformatec/devise/blob/master/lib/devise/models/confirmable.rb
You could also check the RailsCast on Devise:
http://railscasts.com/episodes/209-introducing-devise
Next it would be best to search for example applications on GitHub
This question seems to be odd one ;-) If you written some migration alike:
change_table(:users) do |t|
t.confirmable
end
add_index :users, :confirmation_token, :unique => true
and as you said little change in model (passing additional => :confirmable to devise) like so:
devise :database_authenticatable, :registerable, :confirmable
you can now generate some views (if you didn')
rails generate devise:views
You can go to app/views/devise/confirmations/new.html.erb and check how it looks like or change it. Furthermore you can inspect app/views/devise/confirmations/shared/_links.erb => there is line:
<%- if devise_mapping.confirmable? && controller_name != 'confirmations' %>
This condition checks if confirmable is turned on so... technically if everything went fine it should works OOTB. After creating new account - in log - you should see lines where confirmation mail is sent with appropriate link. It triggers:
Rendered devise/mailer/confirmation_instructions.html.erb
so you have got next place where you can customize it a bit
How to customize confirmation strategy? Please ask exact question what do you want to achieve. You can check devise gem path. In /lib/devise/models/confirmable.rb some comments could be helpful.
regards
If you've already installed devise into your app, and want to add "confirmable" later, instead of running:
rails generate devise:views
as mentioned by Piotr, run
rails generate devise:views confirmable
to produce only the views needed for "confirmable". You'll see output like this:
rails generate devise:views confirmable
invoke Devise::Generators::SharedViewsGenerator
create app/views/confirmable/mailer
create app/views/confirmable/mailer/confirmation_instructions.html.erb
create app/views/confirmable/mailer/reset_password_instructions.html.erb
create app/views/confirmable/mailer/unlock_instructions.html.erb
create app/views/confirmable/shared
create app/views/confirmable/shared/_links.erb
invoke form_for
create app/views/confirmable/confirmations
create app/views/confirmable/confirmations/new.html.erb
create app/views/confirmable/passwords
create app/views/confirmable/passwords/edit.html.erb
create app/views/confirmable/passwords/new.html.erb
create app/views/confirmable/registrations
create app/views/confirmable/registrations/edit.html.erb
create app/views/confirmable/registrations/new.html.erb
create app/views/confirmable/sessions
create app/views/confirmable/sessions/new.html.erb
create app/views/confirmable/unlocks
create app/views/confirmable/unlocks/new.html.erb
You'll then be able to access these files directly in your project to style them like your application. You'll also be able to change the messaging in the emails Devise sends out through the generated mailer views.
Last, don't forget to add config.action_mailer.delivery_method and config.action_mailer.smtp_settings in your app/config/environments/{environment_name}.rb file. This is what my production.rb file looks like:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => '[redacted]',
:user_name => '[redacted]',
:password => '[redacted]',
:authentication => 'plain',
:enable_starttls_auto => true }
Checkout devise wiki page. There is a full answer for your question.
For DRY, you can also put mailer config in config/initializers/mail.rb like:
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => '[redacted]',
:user_name => '[redacted]',
:password => '[redacted]',
:authentication => 'plain',
:enable_starttls_auto => true }
After configuring the ActionMailer setting described above I had to make one last addition to the config/environments/development.rb file to fix an error page that would show up after registering a new user:
config.action_mailer.default_url_options = { :host => 'localhost' }
More details about this solution: Heroku/devise - Missing host to link to! Please provide :host parameter or set default_url_options[:host]

Authlogic issue only in migration in rails 3 project

I'm trying to run the following migration
def self.up
add_column :users, :perishable_token, :string
User.all.each { |u| u.reset_perishable_token! }
change_column :users, :perishable_token, :string, :null => false
add_index :users, :perishable_token
end
and the u.reset_perishable_token! code behaves strangely (no return value, doesn't change the database field). Consequently change_column ..., :null => false fails with
users.perishable_token may not be NULL
Even separating the migration into two doesn't do the trick either if I run them with just one rake command.
Part One
def self.up
add_column :users, :perishable_token, :string
add_index :users, :perishable_token
end
Part Two
def self.up
User.all.each { |u| u.reset_perishable_token! }
change_column :users, :perishable_token, :string, :null => false
end
Only if I run the first and second migration in separate rake processes everything runs fine.
What could possibly be the reason and how can I fix it?
I think you need to add...
User.reset_column_information
...after you have added the perishable_token to the users_table, otherwise the User model is out of sync with the database.
I think the User model would only be loaded once per 'rake db:migrate', so it wouldn't help to split the migration in two.