我正在使用rails4。我添加了一个载波宝石。使用carrierwave时出错。我在票证模型中上传文件。 当我转到新票时,它会抛出一个错误未初始化的常量Ticket :: Asset。 门票控制器
class TicketsController < ApplicationController
before_action :require_signin!
before_action :set_project
before_action :set_ticket, only: [:show, :edit, :update, :destroy]
before_action :authorize_create!, only: [:new, :create]
before_action :authorize_update!, only: [:edit, :update]
before_action :authorize_delete!, only: :destroy
def new
@ticket = @project.tickets.build
3.times {@ticket.assets.build}
end
def edit
end
def create
@ticket = @project.tickets.build(ticket_params)
@ticket.user = current_user
if @ticket.save
flash[:notice] = "Ticket has been created."
redirect_to [@project, @ticket]
else
flash[:alert] = "Ticket has not been created."
render "new"
end
end
def update
if @ticket.update(ticket_params)
flash[:notice] = "Ticket has been updated."
redirect_to [@project, @ticket]
else
flash[:alert] = "Ticket has not been updated."
render action: "edit"
end
end
def destroy
@ticket.destroy
flash[:notice] = "Ticket has been deleted."
redirect_to @project
end
private
def ticket_params
params.require(:ticket).permit(:title, :description, assets_attributes:[:asset])
end
def set_project
@project =Project.find(params[:project_id])
end
def set_ticket
@ticket = @project.tickets.find(params[:id])
end
end
class Ticket < ActiveRecord::Base
has_many :assets
belongs_to :project
belongs_to :user
accepts_nested_attributes_for :assets
validates :title, :description, presence: true
end
资产模型
class Asset < ActiveRecord::Base
mount_uploader :asset, AssetUploader
belongs_to :ticket
end
路由
Ticketee::Application.routes.draw do
resources :users
resources :projects do
resources :tickets
end
答案 0 :(得分:0)
我错过了生成上传器文件。它解决了我的问题。