【问题标题】:Rails: How do I upload Paperclip image attachment to a DB from an unrelated controller + Strong params requirementRails:如何将 Paperclip 图像附件从不相关的控制器上传到数据库 + 强参数要求
【发布时间】:2015-06-12 05:02:26
【问题描述】:

帮助请求

如何从不相关的控制器上传图片附件 + 强参数要求?

背景

我有一个控制器,Cars。我有两个型号CarGarage

Cars 控制器根据用户的车辆创建一个Garage 对象 属性 (users are from devise)。没有Garages 控制器因为Cars控制器创建了一个新的@garage对象。

Garage 模型是来自Car 模型和其他模型的属性 来自Cars 控制器的值将被存储。

Cars显示页面列出所有值

请知道此应用程序运行良好,因为它是 除了 我想让用户将他们车辆的照片 (paperclip gem) 上传到 garages 数据库的部分。

这个应用程序_supposed_functions的方式是......

  1. cars/1 该展示页面包含所有车辆属性的列表和一个按钮Browse... 和一个Add to Garage 按钮。
  2. 选择Browse... 按钮可让您附加车辆照片。这可以使用 paperclip gem 实现。
  3. 附加照片后,您可以选择Add to Garage 按钮。
  4. Cars 控制器创建一个具有多个车辆属性的Garage 对象,包括来自devise 方法、实例变量的一些属性 并将这些值放入garages 表中。

返回 NO 个错误。所有的值都输入到 garages db 除了 附加图像

以下是文件


汽车/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  

| |


请就以下问题提出建议:

  1. 如何从不相关的Cars 控制器将图像附件上传到garages db?除了image attachment 之外,所有内容都会添加到garages 数据库中。请记住,选择Add to Garage按钮时没有错误返回。这是一个强大的参数问题吗?

  2. Cars 控制器中是否需要强参数?如果是这样,我如何将数据库:keys 分配给实例@variables?我已经搜索了所有内容,但不清楚如何将实例变量分配给强参数的键。

    @garage = Garage.create(garage_params)
    params.permit(:tech_id, :customer_id )

  3. 如果可能,请提供您可能拥有的任何重构方法。

任何帮助将不胜感激。 如果您需要更多详细信息,请告诉我。

谢谢

【问题讨论】:

  • 虽然您在两种模型中都不需要 has_attached_file,但我想知道您使用什么 gem/code 来处理上传。从has_attached_file 方法来看,我的感觉是它是回形针。假设您使用的是 Paperclip 或类似的 gem,使用的是什么服务以及您是如何为您的存储配置它的?我的带有附加文件的模型明确声明 storage: :fog(我们使用 Rackspace Cloud Files 作为我们的 CDN)。
  • 嗨,克雷格,我正在使用 paperclip gem。我所有的迁移都已加载,我认为我的关联是正确的。我没有使用服务。我在笔记本电脑上本地搭建了一个开发环境。

标签: ruby-on-rails ruby devise paperclip strong-parameters


【解决方案1】:

您需要在强参数中包含garage_photo

汽车/cars_controller.rb

class CarsController < ApplicationController
  before_action :set_car

  def show
    @garage = Garage.find(params[:id])
  end

  def create    
    @garage = Garage.create(garage_params)
    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, :garage_photo )
  end
end

汽车/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.hidden_field :tech_id, value: @car.service.tech.id %>
  <%= f.hidden_field :customer_id, value: current_customer.id %>
  <%= 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) %>

【讨论】:

  • 嗨@Prashant4020 - 不幸的是,这不起作用。选择Add to Garage 按钮不会产生附加照片 的预期结果,它也不包括garages db 中的键值。我确实意识到一件事,tech.id 不是必需的,因为 Garage 模型 belongs_to :tech。请指教。
【解决方案2】:

我因为没有早点看到它而自责。对于具有文件上传字段的同一表单,您的视图没有提交按钮! 创建一个完全独立的表单,基本上将整个表单封装成一个按钮。因此,单击此按钮时,您在上面的表单中提供的任何值都不会被提交。相反,您应该创建一个标准的 类型按钮。

【讨论】:

  • 嗨 Patrick,我确实在 garages 数据库上运行了迁移。我熟悉添加多个模型。请查看我更新的帖子。
  • 嗨帕特里克 - 请告知如何将 Browse... 文件按钮封装在 Add to Garage 按钮内。我不熟悉这个过程。感谢您的回复。
  • @Prashant4020 是在正确的轨道上,只是没有包含表单的“提交”按钮。 ....
  • 嗨,Patrick,很抱歉,过去几天我一直在努力解决这个问题。我已经阅读了许多文章和资源,但无法达到预期的结果。我不熟悉submit 按钮,因为我的大多数应用程序主要使用link_to 助手。我可能在提交表单后关闭,Rails 询问来自garages 控制器的update 操作在哪里。没有garages 控制器,因为不需要此操作。还有没有办法避免使用隐藏字段?首选传递 id 的控制器方法。请指教
  • 我收到The action 'update' could not be found for GaragesControllergarages 控制器不应该有任何更新,因为这个文件不存在并且应该因为 cars 控制器创建一个新的 garage 对象并将属性插入到 garage 模型。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-29
  • 1970-01-01
  • 2011-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多