Uploading multiple images with paperclip? - ruby-on-rails

I've looked at all the other questions on SO that look similar but I can't find an answer that will help me.
I'm working on upgrading an app that was written a while ago and am struggling to get the image upload section of it working. The relevant bits of code are as follows:
photos_controller.rb:
def create
images = params[:an_image]
if !images.nil?
photos = []
images.each do |image|
photo = #trip.photos.build(:user => current_user, :trip => #trip, :image => image)
if photo.save
p 'saving ok'
photos << photo
else
p 'ERROR saving'
end
end
add_news('created_multiple', photos.first, {:photo_ids => photos.collect(&:id)})
flash[:notice] = "Your photos were updated successfully!"
redirect_to #trip
else
#photo.user = current_user
#photo.trip = #trip
if #photo.update_attributes(params[:photo]) || params[:photo][:an_image]
add_news('created', #photo)
redirect_to trip_photos_url(#photo.trip)
else
render :edit
end
end
end
photo.rb:
def an_image=(file)
self.trip.photos.create(:image => file)
end
attr_accessor :an_image
# Photo image
if Rails.env.test?
has_attached_file :image, :path => "/tmp/:attachment-:id-:style.:extension"
else
has_attached_file :image,
# 200 x 160 for news
:styles => {
:list => "270x200>",
:feed_item => "75x60>",
:map => "30x30#",
:homepage => "160x110#",
:normal => "300x500",
:tiny => "20x20#"
},
:processors => [:auto_orient, :thumbnail],
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:bucket => "saild-production",
:convert_options => {:map => '-bordercolor white -compose over -border 2'}
end
and photos/new.html.erb:
<div id="content-four">
<h1>Upload photos to <%= #trip.name %></h1>
<%= form_tag trip_photos_path(#trip), :multipart => true %>
<form id="post-photo" action="#">
<fieldset class="multiple-uploads">
Add more files<br>
<%= file_field_tag :an_image, :class => 'file-upload' %>
<script type="text/javascript">
$('.duplicate').click(function() {
$('.file-upload:first').clone().appendTo($('.multiple-uploads'))
})
</script>
</fieldset>
<fieldset>
<input type="submit" class="submit button" value="Publish"/>
</fieldset>
</form>
</div>
But when I try to upload a file I keep getting this error:
NoMethodError (undefined method `each' for #<ActionDispatch::Http::UploadedFile:0x007f971263b860>):
app/controllers/photos_controller.rb:33:in `create'
actionpack (3.0.7) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (3.0.7) lib/abstract_controller/base.rb:150:in `process_action'
actionpack (3.0.7) lib/action_controller/metal/rendering.rb:11:in `process_action'
actionpack (3.0.7) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.0.7) lib/active_support/callbacks.rb:461:in `_run__1507672816926971504__process_action__674209566928286804__callbacks'
activesupport (3.0.7) lib/active_support/callbacks.rb:410:in `_run_process_action_callbacks'
activesupport (3.0.7) lib/active_support/callbacks.rb:94:in `run_callbacks'
actionpack (3.0.7) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.0.7) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.0.7) lib/active_support/notifications.rb:52:in `block in instrument'
activesupport (3.0.7) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.7) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.7) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.0.7) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.0.7) lib/abstract_controller/base.rb:119:in `process'
actionpack (3.0.7) lib/abstract_controller/rendering.rb:41:in `process'
actionpack (3.0.7) lib/action_controller/metal.rb:138:in `dispatch'
actionpack (3.0.7) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.0.7) lib/action_controller/metal.rb:178:in `block in action'
actionpack (3.0.7) lib/action_dispatch/routing/route_set.rb:62:in `call'
actionpack (3.0.7) lib/action_dispatch/routing/route_set.rb:62:in `dispatch'
actionpack (3.0.7) lib/action_dispatch/routing/route_set.rb:27:in `call'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:148:in `block in call'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:93:in `block in recognize'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:75:in `optimized_each'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:92:in `recognize'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:139:in `call'
actionpack (3.0.7) lib/action_dispatch/routing/route_set.rb:493:in `call'
hoptoad_notifier (2.4.11) lib/hoptoad_notifier/rack.rb:27:in `call'
sass (3.4.12) lib/sass/plugin/rack.rb:54:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/head.rb:14:in `call'
rack (1.2.8) lib/rack/methodoverride.rb:24:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/flash.rb:182:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/cookies.rb:302:in `call'
activerecord (3.0.7) lib/active_record/query_cache.rb:32:in `block in call'
activerecord (3.0.7) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache'
activerecord (3.0.7) lib/active_record/query_cache.rb:12:in `cache'
activerecord (3.0.7) lib/active_record/query_cache.rb:31:in `call'
activerecord (3.0.7) lib/active_record/connection_adapters/abstract/connection_pool.rb:354:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/callbacks.rb:46:in `block in call'
activesupport (3.0.7) lib/active_support/callbacks.rb:416:in `_run_call_callbacks'
actionpack (3.0.7) lib/action_dispatch/middleware/callbacks.rb:44:in `call'
rack (1.2.8) lib/rack/sendfile.rb:106:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
airbrake (4.1.0) lib/airbrake/rails/middleware.rb:13:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.0.7) lib/rails/rack/logger.rb:13:in `call'
rack (1.2.8) lib/rack/runtime.rb:17:in `call'
activesupport (3.0.7) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.2.8) lib/rack/lock.rb:13:in `block in call'
<internal:prelude>:10:in `synchronize'
rack (1.2.8) lib/rack/lock.rb:13:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/static.rb:30:in `call'
airbrake (4.1.0) lib/airbrake/user_informer.rb:16:in `_call'
airbrake (4.1.0) lib/airbrake/user_informer.rb:12:in `call'
hoptoad_notifier (2.4.11) lib/hoptoad_notifier/user_informer.rb:12:in `call'
railties (3.0.7) lib/rails/application.rb:168:in `call'
railties (3.0.7) lib/rails/application.rb:77:in `method_missing'
railties (3.0.7) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.2.8) lib/rack/content_length.rb:13:in `call'
rack (1.2.8) lib/rack/handler/webrick.rb:52:in `service'
/Users/camillavk/.rvm/rubies/ruby-1.9.3-p551/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/Users/camillavk/.rvm/rubies/ruby-1.9.3-p551/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/Users/camillavk/.rvm/rubies/ruby-1.9.3-p551/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
What I think the code is trying to do is create an array of the images that are uploaded and then .build(which I've never come across -> I assume it is like .create?) with them individually, but it doesn't seem to want to put the uploaded files into an array...
Does anyone know how I can fix this?
(I'm running Rails 3.0.7 and Ruby 1.9.3)

Your problem is that params[:an_image] is a single object, and you call .each on it as if it was an array.
I've not tried this with file data before, but try changing the name attribute of the file_field_tag to have square brackets at the end, like
file_field_tag "an_image[]"
When you have [] at the end of the name attribute, then the values from all inputs with the same name attribute will be collected together into an array. Might be as simple as that...on the other hand, there might be something about how the multipart data is structured which prevents this.
Another tip, if you have to deal with a variable that might be an array or a single object, is to wrap it in an array, and then call flatten on it. eg
#turns foos into an array, whether it is a single object or an array already
foos = [foos].flatten
Now you can do foos.each, safe in the knowledge that it will always be an array.
Also - tail your development.log file when your form submits and you can see what's in params, and compare it to what your code is expecting.

Related

Adding a column to model results in RangeError [Refactoring]

I'm currently working on a assignment for ruby on rails course I'm doing. I have to refactor a blog app written in Rails and add the functionality to merge articles.
What I did was to add a new text field in the form that is displayed when you edit the article
<%= text_field 'article', 'merge' %>
After I do that, it complains that there is no merge method in article model so I went ahead and implemented that.
I run the following migration:
class AddMergeWithToContents < ActiveRecord::Migration
def self.up
add_column :contents, :merge_with, :integer
end
def self.down
remove_column :contents, :merge_with
end
end
Then, when when accessing again the app it gives me the following error:
RangeError in Admin::SettingsController#update_database
bignum too big to convert into `long'
Rails.root: /home/saasbook/typo
Application Trace | Framework Trace | Full Trace
app/controllers/admin/settings_controller.rb:38:in `[]'
app/controllers/admin/settings_controller.rb:38:in `update_database'
Full Trace
app/controllers/admin/settings_controller.rb:38:in `[]'
app/controllers/admin/settings_controller.rb:38:in `update_database'
actionpack (3.0.17) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.0.17) lib/abstract_controller/base.rb:150:in `process_action'
actionpack (3.0.17) lib/action_controller/metal/rendering.rb:11:in `process_action'
actionpack (3.0.17) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.0.17) lib/active_support/callbacks.rb:457:in `block in _run__331447277__process_action__533401003__callbacks'
activesupport (3.0.17) lib/active_support/callbacks.rb:221:in `block in _conditional_callback_around_20300'
activesupport (3.0.17) lib/active_support/callbacks.rb:331:in `around'
activesupport (3.0.17) lib/active_support/callbacks.rb:315:in `_callback_around_20297'
activesupport (3.0.17) lib/active_support/callbacks.rb:220:in `_conditional_callback_around_20300'
activesupport (3.0.17) lib/active_support/callbacks.rb:456:in `_run__331447277__process_action__533401003__callbacks'
activesupport (3.0.17) lib/active_support/callbacks.rb:410:in `_run_process_action_callbacks'
activesupport (3.0.17) lib/active_support/callbacks.rb:94:in `run_callbacks'
actionpack (3.0.17) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.0.17) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.0.17) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.0.17) lib/active_support/notifications.rb:52:in `block in instrument'
activesupport (3.0.17) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.17) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.17) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.0.17) lib/abstract_controller/base.rb:119:in `process'
actionpack (3.0.17) lib/abstract_controller/rendering.rb:41:in `process'
actionpack (3.0.17) lib/action_controller/metal.rb:138:in `dispatch'
actionpack (3.0.17) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.0.17) lib/action_controller/metal.rb:178:in `block in action'
actionpack (3.0.17) lib/action_dispatch/routing/route_set.rb:68:in `call'
actionpack (3.0.17) lib/action_dispatch/routing/route_set.rb:68:in `dispatch'
actionpack (3.0.17) lib/action_dispatch/routing/route_set.rb:33:in `call'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:148:in `block in call'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:93:in `block in recognize'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:75:in `optimized_each'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:92:in `recognize'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:139:in `call'
actionpack (3.0.17) lib/action_dispatch/routing/route_set.rb:499:in `call'
actionpack (3.0.17) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
actionpack (3.0.17) lib/action_dispatch/middleware/head.rb:14:in `call'
rack (1.2.5) lib/rack/methodoverride.rb:24:in `call'
actionpack (3.0.17) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.0.17) lib/action_dispatch/middleware/flash.rb:182:in `call'
actionpack (3.0.17) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call'
actionpack (3.0.17) lib/action_dispatch/middleware/cookies.rb:302:in `call'
activerecord (3.0.17) lib/active_record/query_cache.rb:32:in `block in call'
activerecord (3.0.17) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache'
activerecord (3.0.17) lib/active_record/query_cache.rb:12:in `cache'
activerecord (3.0.17) lib/active_record/query_cache.rb:31:in `call'
activerecord (3.0.17) lib/active_record/connection_adapters/abstract/connection_pool.rb:354:in `call'
actionpack (3.0.17) lib/action_dispatch/middleware/callbacks.rb:46:in `block in call'
activesupport (3.0.17) lib/active_support/callbacks.rb:416:in `_run_call_callbacks'
actionpack (3.0.17) lib/action_dispatch/middleware/callbacks.rb:44:in `call'
rack (1.2.5) lib/rack/sendfile.rb:106:in `call'
actionpack (3.0.17) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.0.17) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.0.17) lib/rails/rack/logger.rb:13:in `call'
rack (1.2.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.0.17) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.2.5) lib/rack/lock.rb:13:in `block in call'
<internal:prelude>:10:in `synchronize'
rack (1.2.5) lib/rack/lock.rb:13:in `call'
actionpack (3.0.17) lib/action_dispatch/middleware/static.rb:30:in `call'
railties (3.0.17) lib/rails/application.rb:168:in `call'
railties (3.0.17) lib/rails/application.rb:77:in `method_missing'
railties (3.0.17) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.2.5) lib/rack/content_length.rb:13:in `call'
rack (1.2.5) lib/rack/handler/webrick.rb:52:in `service'
/home/saasbook/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/home/saasbook/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/home/saasbook/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
This is the settings controller
class Admin::SettingsController < Admin::BaseController
cache_sweeper :blog_sweeper
def index
if this_blog.base_url.blank?
this_blog.base_url = blog_base_url
end
load_settings
end
def write; load_settings end
def feedback; load_settings end
def errors; load_settings; end
def redirect
flash[:notice] = _("Please review and save the settings before continuing")
redirect_to :action => "index"
end
def update
if request.post?
Blog.transaction do
params[:setting].each { |k,v| this_blog.send("#{k.to_s}=", v) }
this_blog.save
flash[:notice] = _('config updated.')
end
redirect_to :action => params[:from]
end
rescue ActiveRecord::RecordInvalid
render params[:from]
end
def update_database
#current_version = Migrator.current_schema_version
#needed_version = Migrator.max_schema_version
#support = Migrator.db_supports_migrations?
#needed_migrations = Migrator.available_migrations[#current_version..#needed_version].collect do |mig|
mig.scan(/\d+\_([\w_]+)\.rb$/).flatten.first.humanize
end
end
def migrate
if request.post?
Migrator.migrate
redirect_to :action => 'update_database'
end
end
private
def load_settings
#setting = this_blog
end
end
Any help would be really appreciated. Thanks.
So to just answer the question, you can change the text_field to text_field_tag and remove the first argument. Or you can add a "setter" method to your article model named merge=(val) in which you can code whatever you need to. Hope this helps!

Paperclip - undefined method `match' for nil:NilClass

I'm new to rails, and ruby. I'm creating a simple application where I can upload a photo. I have the paperclip gem installed and I'm trying to configure it within my model to specify a thumbnail size like so:
has_attached_file :photo,
:styles => {:thumb => '120x120>', :large => '640x480>' },
:default_style => :thumb,
:url => "/system/photos/:basename.:extension",
:path => ":rails_root/public/system/photos/:basename.:extension"
However, I keep receiving the error:
undefined method `match' for nil:NilClass
When I remove the :styles parameter, everything works as expected.
Can anybody shed some light and point me in the right direction?
Thanks
EDIT:
Stack trace is as follows:
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/bundler/gems/paperclip-70f0f1fea030/lib/paperclip/content_type_detector.rb:60:in `type_from_file_command'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/bundler/gems/paperclip-70f0f1fea030/lib/paperclip/content_type_detector.rb:16:in `detect'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/bundler/gems/paperclip-70f0f1fea030/lib/paperclip/io_adapters/file_adapter.rb:14:in `cache_current_values'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/bundler/gems/paperclip-70f0f1fea030/lib/paperclip/io_adapters/file_adapter.rb:5:in `initialize'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/bundler/gems/paperclip-70f0f1fea030/lib/paperclip/io_adapters/registry.rb:29:in `new'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/bundler/gems/paperclip-70f0f1fea030/lib/paperclip/io_adapters/registry.rb:29:in `for'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/bundler/gems/paperclip-70f0f1fea030/lib/paperclip/attachment.rb:412:in `post_process_style'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/bundler/gems/paperclip-70f0f1fea030/lib/paperclip/attachment.rb:402:in `block in post_process_styles'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/bundler/gems/paperclip-70f0f1fea030/lib/paperclip/attachment.rb:401:in `each'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/bundler/gems/paperclip-70f0f1fea030/lib/paperclip/attachment.rb:401:in `post_process_styles'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/bundler/gems/paperclip-70f0f1fea030/lib/paperclip/attachment.rb:394:in `block (2 levels) in post_process'
activesupport (3.2.7) lib/active_support/callbacks.rb:403:in `_run__792123923__photo_post_process__530674861__callbacks'
activesupport (3.2.7) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.7) lib/active_support/callbacks.rb:385:in `_run_photo_post_process_callbacks'
activesupport (3.2.7) lib/active_support/callbacks.rb:81:in `run_callbacks'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/bundler/gems/paperclip-70f0f1fea030/lib/paperclip/callbacks.rb:26:in `run_paperclip_callbacks'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/bundler/gems/paperclip-70f0f1fea030/lib/paperclip/attachment.rb:393:in `block in post_process'
activesupport (3.2.7) lib/active_support/callbacks.rb:403:in `_run__792123923__post_process__530674861__callbacks'
activesupport (3.2.7) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.7) lib/active_support/callbacks.rb:385:in `_run_post_process_callbacks'
activesupport (3.2.7) lib/active_support/callbacks.rb:81:in `run_callbacks'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/bundler/gems/paperclip-70f0f1fea030/lib/paperclip/callbacks.rb:26:in `run_paperclip_callbacks'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/bundler/gems/paperclip-70f0f1fea030/lib/paperclip/attachment.rb:392:in `post_process'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/bundler/gems/paperclip-70f0f1fea030/lib/paperclip/attachment.rb:107:in `assign'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/bundler/gems/paperclip-70f0f1fea030/lib/paperclip.rb:196:in `block in has_attached_file'
activerecord (3.2.7) lib/active_record/attribute_assignment.rb:85:in `block in assign_attributes'
activerecord (3.2.7) lib/active_record/attribute_assignment.rb:78:in `each'
activerecord (3.2.7) lib/active_record/attribute_assignment.rb:78:in `assign_attributes'
activerecord (3.2.7) lib/active_record/base.rb:498:in `initialize'
activerecord (3.2.7) lib/active_record/persistence.rb:44:in `new'
activerecord (3.2.7) lib/active_record/persistence.rb:44:in `create'
app/controllers/photography_controller.rb:23:in `create'
actionpack (3.2.7) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.7) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.7) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.7) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.7) lib/active_support/callbacks.rb:414:in `_run__403597772__process_action__67609296__callbacks'
activesupport (3.2.7) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.7) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.7) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.7) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.7) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.7) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.7) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.7) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.7) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.7) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.7) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.7) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.7) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.7) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.7) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.7) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.7) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.7) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.7) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.7) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.7) lib/action_dispatch/routing/route_set.rb:600:in `call'
actionpack (3.2.7) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.1) lib/rack/etag.rb:23:in `call'
rack (1.4.1) lib/rack/conditionalget.rb:35:in `call'
actionpack (3.2.7) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.7) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.7) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context'
rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call'
actionpack (3.2.7) lib/action_dispatch/middleware/cookies.rb:338:in `call'
activerecord (3.2.7) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.7) lib/active_record/connection_adapters/abstract/connection_pool.rb:473:in `call'
actionpack (3.2.7) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.7) lib/active_support/callbacks.rb:405:in `_run__122749671__call__530674861__callbacks'
activesupport (3.2.7) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.7) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.7) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.7) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.7) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.7) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.7) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.7) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.7) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.7) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.7) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.7) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.7) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.7) lib/rails/engine.rb:479:in `call'
railties (3.2.7) lib/rails/application.rb:220:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.7) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
PhotographyController:
def create
#photo = Photo.create(params[:photo])
end
And finally, my form partial may be of some help:
<%= form_for #photo, :url => { :controller => 'photography', :action => 'create'}, :html => { :multipart => true } do |f| %>
<% if #photo.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(#photo.errors.count, "error") %> prohibited
this photo from being saved:</h2>
<ul>
<% #photo.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<%= f.file_field :photo %>
<div class="actions">
<%= f.submit ('Upload Photo') %>
</div>
<% end %>
Thanks again.
I've ran into this error, and other similar ones as well, using paperclip. But I run my development environment on windows, and the latest version of paperclip uses the `file' command, which windows doesn't seem to like so much.
Hopefully this will be fixed soon, but for now I sidestepped the issue by using the last version of paperclip that works, namely 3.0.4.
If you're running on windows as well, just add this to your Gemfile:
gem 'paperclip', '3.0.4'
P.S. It might be worthwhile to try using 3.0.4 on development, and the latest version on production.
As the accepted answer suggests, this error can happen when Paperclip is unhappy for a variety of reasons.
In my case, I was upgrading from 4.1.1 to 6.1.0 and encountered this error.
The update required that I use the aws-sdk-s3 gem.
I resolved the error by setting s3_region in the paperclip config which is required when using aws-sdk-s3.

