Reputation: 51
I am using CarrierWave to handle image uploading for my rails app (rails 4.1).
The issue is I am getting this
undefined method `images_path' for #<#
error every time try to go to /image/new.
I have checked my routes, my views, my controller, my model, and my uploader and everything seems fine. any advice or ideas?
Code:
image_uploader.rb:
class ImageUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
/models/image.rb:
class Image < ActiveRecord::Base
mount_uploader :image, ImageUploader
end
Relevant Routes:
Rails.application.routes.draw do
resources :image
image_controller.rb
class ImageController < ApplicationController
def new
@image = Image.new(:image => params[:image_params])
end
def create
@image = Image.create( image_params )
if @image.save
redirect_to @image
else
render 'new'
end
end
def show
end
private
def image_params
params.require(:image).permit(:title, :description, :image
end
def find_image
@image = Image.find(params[:id])
end
end
new.html.erb
<div class="col-md-12 col-md-offset-2">
<%= form_for @image, html: { multipart: true } do |f| %>
<%= f.file_field @image %>
<p id="uploadClick">Click to Upload</p>
<br>
<%= submit_tag 'Upload Image', id: 'submitPhoto' %>
<% end %>
</div>
Any help with this would be greatly appreciated, thanks!
SOLUTION:
in new.html.erb I had to change:
<%= form_for @asset, html: { multipart: true } do |f| %>
to
<%= form_for :asset, html: { multipart: true } do |f| %>
my new action is now working correctly after those changes, hope this helps people in the future.
Upvotes: 2
Views: 499
Reputation: 33542
Try changing resources :image
to resources :images
in routes.rb
and change new
and create
methods in the controller like below
def new
@image = Image.new
end
def create
@image = Image.new(image_params)
if @image.save
redirect_to @image
else
render 'new'
end
end
Upvotes: 1