【发布时间】:2015-06-12 05:02:26
【问题描述】:
帮助请求
如何从不相关的控制器上传图片附件 + 强参数要求?
背景
我有一个控制器,
Cars。我有两个型号Car和Garage。
Cars控制器根据用户的车辆创建一个Garage对象 属性 (users are from devise)。没有Garages控制器因为Cars控制器创建了一个新的@garage对象。
Garage模型是来自Car模型和其他模型的属性 来自Cars控制器的值将被存储。
Cars显示页面列出所有值请知道此应用程序运行良好,因为它是 除了 我想让用户将他们车辆的照片 (
paperclip gem) 上传到garages数据库的部分。这个应用程序_supposed_functions的方式是......
- cars/1 该展示页面包含所有车辆属性的列表和一个按钮
Browse...和一个Add to Garage按钮。- 选择
Browse...按钮可让您附加车辆照片。这可以使用paperclipgem 实现。- 附加照片后,您可以选择
Add to Garage按钮。Cars控制器创建一个具有多个车辆属性的Garage对象,包括来自devise方法、实例变量的一些属性 并将这些值放入garages表中。返回 NO 个错误。所有的值都输入到
garagesdb 除了 附加图像。
以下是文件
汽车/cars_controller.rb
class CarsController < ApplicationController
before_action :set_car
def show
@garage = Garage.find(params[:id])
end
def create
@garage = Garage.create( tech_id: @car.service.tech.id, :customer_id: current_customer.id )
if @garage.save
redirect_to techs_path, notice: "You car has been added to the garage!"
elsif
redirect_to tech_path, notice: "Uh oh, flat tire!"
end
end
private
def set_car
@car = Car.find(params[:id])
end
def garage_params
params.permit(:tech_id, :customer_id )
end
end
模型
models/car.rb(不确定此模型中是否需要 has_attached_file,但还是添加了)
class Car < ActiveRecord::Base
belongs_to :service
belongs_to :tech #added 5/27
has_attached_file :garage_photo, styles: { medium: "300x300>", :thumb => "100x100>" }#, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :garage_photo, content_type: /\Aimage\/.*\Z/
end
models/garage.rb
class Garage < ActiveRecord::Base
belongs_to :customer
belongs_to :tech
has_attached_file :garage_photo, styles: { medium: "300x300>", :thumb => "100x100>" }#, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :garage_photo, content_type: /\Aimage\/.*\Z/
end
观看次数
cars/show.html.erb
<h2>Please review to add to garage </h2>
<b>Your garage tech:</b> <%= @car.service.tech.id %><br>
<b>ID:</b> <%= current_customer.id %><br>
<%= form_for @garage, :html => { :multipart => true } do |f| %>
<%= f.file_field :garage_photo %>
<% end %>
<%= button_to 'Add to Garage', tech_cars_path(tech_id: @car.service.tech.id, id: @car.id) %>
<%= link_to 'Back to tech', tech_path(@car.service.tech.id) %>
路线
Rails.application.routes.draw do
devise_for :customers, controllers: { sessions: 'customers/sessions' }, :path => ''
devise_for :techs, controllers: { sessions: 'techs/sessions' }
#, :path => ''#, :path_names => { :sign_in => "login", :sign_out => "logout" }
resources :techs, only: [:index, :show], shallow: true do
resources :cars, only: [:show, :create]
end
resources :service_menus, :services, :garages
root to: "home#index"
end
6-12 添加更多详情
_add_attachment_garage_photo_to_garages.rb 迁移文件
class AddAttachmentGaragePhotoToGarages < ActiveRecord::Migration
def change
change_table :garages do |t|
t.attachment :garage_photo
end
end
end
mysql> 描述车库 sn-p
| garage_photo_updated_at | datetime | YES | | NULL | |
| garage_photo_file_size | int(11) | YES | | NULL | |
| garage_photo_content_type | varchar(255) | YES | | NULL | |
| garage_photo_file_name | varchar(255) | YES | | NULL
| |
请就以下问题提出建议:
如何从不相关的
Cars控制器将图像附件上传到garagesdb?除了image attachment之外,所有内容都会添加到garages数据库中。请记住,选择Add to Garage按钮时没有错误返回。这是一个强大的参数问题吗?-
Cars控制器中是否需要强参数?如果是这样,我如何将数据库:keys分配给实例@variables?我已经搜索了所有内容,但不清楚如何将实例变量分配给强参数的键。@garage = Garage.create(garage_params)params.permit(:tech_id, :customer_id ) 如果可能,请提供您可能拥有的任何重构方法。
任何帮助将不胜感激。 如果您需要更多详细信息,请告诉我。
谢谢
【问题讨论】:
-
虽然您在两种模型中都不需要
has_attached_file,但我想知道您使用什么 gem/code 来处理上传。从has_attached_file方法来看,我的感觉是它是回形针。假设您使用的是 Paperclip 或类似的 gem,使用的是什么服务以及您是如何为您的存储配置它的?我的带有附加文件的模型明确声明storage: :fog(我们使用 Rackspace Cloud Files 作为我们的 CDN)。 -
嗨,克雷格,我正在使用
paperclipgem。我所有的迁移都已加载,我认为我的关联是正确的。我没有使用服务。我在笔记本电脑上本地搭建了一个开发环境。
标签: ruby-on-rails ruby devise paperclip strong-parameters