Undefined method 'link_to_add\remove' for #<SimpleForm::FormBuilder>

Ive been doing some research and I can't seem to find what Im doing wrong.
I have installed simple_form and nested_form gems but when I use either link_to_add or remove I get the same error.
Heres the code
/fields.html.erb
<%= render 'shared/error_messages', :object => f.object %>
<%= f.input :nombre_cliente, :label => 'Nombre del cliente:', :as => :string %>
<%= f.input :mail_cliente, :label => 'E-mail del cliente:', :as => :string %>
<%= f.input :empresa_cliente, :label => 'Empresa:', :as => :string %>
<%= f.input :confirmada, :label => 'Ha sido confirmada por el cliente.' %>
<%= f.input :confirmacion_cliente, :label => 'Fecha de confirmacion por el cliente:', :as => :date %>
<b>something here</b>
</br></br>
<%= f.simple_fields_for :producto_de_cotizacion do |productos| %>
<%= productos.input :nombre %>
<%= productos.input :descripcion %>
<%= productos.input :precio%>
<%= productos.link_to_remove("Eliminar producto") %>
<% end %>
<%= f.link_to_add "Agregar producto", :producto_de_cotizacion %>
Some of it is in spanish but I think its understandable
Here is the actual error Im getting:
undefined method `link_to_remove' for < SimpleForm::FormBuilder:0x9ccf37c>
That is the entirety of _fields.html.erb, but its being called by the new.html.erb and the edit.html.erb files which are basically identical, Ill post the edit file:
<h1>Agregar Cotizacion</h1>
<div id='admin_view'>
<%= simple_form_for #cotizacion, :html => {:multipart => true} do |f| %>
<%= render "fields", :f => f %>
<%= f.button :submit, 'Actualizar Cotizacion' %>
<% end%>
</div>
After changing it to simple_nested_form I get a new error:
undefined method `klass' for nil:NilClass
and heres the stack
activesupport (3.0.7) lib/active_support/whiny_nil.rb:48:in `method_missing'
/home/peanut/.rvm/gems/ruby-1.9.2-p180/bundler/gems/nested_form-57f32788f195/lib/nested_form/builder_mixin.rb:23:in `block in link_to_add'
/home/peanut/.rvm/gems/ruby-1.9.2-p180/bundler/gems/nested_form-57f32788f195/lib/nested_form/view_helper.rb:37:in `call'
/home/peanut/.rvm/gems/ruby-1.9.2-p180/bundler/gems/nested_form-57f32788f195/lib/nested_form/view_helper.rb:37:in `block in after_nested_form_callbacks'
/home/peanut/.rvm/gems/ruby-1.9.2-p180/bundler/gems/nested_form-57f32788f195/lib/nested_form/view_helper.rb:36:in `map'
/home/peanut/.rvm/gems/ruby-1.9.2-p180/bundler/gems/nested_form-57f32788f195/lib/nested_form/view_helper.rb:36:in `after_nested_form_callbacks'
/home/peanut/.rvm/gems/ruby-1.9.2-p180/bundler/gems/nested_form-57f32788f195/lib/nested_form/view_helper.rb:13:in `simple_nested_form_for'
app/views/cotizacions/edit.html.erb:3:in `_app_views_cotizacions_edit_html_erb__855809706_86735240_354753338'
actionpack (3.0.7) lib/action_view/template.rb:135:in `block in render'
activesupport (3.0.7) lib/active_support/notifications.rb:54:in `instrument'
actionpack (3.0.7) lib/action_view/template.rb:127:in `render'
actionpack (3.0.7) lib/action_view/render/rendering.rb:59:in `block in _render_template'
activesupport (3.0.7) lib/active_support/notifications.rb:52:in `block in instrument'
activesupport (3.0.7) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.7) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.7) lib/action_view/render/rendering.rb:56:in `_render_template'
actionpack (3.0.7) lib/action_view/render/rendering.rb:26:in `render'
actionpack (3.0.7) lib/abstract_controller/rendering.rb:115:in `_render_template'
actionpack (3.0.7) lib/abstract_controller/rendering.rb:109:in `render_to_body'
actionpack (3.0.7) lib/action_controller/metal/renderers.rb:47:in `render_to_body'
actionpack (3.0.7) lib/action_controller/metal/compatibility.rb:55:in `render_to_body'
actionpack (3.0.7) lib/abstract_controller/rendering.rb:102:in `render_to_string'
actionpack (3.0.7) lib/abstract_controller/rendering.rb:93:in `render'
actionpack (3.0.7) lib/action_controller/metal/rendering.rb:17:in `render'
actionpack (3.0.7) lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
activesupport (3.0.7) lib/active_support/core_ext/benchmark.rb:5:in `block in ms'
/home/peanut/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/benchmark.rb:309:in `realtime'
activesupport (3.0.7) lib/active_support/core_ext/benchmark.rb:5:in `ms'
actionpack (3.0.7) lib/action_controller/metal/instrumentation.rb:40:in `block in render'
actionpack (3.0.7) lib/action_controller/metal/instrumentation.rb:78:in `cleanup_view_runtime'
activerecord (3.0.7) lib/active_record/railties/controller_runtime.rb:15:in `cleanup_view_runtime'
actionpack (3.0.7) lib/action_controller/metal/instrumentation.rb:39:in `render'
meta-tags (1.2.4) lib/meta_tags/controller_helper.rb:28:in `render_with_meta_tags'
actionpack (3.0.7) lib/action_controller/metal/implicit_render.rb:14:in `default_render'
actionpack (3.0.7) lib/action_controller/metal/implicit_render.rb:6:in `send_action'
actionpack (3.0.7) lib/abstract_controller/base.rb:150:in `process_action'
actionpack (3.0.7) lib/action_controller/metal/rendering.rb:11:in `process_action'
actionpack (3.0.7) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.0.7) lib/active_support/callbacks.rb:451:in `_run__761686272__process_action__750077467__callbacks'
activesupport (3.0.7) lib/active_support/callbacks.rb:410:in `_run_process_action_callbacks'
activesupport (3.0.7) lib/active_support/callbacks.rb:94:in `run_callbacks'
actionpack (3.0.7) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.0.7) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.0.7) lib/active_support/notifications.rb:52:in `block in instrument'
activesupport (3.0.7) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.7) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.7) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.0.7) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.0.7) lib/abstract_controller/base.rb:119:in `process'
actionpack (3.0.7) lib/abstract_controller/rendering.rb:41:in `process'
actionpack (3.0.7) lib/action_controller/metal.rb:138:in `dispatch'
actionpack (3.0.7) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.0.7) lib/action_controller/metal.rb:178:in `block in action'
actionpack (3.0.7) lib/action_dispatch/routing/route_set.rb:62:in `call'
actionpack (3.0.7) lib/action_dispatch/routing/route_set.rb:62:in `dispatch'
actionpack (3.0.7) lib/action_dispatch/routing/route_set.rb:27:in `call'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:148:in `block in call'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:93:in `block in recognize'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:89:in `optimized_each'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:92:in `recognize'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:139:in `call'
actionpack (3.0.7) lib/action_dispatch/routing/route_set.rb:493:in `call'
warden (0.10.7) lib/warden/manager.rb:35:in `block in call'
warden (0.10.7) lib/warden/manager.rb:34:in `catch'
warden (0.10.7) lib/warden/manager.rb:34:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/head.rb:14:in `call'
rack (1.2.4) lib/rack/methodoverride.rb:24:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/flash.rb:182:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/cookies.rb:302:in `call'
activerecord (3.0.7) lib/active_record/query_cache.rb:32:in `block in call'
activerecord (3.0.7) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache'
activerecord (3.0.7) lib/active_record/query_cache.rb:12:in `cache'
activerecord (3.0.7) lib/active_record/query_cache.rb:31:in `call'
activerecord (3.0.7) lib/active_record/connection_adapters/abstract/connection_pool.rb:354:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/callbacks.rb:46:in `block in call'
activesupport (3.0.7) lib/active_support/callbacks.rb:416:in `_run_call_callbacks'
actionpack (3.0.7) lib/action_dispatch/middleware/callbacks.rb:44:in `call'
rack (1.2.4) lib/rack/sendfile.rb:106:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.0.7) lib/rails/rack/logger.rb:13:in `call'
rack (1.2.4) lib/rack/runtime.rb:17:in `call'
activesupport (3.0.7) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.2.4) lib/rack/lock.rb:11:in `block in call'
<internal:prelude>:10:in `synchronize'
rack (1.2.4) lib/rack/lock.rb:11:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/static.rb:30:in `call'
railties (3.0.7) lib/rails/application.rb:168:in `call'
railties (3.0.7) lib/rails/application.rb:77:in `method_missing'
railties (3.0.7) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.2.4) lib/rack/content_length.rb:13:in `call'
rack (1.2.4) lib/rack/handler/webrick.rb:52:in `service'
/home/peanut/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/home/peanut/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/home/peanut/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
Use :locals => { :f => f }
EDIT: Changing my answer.
I just realized you are using:
simple_form_for
Change that to
simple_nested_form_for
This allows you to use the nested_form options (link_to_add, link_to_remove, etc).
Cheers!
So I changed the new.html.erb and edit.html.erb to look like this:
<h1>Agregar Cotizacion</h1>
<div id='admin_view'>
<%= simple_nested_form_for #cotizacion, :html => {:multipart => true} do |f| %>
<%= render 'fields', :f => f %>
<%= f.button :submit, 'Agregar Cotizacion' %>
<% end%>
</div>
And I added this to the routes.rb file
resources :cotizacions do
resources :producto_de_cotizacions
end
resources :producto_de_cotizacions
Im not exactly sure how but this fixed the issue. Thanks a lot for your help. Cheers!
I have this error, solved by restarting server and adding this code to model:
accepts_nested_attributes_for :photos, :reject_if => :all_blank, :allow_destroy => true

