I missed to generate the uploader file. It solved my problem.
(Rails4, Carrierwave gem )uninitialized constant Ticket::Asset
-
17-07-2023 - |
题
I am using rails4. I added a carrierwave gem. There is an error using carrierwave. I am uploading file in ticket model. When I go to new ticket it throws a error-uninitialized constant Ticket::Asset. tickets controller
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
asset model
class Asset < ActiveRecord::Base
mount_uploader :asset, AssetUploader
belongs_to :ticket
end
routes
Ticketee::Application.routes.draw do
resources :users
resources :projects do
resources :tickets
end
解决方案
不隶属于 StackOverflow