我使用device和carrierwave gem得到以下错误:
undefined method `user_media_index_path'
.Showing .../user_medias/new.html.erb where line #3 raised:
我在user_media模型中添加了user_id的索引
我已经为单个模型成功实现了文件上传,但我不知道如何使用单独的模块进行文件上传。
new.html
form_for @media, :html =>{:multipart =>true} do |f|
Upload an Image f.file_field :image
f.submit
end
这是使用设备gem生成的用户模型:
user.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模型
user_media.rb
class UserMedia < ActiveRecord::Base
attr_accessible :anudio, :image, :video
belongs_to :user
mount_uploader :image, MediaUploader
end
当要求上传图像的创建操作时,它会被重定向到
user_medias_controller
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
路由详情如下:
的routes.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路由输出这一切
dashboard_index GET /dashboard/index(.:format) dashboard#index GET /dashboard(.:format) dashboard#index POST /dashboard(.:format) dashboard#create new_dashboard GET /dashboard/new(.:format) dashboard#new edit_dashboard GET /dashboard/:id/edit(.:format) dashboard#edit dashboard GET /dashboard/:id(.:format) dashboard#show PUT /dashboard/:id(.:format) dashboard#update DELETE /dashboard/:id(.:format) dashboard#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 DELETE /user_medias/:id(.:format) user_medias#destroy home_index GET /home/index(.:format) home#index new_user_session GET /users/sign_in(.:format) devise/sessions#new user_session POST /users/sign_in(.:format) devise/sessions#create destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy user_password POST /users/password(.:format) devise/passwords#create new_user_password GET /users/password/new(.:format) devise/passwords#new edit_user_password GET /users/password/edit(.:format) devise/passwords#edit PUT /users/password(.:format) devise/passwords#update cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel user_registration POST /users(.:format) devise/registrations#create new_user_registration GET /users/sign_up(.:format) devise/registrations#new edit_user_registration GET /users/edit(.:format) devise/registrations#edit PUT /users(.:format) devise/registrations#update DELETE /users(.:format) devise/registrations#destroy root / home#index uploder /uploder(.:format) user_medias#new
答案 0 :(得分:2)
错误指向路由文件中缺少的路由。将此添加到您的路线
resources :user_medias