在我的rails应用程序中,我有两个步骤的客户表单。在第一步中,客户输入他的详细信息和徽标以及pdf,然后在下一步中选择他喜欢的主题。
my customers_controller
def new
if current_user.admin?
@customer = Customer.new
@customer.additional_fields.build
else
redirect_to root_path, notice: 'You dont have admin permissions'
end
end
def edit
if current_user.admin?
@customer = Customer.find(params[:id])
else
redirect_to root_path, notice: 'You dont have admin permissions'
end
end
def theme_selector
@customer_id = params[:customer_id]
puts @customer_id
end
def create
Rails.logger.debug "PARAMS:"
params.each do |k,v|
Rails.logger.debug "#{k}: #{v}"
end
if current_user.admin?
@customer = Customer.new(customer_params)
respond_to do |format|
if @customer.save
format.html do
redirect_to @customer, notice: 'Customer was successfully created.'
end
format.json { render :show, status: :created, location: @customer }
else
format.html { render :new }
format.json do
render json: @customer.errors, status: :unprocessable_entity
end
end
end
else
redirect_to root_path, notice: 'You dont have admin permissions'
end
end
_form.html.erb
<%= form_for @customer, url: theme_selector_path,:method => :post,html: { multipart: true } do |f| %>
<input type="hidden" name="customer_id" value="<%= @customer.id %>">
<% if @customer.errors.any? %>
<div id="error_explanation">
<h2>
<%= "#{pluralize(@customer.errors.count, "error")} prohibited this customer from being saved:" %>
</h2>
<ul>
<% @customer.errors.full_messages.each do |msg| %>
<li>
<%= msg %>
</li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class:"form-control" %>
</div>
<div class="form-group">
<%= f.label :address %>
<%= f.text_field :address, class:"form-control" %>
</div>
<div class="form-group">
<%= f.label :affiliate_id %>
<%= f.text_field :affiliate_id, class:"form-control" %>
</div>
<div class="control-group">
<%= f.label :logo, :class => 'control-label' %>
<div class="controls">
<%= f.file_field :logo,accept: 'image/png,image/gif,image/jpeg, image/jpg' %>
</div>
</div>
<div class="form-group">
<label>Upload File</label>
<%= f.file_field :file,accept: 'application/pdf' %>
</div>
<div class="form-group">
<% f.fields_for :additional_fields do | ing | %>
<%= ing.text_field :key, :size => 50 %>
<%= ing.text_field :value, :size => 50 %>
<% end %>
</div>
<div class="actions">
<div class="row">
<div class="col-md-5">
<span><%= f.submit 'Save', class: "btn-addmore" %></span>
<span><!--%= link_to 'Back', customers_path, class: "btn btn-danger" %--> </span>
</div>
<div class="col-md-2 col-md-offset-4">
<%= link_to "next", theme_selector_path, class: "btn-update" %>
</div>
</div>
</div>
<% end %>
我在theme_selector.html.erb上有一个提交按钮来创建客户。但是我收到了一个错误&#34; Paperclip :: AdapterRegistry :: NoHandlerError(找不到&#34的处理程序;#&#34; )&#34;在客户控制器中创建行动。
开发日志:
Processing by CustomersController#create as HTML
Parameters: {"customer"=>{"name"=>"test", "address"=>"test", "affiliate_id"=>"test", "category"=>"", "domain"=>"test@web.com", "phone"=>"1111111111", "contact"=>"1214521", "email"=>"test@malibu.com", "comments"=>"none", "logo"=>"# <ActionDispatch::Http::UploadedFile:0x007f3f4946c258>", "file"=>"#<ActionDispatch::Http::UploadedFile:0x007f3f4946c208>"}}
PARAMS:
customer: #<ActionController::Parameters:0x007f3f442c0058>
controller: customers
action: create
Completed 500 Internal Server Error in 10ms (ActiveRecord: 1.0ms)
Paperclip::AdapterRegistry::NoHandlerError (No handler found for "# <ActionDispatch::Http::UploadedFile:0x007f3f4946c258>"):
app/controllers/customers_controller.rb:47:in `create'
提前致谢!
客户表单的第1步包含客户详细信息,第2步包含主题选择。但是,我能够在步骤1中上传和保存数据,但是在保存步骤1中的数据时遇到错误第2步一起在theme_selector页面。
customer.rb
class Customer < ApplicationRecord
has_attached_file :logo
validates_attachment_content_type :logo, :content_type => /\Aimage\/.*\Z/
has_attached_file :file
validates_attachment_content_type :file, :content_type => "application/pdf"
end