【发布时间】:2015-09-17 03:42:05
【问题描述】:
我正在使用 CarrierWave 为我的 rails 应用(rails 4.1)处理图片上传。
问题是我得到了这个
# 的未定义方法 `images_path'
每次尝试转到 /image/new 时都会出错。
我检查了我的路线、我的视图、我的控制器、我的模型和我的上传器,一切似乎都很好。有什么建议或想法吗?
代码:
image_uploader.rb:
class ImageUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
结束
/models/image.rb:
class Image < ActiveRecord::Base
mount_uploader :image, ImageUploader
end
相关路线:
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>
对此的任何帮助将不胜感激,谢谢!
解决方案:
在 new.html.erb 中我不得不改变:
<%= form_for @asset, html: { multipart: true } do |f| %>
到
<%= form_for :asset, html: { multipart: true } do |f| %>
在这些更改之后,我的新操作现在可以正常工作,希望这对未来的人们有所帮助。
【问题讨论】: