0
votes

I am trying to implement a basic upload form in my rails app using Carrierwave. I have a model called image with an uploader called image_path and am getting the error:

undefined method `images_path' for #<#<Class:0x0000010369ea20>:0x00000103765288>

I'm not sure why it thinks it should be finding "images_path" instead of "image_path" - I searched for "images_path" in my app and found nothing.

This is what everything currently looks like:

html.erb:

<%= form_for Image.new do |f| %>
    <%= f.error_messages %>
    <%= f.hidden_field :spin_id %>
    <p>
        <%= f.file_field :image_path, multiple: true %>
        <%= f.submit %> 
    </p>
<% end %>

image.rb

class Image < ActiveRecord::Base
    belongs_to :spin

    mount_uploader :image_path, ImagePathUploader
end

image_path_uploader.rb

class ImagePathUploader < CarrierWave::Uploader::Base
  storage :file
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
end

Would really appreciate any help as I am completely stumped.

Here is the full error:

ActionView::Template::Error (undefined method `images_path' for #<#<Class:0x00000104f36da8>:0x000001037e4ec0>):
    40: 
    41:                         </div>
    42:                         <%# add file input stuff here %>
    43:                         <%= form_for Image.new, :html => {:multipart => true} do |f| %>
    44:                             <%= f.error_messages %>
    45:                             <%= f.hidden_field :spin_id %>
    46:                             <p>
  app/views/home/index.html.erb:43:in `block (2 levels) in _app_views_home_index_html_erb__3305799271767470298_2188818660'
  app/views/home/index.html.erb:30:in `block in _app_views_home_index_html_erb__3305799271767470298_2188818660'
  app/views/home/index.html.erb:26:in `_app_views_home_index_html_erb__3305799271767470298_2188818660'


  Rendered /Users/[]/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.2ms)
  Rendered /Users/[]/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.9ms)
  Rendered /Users/[]/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (11.1ms)
1
Try to rename it to image_file for example and try to reproduce error again. - Maxim
this is so strange - I changed everything to image_file as you suggested and it is still looking for images_path. There is no "image_path" or "images_path" anywhere in my app, and I've restarted the server of course. - scientiffic
Requires full backtrace of error. - Maxim
I added the full error message - scientiffic

1 Answers

2
votes

forms with new objects will post to the plural path (in this case images_path). This is coming from the Image.new (where .new_record? is true). It is caused by the polymorphic path method, if you want to look into it further.

This is expected and good. In your routes, you should have the ability to create an Image object, for example:

resources :images

This will create a number of routes.

One will be a POST to the images_path url. This should not interfere with the image_path method in the image, but you are using a lot of similar names that can be confusing.

To see what routes you have, try rake routes from the command line.