我有一点时间搞清楚这一点......我基本上是想跟踪一个用于将画廊图像添加到页面的railscast,但在这种情况下,场景如下:
我有一个项目模型和一个BodyImages模型。 bodyimages模型使用载波上传器将图像文件附加到每个新创建的“bodyimage”。以管理员身份登录并查看“项目显示”页面时...我的目标是允许我的管理员用户直接从此“项目显示”页面中的div创建BodyImage(并上传关联的载波图像)。这些BodyImages应显示在上传按钮所在的位置(也在此项目显示页面上)。这样做两个模型可能是荒谬的,但我只是希望能够解决这个问题,因为我整晚都在撞墙。当我上传图片并单击“创建bodyimage”按钮时,我目前收到此错误消息:
Routing Error
uninitialized constant BodyimagesController
*它重定向到我的URL是localhost:3000 / bodyimages由于某种原因(我认为我将代码重定向到项目页面)
非常感谢您提前寻求帮助:
的routes.rb
devise_for :users
resources :projects
resources :contacts, only: [:new, :create]
resources :bodyimages, only: [:new, :create]
get 'home/index'
root 'home#index'
get '*path' => redirect('/')
VIEWS / PROJECTS / SHOW.HTML.ERB(管理员将上传照片并将其显示在任何人的同一页面上)
<div class="gallery_admin_add_images">
<%= form_for Bodyimage.new do |f| %>
<%= f.hidden_field :project_id %>
<p>
<%= f.file_field :image %>
</p>
<p><%= f.submit %></p>
<% end %>
<div class="gallery_public_view_images">
<% for bodyimage in @project.bodyimages %>
<div class="col-md-6">
<%= image_tag bodyimage.image_url.to_s %>
</div>
<% end %>
</div>
MODELS / PROJECT.RB
has_many :categorizations
has_many :categories, through: :categorizations
has_many :bodyimages, dependent: :destroy
end
MODELS / BODYIMAGE.RB
class Bodyimage < ActiveRecord::Base
belongs_to :project
mount_uploader :image, ImageUploader
end
控制器/ PROJECTS_CONTROLLER.RB
class ProjectsController < ApplicationController
before_action :find_project, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
@projects = Project.order('created_at DESC').paginate(page: params[:page], :per_page => 5)
end
def new
@project = Project.new
end
def create
@project = Project.new project_params
if @project.save
redirect_to @project, notice: "your projects were successfully updated!"
else
render 'new', notice: "your projects could not be updated."
end
end
def show
end
def edit
end
def update
if @project.update project_params
redirect_to @project, notice: "your projects were successfully updated!"
else
render 'edit'
end
end
def destroy
@project.destroy
redirect_to projects_path
end
private
def project_params
params.require(:project).permit(:title, :description, :link, :coverimage, :slug, :remote_coverimage_url, category_ids: [])
end
def find_project
@project = Project.friendly.find(params[:id])
end
end
控制器/ BODY_IMAGES_CONTROLLER.RB
class BodyImagesController < ApplicationController
def new
@bodyimage = @project.bodyimages.build(project_id: current_project.id)
end
def create
@project = Project.find(params[:project_id])
@bodyimage = @project.bodyimages.build(bodyimage_params)
if @bodyimage.save
flash[:notice] = "Successfully added bodyimage."
redirect_to project_path(@project)
else
render :action => 'new'
end
end
def index
@bodyimages = Bodyimage.all, params[:project_id]).paginate(page: params[:page]).per_page(36)
end
private
def find_project
@project = Project.find(params[:project_id])
end
def bodyimage_params
params.require(:bodyimage).permit(:project_id, :image)
end
end
最后,我认为上传者文件没有问题,但我也会将其包括在内
上传者/ IMAGE_UPLOADER.RB
# encoding: utf-8
class ImageUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
# storage :file if development & fog if in production
if Rails.env.production?
storage :fog
else
storage :file
end
include CarrierWave::MimeTypes
process :set_content_type
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
# # For Rails 3.1+ asset pipeline compatibility:
# # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
#
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
# version :thumb do
# process :resize_to_fit => [50, 50]
# end
version :thumb do
process :resize_to_limit => [200, 200]
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
# def extension_white_list
# %w(jpg jpeg gif png)
# end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
end
答案 0 :(得分:2)
路由错误未初始化的常量BodyimagesController
您的 模型 名称为Bodyimage
,因此控制器名称应为Bodyimagescontroller
。但你有BodyImagesController
。
将控制器文件名更改为bodyimages_controller.rb
,将班级名称更改为BodyimagesController
可以解决您的问题
<强> 更新 强>
BodyimagesController中的ActiveRecord :: RecordNotFound #create无法 找到项目'id'=
错误是由于这行redirect_to project_path(@project),这里@project是nil,因为你没有将值传递给hidden_field project_id
将<%= f.hidden_field :project_id %>
更改为<%= f.hidden_field :project_id, @project.id %>