In Rails 4 在第二个模型的显示页面上从一个模型创建图像
In Rails 4 Create Images from one Model on the Show Page of second Model
我费了好大劲才弄清楚这个问题...我基本上是在尝试按照 railscast 将图库图像添加到页面,但在这种情况下,情况如下:
我有一个 Projects 模型和一个 BodyImages 模型。 bodyimages 模型使用载波上传器将图像文件附加到每个新创建的 "bodyimage"。以管理员身份登录并查看项目展示页面时...我的目标是允许我的管理员用户直接从该项目展示页面中的 div 创建 BodyImage(并上传相关的载波图像)。这些 BodyImages 应该显示在上传按钮所在的下方(也在这个项目显示页面上)。作为两个模型这样做可能很荒谬,但我只是希望弄清楚这一点,因为我整晚都在用头撞墙。我目前在上传图片并单击 'create 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
CONTROLLERS/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
CONTROLLERS/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
最后,我认为上传文件没有问题,但我也会将其包括在内
UPLOADERS/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
Routing Error uninitialized constant BodyimagesController
您的模型名称是Bodyimage
,所以控制器名称应该是Bodyimagescontroller
。但是你有 BodyImagesController
.
将您的控制器文件名更改为 bodyimages_controller.rb
并将 class 名称更改为 BodyimagesController
应该可以解决您的问题
更新
ActiveRecord::RecordNotFound in BodyimagesController#create Couldn't
find Project with 'id'=
错误是由于这一行 redirect_to project_path(@project),这里 @project 是 nil 因为你没有将值传递给 hidden_field project_id
将 <%= f.hidden_field :project_id %>
更改为 <%= f.hidden_field :project_id, @project.id %>
我费了好大劲才弄清楚这个问题...我基本上是在尝试按照 railscast 将图库图像添加到页面,但在这种情况下,情况如下:
我有一个 Projects 模型和一个 BodyImages 模型。 bodyimages 模型使用载波上传器将图像文件附加到每个新创建的 "bodyimage"。以管理员身份登录并查看项目展示页面时...我的目标是允许我的管理员用户直接从该项目展示页面中的 div 创建 BodyImage(并上传相关的载波图像)。这些 BodyImages 应该显示在上传按钮所在的下方(也在这个项目显示页面上)。作为两个模型这样做可能很荒谬,但我只是希望弄清楚这一点,因为我整晚都在用头撞墙。我目前在上传图片并单击 'create 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
CONTROLLERS/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
CONTROLLERS/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
最后,我认为上传文件没有问题,但我也会将其包括在内
UPLOADERS/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
Routing Error uninitialized constant BodyimagesController
您的模型名称是Bodyimage
,所以控制器名称应该是Bodyimagescontroller
。但是你有 BodyImagesController
.
将您的控制器文件名更改为 bodyimages_controller.rb
并将 class 名称更改为 BodyimagesController
应该可以解决您的问题
更新
ActiveRecord::RecordNotFound in BodyimagesController#create Couldn't find Project with 'id'=
错误是由于这一行 redirect_to project_path(@project),这里 @project 是 nil 因为你没有将值传递给 hidden_field project_id
将 <%= f.hidden_field :project_id %>
更改为 <%= f.hidden_field :project_id, @project.id %>