使用 device 和 carrierwave gem 时出现以下错误:
undefined method `user_media_index_path'
.Showing .../user_medias/new.html.erb where line #3 raised:
我在 user_media 模型中添加了 user_id 索引
我已经成功地为单个模型实现了文件上传,但我不知道如何使用单独的模块来实现。
新的.html
form_for @media, :html =>{:multipart =>true} do |f|
Upload an Image f.file_field :image
f.submit
end
这是使用设备 gem 生成的用户模型:
用户.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me
has_many :user_media, dependent: :destroy
end
它是存储用户媒体(如图像等)的模型;我现在仅将其用于图像,但为了将来添加更多类型的媒体,我创建了此 user_media 模型
用户媒体.rb
class UserMedia < ActiveRecord::Base
attr_accessible :anudio, :image, :video
belongs_to :user
mount_uploader :image, MediaUploader
end
当要求创建上传图像的操作时,它会被重定向到这里
用户媒体控制器
class UserMediasController < ApplicationController
def new
@media = UserMedia.new
end
def create
@media=current_user.user_media.build(params[:media])
if @media.save
render'index'
else
render'new'
end
end
end
路线详情如下:
路线.rb
Projectx::Application.routes.draw do
get "dashboard/index"
resources :dashboard, :UserMedias
get "home/index"
devise_for :users
root :to => 'home#index'
match 'uploder' =>'UserMedias#new'
添加@peter建议的资源后,rake routes 会输出这一切
dashboard_index GET /dashboard/index(.:format) 仪表盘#index 获取 /dashboard(.:format) 仪表板#index POST /dashboard(.:format) 仪表板#create new_dashboard GET /dashboard/new(.:format) 仪表板#new edit_dashboard GET /dashboard/:id/edit(.:format) 仪表板#edit 仪表板 GET /dashboard/:id(.:format) 仪表板#show PUT /dashboard/:id(.:format) 仪表板#update 删除 /dashboard/:id(.:format) 仪表板#destroy user_medias GET /user_medias(.:format) user_medias#index POST /user_medias(.:format) user_medias#create new_user_media GET /user_medias/new(.:format) user_medias#new edit_user_media GET /user_medias/:id/edit(.:format) user_medias#edit user_media GET /user_medias/:id(.:format) user_medias#show PUT /user_medias/:id(.:format) user_medias#update 删除 /user_medias/:id(.:format) user_medias#destroy home_index GET /home/index(.:format) home#index new_user_session GET /users/sign_in(.:format) 设计/会话#new user_session POST /users/sign_in(.:format) 设计/会话#create destroy_user_session 删除 /users/sign_out(.:format) 设计/会话#destroy user_password POST /users/password(.:format) 设计/密码#create new_user_password GET /users/password/new(.:format) 设计/密码#new edit_user_password GET /users/password/edit(.:format) 设计/密码#edit PUT /users/password(.:format) 设计/密码#update cancel_user_registration GET /users/cancel(.:format) 设计/注册#cancel user_registration POST /users(.:format) 设计/注册#create new_user_registration GET /users/sign_up(.:format) 设计/注册#new edit_user_registration GET /users/edit(.:format) 设计/注册#edit PUT /users(.:format) 设计/注册#update 删除 /users(.:format) 设计/注册#destroy 根/主页#index 上传者 /uploder(.:format) user_medias#new