Authlogic undefined method `login_field' for Object:Class

I'm trying to set up Authlogic as my default authentication system. I want the user to be able to log in using either their username or email. To accomplish this, I did the following:
app/model/user.rb:
class User < ActiveRecord::Base
#add authlogic authentications
acts_as_authentic do |c|
c.find_by_login_method :find_by_username_or_email
end
attr_accessible :username, :email, :password, :password_confirmation
validates :username, :presence => true,
:length => { :within => 3..20 },
:uniqueness => true,
:format => { :with => /\A[a-z0-9][a-z0-9\-]+[a-z0-9]\z/ }
validates :email, :presence => true,
:format => { :with => /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i },
:uniqueness => { :case_sensative => false },
:length => { :maximum => 255 }
validates :password, :presence => true,
:confirmation => true,
:length => { :within => 6..40 }
def self.find_by_username_or_email(username_or_email)
find_by_username(username_or_email) || find_by_email(username_or_email)
end
end
app/model/session.rb:
class Session < Authlogic::Session::Base
end
app/controller/sessions_controller.rb:
class SessionsController < ApplicationController
before_filter :require_no_user, :only => [:new, :create]
before_filter :require_user, :only => :destroy
def new
#session = Session.new
end
def create
username = User.find_by_email(params[:session][:username_or_email]).username
username ||= params[:session][:username_or_email]
#session = Session.new(params[:session])
if #session.save
flash[:notice] = "Login successful!"
redirect_back_or_default :users
else
render :action => :new
end
end
def destroy
current_session.destroy
redirect_back_or_default new_session_url
end
end
app/views/sessions/new.html.erb:
<h1>Sign In</h1>
<%= render 'login_form' %>
app/views/sessions/_login_form.html.erb:
<%= form_for #session do |f| %>
<div class="field">
<%= f.label 'Username or Email' %><br />
<%= f.text_field :login %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="actions">
<%= f.submit "Sign In", :class => "button" %>
</div>
<% end %>
config/routes.rb:
Test::Application.routes.draw do
resources :users
resources :sessions, :only => :create
match '/signup', :to => 'users#new'
match '/signin', :to => 'sessions#new', :as => :signin
match '/signout', :to => 'sessions#destroy', :as => :signout
end
When I run the rails server and try to connect to http://localhost:3000/signin, it gives the following error:
undefined method `login_field' for Object:Class
The full stack trace is as follows:
authlogic (3.0.3) lib/authlogic/session/password.rb:90:in `login_field'
authlogic (3.0.3) lib/authlogic/session/password.rb:213:in `login_field'
authlogic (3.0.3) lib/authlogic/session/password.rb:118:in `initialize'
authlogic (3.0.3) lib/authlogic/session/activation.rb:48:in `initialize'
authlogic (3.0.3) lib/authlogic/session/klass.rb:64:in `initialize'
authlogic (3.0.3) lib/authlogic/session/scopes.rb:79:in `initialize'
authlogic (3.0.3) lib/authlogic/session/persistence.rb:37:in `new'
authlogic (3.0.3) lib/authlogic/session/persistence.rb:37:in `find'
app/controllers/application_controller.rb:15:in `current_session'
app/controllers/application_controller.rb:20:in `current_user'
app/controllers/application_controller.rb:33:in `require_no_user'
activesupport (3.0.7) lib/active_support/callbacks.rb:447:in `_run__2106190539632311804__process_action__1284228636937718599__callbacks'
activesupport (3.0.7) lib/active_support/callbacks.rb:410:in `_run_process_action_callbacks'
activesupport (3.0.7) lib/active_support/callbacks.rb:94:in `run_callbacks'
actionpack (3.0.7) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.0.7) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.0.7) lib/active_support/notifications.rb:52:in `block in instrument'
activesupport (3.0.7) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.7) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.7) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.0.7) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.0.7) lib/abstract_controller/base.rb:119:in `process'
actionpack (3.0.7) lib/abstract_controller/rendering.rb:41:in `process'
actionpack (3.0.7) lib/action_controller/metal.rb:138:in `dispatch'
actionpack (3.0.7) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.0.7) lib/action_controller/metal.rb:178:in `block in action'
actionpack (3.0.7) lib/action_dispatch/routing/route_set.rb:62:in `call'
actionpack (3.0.7) lib/action_dispatch/routing/route_set.rb:62:in `dispatch'
actionpack (3.0.7) lib/action_dispatch/routing/route_set.rb:27:in `call'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:148:in `block in call'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:93:in `block in recognize'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:68:in `optimized_each'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:92:in `recognize'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:139:in `call'
actionpack (3.0.7) lib/action_dispatch/routing/route_set.rb:493:in `call'
sass (3.1.1) lib/sass/plugin/rack.rb:54:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/head.rb:14:in `call'
rack (1.2.2) lib/rack/methodoverride.rb:24:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/flash.rb:182:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/cookies.rb:302:in `call'
activerecord (3.0.7) lib/active_record/query_cache.rb:32:in `block in call'
activerecord (3.0.7) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache'
activerecord (3.0.7) lib/active_record/query_cache.rb:12:in `cache'
activerecord (3.0.7) lib/active_record/query_cache.rb:31:in `call'
activerecord (3.0.7) lib/active_record/connection_adapters/abstract/connection_pool.rb:354:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/callbacks.rb:46:in `block in call'
activesupport (3.0.7) lib/active_support/callbacks.rb:416:in `_run_call_callbacks'
actionpack (3.0.7) lib/action_dispatch/middleware/callbacks.rb:44:in `call'
rack (1.2.2) lib/rack/sendfile.rb:107:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.0.7) lib/rails/rack/logger.rb:13:in `call'
rack (1.2.2) lib/rack/runtime.rb:17:in `call'
activesupport (3.0.7) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.2.2) lib/rack/lock.rb:11:in `block in call'
<internal:prelude>:10:in `synchronize'
rack (1.2.2) lib/rack/lock.rb:11:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/static.rb:30:in `call'
railties (3.0.7) lib/rails/application.rb:168:in `call'
railties (3.0.7) lib/rails/application.rb:77:in `method_missing'
railties (3.0.7) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.2.2) lib/rack/content_length.rb:13:in `call'
rack (1.2.2) lib/rack/handler/webrick.rb:52:in `service'
/Users/helixed/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/Users/helixed/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/Users/helixed/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
I don't really understand why this is happening. If I'm using find_by_login_method, then why does Authlogic even need login_field?
Update
I updated my code to match Olivier L's answer. The controller isn't working, but I think it's a lot closer.
app/models/user.rb:
class User < ActiveRecord::Base
#add authlogic authentications
acts_as_authentic do |c|
c.session_class = Session
c.find_by_login_method :find_by_username_or_email
end
attr_accessible :username, :email, :password, :password_confirmation
validates :username, :presence => true,
:length => { :within => 3..20 },
:uniqueness => true,
:format => { :with => /\A[a-z0-9][a-z0-9\-]+[a-z0-9]\z/ }
validates :email, :presence => true,
:format => { :with => /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i },
:uniqueness => { :case_sensative => false },
:length => { :maximum => 255 }
validates :password, :presence => true,
:confirmation => true,
:length => { :within => 6..40 }
def self.find_by_username_or_email(username_or_email)
find_by_username(username_or_email) || find_by_email(username_or_email)
end
end
app/models/session.rb:
class Session < Authlogic::Session::Base
authenticate_with User
end
I'm now getting the error:
undefined method `find_by_login_method' for #<Class:0x00000101a77ba8>
Here's the application trace:
activerecord (3.0.7) lib/active_record/base.rb:984:in `method_missing'
app/models/user.rb:30:in `block in <class:User>'
authlogic (3.0.3) lib/authlogic/acts_as_authentic/base.rb:36:in `acts_as_authentic'
app/models/user.rb:28:in `<class:User>'
app/models/user.rb:25:in `<top (required)>'
activesupport (3.0.7) lib/active_support/dependencies.rb:454:in `load'
activesupport (3.0.7) lib/active_support/dependencies.rb:454:in `block in load_file'
activesupport (3.0.7) lib/active_support/dependencies.rb:596:in `new_constants_in'
activesupport (3.0.7) lib/active_support/dependencies.rb:453:in `load_file'
activesupport (3.0.7) lib/active_support/dependencies.rb:340:in `require_or_load'
activesupport (3.0.7) lib/active_support/dependencies.rb:491:in `load_missing_constant'
activesupport (3.0.7) lib/active_support/dependencies.rb:183:in `block in const_missing'
activesupport (3.0.7) lib/active_support/dependencies.rb:181:in `each'
activesupport (3.0.7) lib/active_support/dependencies.rb:181:in `const_missing'
activesupport (3.0.7) lib/active_support/dependencies.rb:503:in `load_missing_constant'
activesupport (3.0.7) lib/active_support/dependencies.rb:183:in `block in const_missing'
activesupport (3.0.7) lib/active_support/dependencies.rb:181:in `each'
activesupport (3.0.7) lib/active_support/dependencies.rb:181:in `const_missing'
app/models/session.rb:2:in `<class:Session>'
app/models/session.rb:1:in `<top (required)>'
activesupport (3.0.7) lib/active_support/dependencies.rb:454:in `load'
activesupport (3.0.7) lib/active_support/dependencies.rb:454:in `block in load_file'
activesupport (3.0.7) lib/active_support/dependencies.rb:596:in `new_constants_in'
activesupport (3.0.7) lib/active_support/dependencies.rb:453:in `load_file'
activesupport (3.0.7) lib/active_support/dependencies.rb:340:in `require_or_load'
activesupport (3.0.7) lib/active_support/dependencies.rb:491:in `load_missing_constant'
activesupport (3.0.7) lib/active_support/dependencies.rb:183:in `block in const_missing'
activesupport (3.0.7) lib/active_support/dependencies.rb:181:in `each'
activesupport (3.0.7) lib/active_support/dependencies.rb:181:in `const_missing'
activesupport (3.0.7) lib/active_support/dependencies.rb:503:in `load_missing_constant'
activesupport (3.0.7) lib/active_support/dependencies.rb:183:in `block in const_missing'
activesupport (3.0.7) lib/active_support/dependencies.rb:181:in `each'
activesupport (3.0.7) lib/active_support/dependencies.rb:181:in `const_missing'
app/controllers/application_controller.rb:15:in `current_session'
app/controllers/application_controller.rb:20:in `current_user'
app/controllers/application_controller.rb:33:in `require_no_user'
activesupport (3.0.7) lib/active_support/callbacks.rb:447:in `_run__1226739957950205446__process_action__742106135127651722__callbacks'
activesupport (3.0.7) lib/active_support/callbacks.rb:410:in `_run_process_action_callbacks'
activesupport (3.0.7) lib/active_support/callbacks.rb:94:in `run_callbacks'
actionpack (3.0.7) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.0.7) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.0.7) lib/active_support/notifications.rb:52:in `block in instrument'
activesupport (3.0.7) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.7) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.7) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.0.7) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.0.7) lib/abstract_controller/base.rb:119:in `process'
actionpack (3.0.7) lib/abstract_controller/rendering.rb:41:in `process'
actionpack (3.0.7) lib/action_controller/metal.rb:138:in `dispatch'
actionpack (3.0.7) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.0.7) lib/action_controller/metal.rb:178:in `block in action'
actionpack (3.0.7) lib/action_dispatch/routing/route_set.rb:62:in `call'
actionpack (3.0.7) lib/action_dispatch/routing/route_set.rb:62:in `dispatch'
actionpack (3.0.7) lib/action_dispatch/routing/route_set.rb:27:in `call'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:148:in `block in call'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:93:in `block in recognize'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:68:in `optimized_each'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:92:in `recognize'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:139:in `call'
actionpack (3.0.7) lib/action_dispatch/routing/route_set.rb:493:in `call'
sass (3.1.1) lib/sass/plugin/rack.rb:54:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/head.rb:14:in `call'
rack (1.2.2) lib/rack/methodoverride.rb:24:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/flash.rb:182:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/cookies.rb:302:in `call'
activerecord (3.0.7) lib/active_record/query_cache.rb:32:in `block in call'
activerecord (3.0.7) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache'
activerecord (3.0.7) lib/active_record/query_cache.rb:12:in `cache'
activerecord (3.0.7) lib/active_record/query_cache.rb:31:in `call'
activerecord (3.0.7) lib/active_record/connection_adapters/abstract/connection_pool.rb:354:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/callbacks.rb:46:in `block in call'
activesupport (3.0.7) lib/active_support/callbacks.rb:416:in `_run_call_callbacks'
actionpack (3.0.7) lib/action_dispatch/middleware/callbacks.rb:44:in `call'
rack (1.2.2) lib/rack/sendfile.rb:107:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.0.7) lib/rails/rack/logger.rb:13:in `call'
rack (1.2.2) lib/rack/runtime.rb:17:in `call'
activesupport (3.0.7) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.2.2) lib/rack/lock.rb:11:in `block in call'
<internal:prelude>:10:in `synchronize'
rack (1.2.2) lib/rack/lock.rb:11:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/static.rb:30:in `call'
railties (3.0.7) lib/rails/application.rb:168:in `call'
railties (3.0.7) lib/rails/application.rb:77:in `method_missing'
railties (3.0.7) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.2.2) lib/rack/content_length.rb:13:in `call'
rack (1.2.2) lib/rack/handler/webrick.rb:52:in `service'
/Users/helixed/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/Users/helixed/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/Users/helixed/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
Edit: ok, I found this other question which is basically the same as yours. His accepted answer would indicate you need to do this:
class Session < Authlogic::Session::Base
authenticate_with User
end
class User < ActiveRecord::Base
acts_as_authentic do |c|
c.session_class = Session
c.find_by_login_method :find_by_username_or_email
end
end
Look here for a good way to implement multi-login-field authentication.
Just in case the link ever becomes broken, here's what Michael Cindric suggests in his blog (edited to use your classes' and fields' names):
class Session < Authlogic::Session::Base
find_by_login_method :find_by_username_or_email
end
class User < ActiveRecord::Base
def self.find_by_username_or_email(login)
find_by_username(login) || find_by_email(login)
end
end

Problem with ActionMailer in Rails3. (ssl hostname was not match with the server certificate)

strange error here, I setup an mailer to send a confirmation to the user after signing up in my rails3 app, it worked some days ago, now after moving it to another server and adding different other features i tried it again today and i get this error:
OpenSSL::SSL::SSLError in UsersController#create
hostname was not match with the server certificate
My initializers/setup_mail.rb file looks like this
ActionMailer::Base.delivery_method = :smtp # be sure to choose SMTP delivery
ActionMailer::Base.smtp_settings = {
:address => "mailout.eve-images.de",
:port => 587,
:domain => "eve-images.de",
:user_name => "xxxxxxx-noreply",
:password => "xxxxxxxx",
:authentication => "plain",
:enable_starttls_auto => true
}
My mailers/user_mailer.rb file
class UserMailer < ActionMailer::Base
default :from => "no-reply#eve-images.de"
def registration_confirmation(user)
mail(:to => user.email, :subject => "Registered")
end
end
and my users_controller
def create
#user = User.new(params[:user])
if #user.save
UserMailer.registration_confirmation(#user).deliver
session[:user_id] = #user.id
flash[:notice] = "Thank you for signing up! You are now logged in."
redirect_to "/"
else
....
end
end
I tried already to set the :enable_starttls_auto to false and got the same error.
Also I want my pw and user to be encrypted...
Any ideas are really appreciated!
Here is the full strack trace if it helps
/Users/eveadmin/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/openssl/ssl-internal.rb:121:in `post_connection_check'
mail (2.2.15) lib/mail/core_extensions/smtp.rb:16:in `tlsconnect'
/Users/eveadmin/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/smtp.rb:566:in `do_start'
/Users/eveadmin/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/smtp.rb:525:in `start'
mail (2.2.15) lib/mail/network/delivery_methods/smtp.rb:127:in `deliver!'
mail (2.2.15) lib/mail/message.rb:1967:in `do_delivery'
mail (2.2.15) lib/mail/message.rb:228:in `block in deliver'
actionmailer (3.0.3) lib/action_mailer/base.rb:401:in `block in deliver_mail'
activesupport (3.0.3) lib/active_support/notifications.rb:52:in `block in instrument'
activesupport (3.0.3) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.3) lib/active_support/notifications.rb:52:in `instrument'
actionmailer (3.0.3) lib/action_mailer/base.rb:399:in `deliver_mail'
mail (2.2.15) lib/mail/message.rb:228:in `deliver'
app/controllers/users_controller.rb:23:in `create'
actionpack (3.0.3) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.0.3) lib/abstract_controller/base.rb:151:in `process_action'
actionpack (3.0.3) lib/action_controller/metal/rendering.rb:11:in `process_action'
actionpack (3.0.3) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.0.3) lib/active_support/callbacks.rb:435:in `_run__2864349735198625628__process_action__3535857296079828011__callbacks'
activesupport (3.0.3) lib/active_support/callbacks.rb:409:in `_run_process_action_callbacks'
activesupport (3.0.3) lib/active_support/callbacks.rb:93:in `run_callbacks'
actionpack (3.0.3) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.0.3) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.0.3) lib/active_support/notifications.rb:52:in `block in instrument'
activesupport (3.0.3) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.3) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.3) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.0.3) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.0.3) lib/abstract_controller/base.rb:120:in `process'
actionpack (3.0.3) lib/abstract_controller/rendering.rb:40:in `process'
actionpack (3.0.3) lib/action_controller/metal.rb:138:in `dispatch'
actionpack (3.0.3) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.0.3) lib/action_controller/metal.rb:178:in `block in action'
actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:62:in `call'
actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:62:in `dispatch'
actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:27:in `call'
rack-mount (0.6.13) lib/rack/mount/route_set.rb:148:in `block in call'
rack-mount (0.6.13) lib/rack/mount/code_generation.rb:93:in `block in recognize'
rack-mount (0.6.13) lib/rack/mount/code_generation.rb:68:in `optimized_each'
rack-mount (0.6.13) lib/rack/mount/code_generation.rb:92:in `recognize'
rack-mount (0.6.13) lib/rack/mount/route_set.rb:139:in `call'
actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:492:in `call'
haml (3.0.25) lib/sass/plugin/rack.rb:41:in `call'
client_side_validations (3.0.4) lib/client_side_validations/middleware.rb:17:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/head.rb:14:in `call'
rack (1.2.1) lib/rack/methodoverride.rb:24:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/flash.rb:182:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/cookies.rb:295:in `call'
activerecord (3.0.3) lib/active_record/query_cache.rb:32:in `block in call'
activerecord (3.0.3) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache'
activerecord (3.0.3) lib/active_record/query_cache.rb:12:in `cache'
activerecord (3.0.3) lib/active_record/query_cache.rb:31:in `call'
activerecord (3.0.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:353:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/callbacks.rb:46:in `block in call'
activesupport (3.0.3) lib/active_support/callbacks.rb:415:in `_run_call_callbacks'
actionpack (3.0.3) lib/action_dispatch/middleware/callbacks.rb:44:in `call'
rack (1.2.1) lib/rack/sendfile.rb:107:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/show_exceptions.rb:46:in `call'
railties (3.0.3) lib/rails/rack/logger.rb:13:in `call'
rack (1.2.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.0.3) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.2.1) lib/rack/lock.rb:11:in `block in call'
<internal:prelude>:10:in `synchronize'
rack (1.2.1) lib/rack/lock.rb:11:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/static.rb:30:in `call'
railties (3.0.3) lib/rails/application.rb:168:in `call'
railties (3.0.3) lib/rails/application.rb:77:in `method_missing'
railties (3.0.3) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.2.1) lib/rack/content_length.rb:13:in `call'
rack (1.2.1) lib/rack/handler/webrick.rb:52:in `service'
/Users/eveadmin/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/Users/eveadmin/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/Users/eveadmin/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
solution:
:openssl_verify_mode => 'none'
Use:
ActionMailer::Base.delivery_method = :sendmail
Not
ActionMailer::Base.delivery_method = :smtp
I had the same problem, though Franks solution didn't work for me. I found a solutions that is along the same path.
:enable_starttls_auto => false # changed from true
reference: https://gist.github.com/1021285
For me :enable_starttls_auto => false didn't worked and I left it :enable_starttls_auto => true. But the thing I had gotten wrong was that in devise.rb I hadn't uncommented this line: config.mailer = "Devise::Mailer". When I removed the comment everything worked on my localhost. On the other hand when on the machine where I host my site I still get this error.

Resources