Ruby on rails 使用带有Rails 3.1.3的回形针时,不会将任何文件添加到form POST上的Paramaters散列中
我有一个奇怪的问题,钢轨和回形针,我在我的智囊团试图解决它 我已经执行了将回形针附件添加到模型的所有步骤: 首先:db更新为“rails g回形针列表照片”,然后是rake db:migrate,模式看起来很棒Ruby on rails 使用带有Rails 3.1.3的回形针时,不会将任何文件添加到form POST上的Paramaters散列中,ruby-on-rails,forms,file,post,paperclip,Ruby On Rails,Forms,File,Post,Paperclip,我有一个奇怪的问题,钢轨和回形针,我在我的智囊团试图解决它 我已经执行了将回形针附件添加到模型的所有步骤: 首先:db更新为“rails g回形针列表照片”,然后是rake db:migrate,模式看起来很棒 create_table "listings", :force => true do |t| t.text "title" t.text "description" t.datetime "created_at" t.datetim
create_table "listings", :force => true do |t|
t.text "title"
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
t.string "photo_file_name"
t.string "photo_content_type"
t.integer "photo_file_size"
t.datetime "photo_updated_at"
end
然后:将'has_attached_file:photo'添加到listing.rb模型中(我已经尝试了我看到的通常建议的解决方案,在添加“attr_accessible”时添加了“photo”,但这对be不起作用,因为正如您在下面看到的,我的问题似乎不同)
class Listing < ActiveRecord::Base
has_attached_file :photo
has_many :comments
end
<%= form_for(@listing, :html => { :multipart => true }) do |f| %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_area :title %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :photo %><br />
<%= f.file_field :photo %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Started POST "/listings" for 127.0.0.1 at Thu Feb 16 16:14:58 -0400 2012
Processing by ListingsController#create as HTML
Parameters: {"commit"=>"Create Listing", "authenticity_token"=>"L6IH7NffyrCbTO0Me9rFb/M3nB2iHyy9BWfh/86H1lQ=", "utf8"=>"\342\234\223", "listing"=>{"title"=>"affefef", "description"=>"awefewfwefwef"}}
SQL (5.4ms) INSERT INTO "listings" ("created_at", "description", "photo_content_type", "photo_file_name", "photo_file_size", "photo_updated_at", "title", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?) [["created_at", Thu, 16 Feb 2012 20:14:58 UTC +00:00], ["description", "awefewfwefwef"], ["photo_content_type", nil], ["photo_file_name", nil], ["photo_file_size", nil], ["photo_updated_at", nil], ["title", "affefef"], ["updated_at", Thu, 16 Feb 2012 20:14:58 UTC +00:00]]
[paperclip] Saving attachments.
Redirected to http://localhost:3000/listings/17
Completed 302 Found in 15ms
class ListingsController < ApplicationController
# GET /listings
# GET /listings.json
def index
@listings = Listing.all
respond_to do |format|
format.html # index.html.erb
format.json { render :json => @listings }
end
end
# GET /listings/1
# GET /listings/1.json
def show
@listing = Listing.find(params[:id])
respond_to do |format|
format.all { render :json => @listing }
end
end
# GET /listings/new
# GET /listings/new.json
def new
@listing = Listing.new
respond_to do |format|
format.html # new.html.erb
format.json { render :json => @listing }
end
end
# GET /listings/1/edit
def edit
@listing = Listing.find(params[:id])
end
# POST /listings
# POST /listings.json
def create
@listing = Listing.new(params[:listing])
respond_to do |format|
if @listing.save
format.html { redirect_to @listing, :notice => 'Listing was successfully created.' }
format.json { render :json => @listing, :status => :created, :location => @listing }
else
format.html { render :action => "new" }
format.json { render :json => @listing.errors, :status => :unprocessable_entity }
end
end
end
# PUT /listings/1
# PUT /listings/1.json
def update
@listing = Listing.find(params[:id])
respond_to do |format|
if @listing.update_attributes(params[:listing])
format.html { redirect_to @listing, :notice => 'Listing was successfully updated.' }
format.json { head :ok }
else
format.html { render :action => "edit" }
format.json { render :json => @listing.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /listings/1
# DELETE /listings/1.json
def destroy
@listing = Listing.find(params[:id])
@listing.destroy
respond_to do |format|
format.html { redirect_to listings_url }
format.json { head :ok }
end
end
end
我已经进行了一些检查,试图解决这个问题,例如添加另一个直接链接到照片内容类型的文本区域,并且我能够创建一个直接分配给该属性的随机字符串列表,因此我知道我可以添加表单元素,而不仅仅是“title”和“description”位于上面日志的参数中
这个问题似乎和我的问题一样,因为我有完全相同的步骤和问题,除非我注意到来自那个帖子的日志至少在帖子参数中有这个文件。。。所以我相信这就是我的问题所在,我的帖子不知何故没有在参数中获得file_字段输入
Fwiw,我还创建了一个基本的测试项目,基本上是相同的(rails 3.1.3,paperclip 2.6.0,就我所知,所有配置都是相同的),它工作得很好,所以我知道我的环境至少适合paperclip
让我知道,如果有什么其他有用的我可以提供,我忘记了包括在内
谢谢
肖恩
更新:
清单u controller.rb:
class Listing < ActiveRecord::Base
has_attached_file :photo
has_many :comments
end
<%= form_for(@listing, :html => { :multipart => true }) do |f| %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_area :title %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :photo %><br />
<%= f.file_field :photo %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Started POST "/listings" for 127.0.0.1 at Thu Feb 16 16:14:58 -0400 2012
Processing by ListingsController#create as HTML
Parameters: {"commit"=>"Create Listing", "authenticity_token"=>"L6IH7NffyrCbTO0Me9rFb/M3nB2iHyy9BWfh/86H1lQ=", "utf8"=>"\342\234\223", "listing"=>{"title"=>"affefef", "description"=>"awefewfwefwef"}}
SQL (5.4ms) INSERT INTO "listings" ("created_at", "description", "photo_content_type", "photo_file_name", "photo_file_size", "photo_updated_at", "title", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?) [["created_at", Thu, 16 Feb 2012 20:14:58 UTC +00:00], ["description", "awefewfwefwef"], ["photo_content_type", nil], ["photo_file_name", nil], ["photo_file_size", nil], ["photo_updated_at", nil], ["title", "affefef"], ["updated_at", Thu, 16 Feb 2012 20:14:58 UTC +00:00]]
[paperclip] Saving attachments.
Redirected to http://localhost:3000/listings/17
Completed 302 Found in 15ms
class ListingsController < ApplicationController
# GET /listings
# GET /listings.json
def index
@listings = Listing.all
respond_to do |format|
format.html # index.html.erb
format.json { render :json => @listings }
end
end
# GET /listings/1
# GET /listings/1.json
def show
@listing = Listing.find(params[:id])
respond_to do |format|
format.all { render :json => @listing }
end
end
# GET /listings/new
# GET /listings/new.json
def new
@listing = Listing.new
respond_to do |format|
format.html # new.html.erb
format.json { render :json => @listing }
end
end
# GET /listings/1/edit
def edit
@listing = Listing.find(params[:id])
end
# POST /listings
# POST /listings.json
def create
@listing = Listing.new(params[:listing])
respond_to do |format|
if @listing.save
format.html { redirect_to @listing, :notice => 'Listing was successfully created.' }
format.json { render :json => @listing, :status => :created, :location => @listing }
else
format.html { render :action => "new" }
format.json { render :json => @listing.errors, :status => :unprocessable_entity }
end
end
end
# PUT /listings/1
# PUT /listings/1.json
def update
@listing = Listing.find(params[:id])
respond_to do |format|
if @listing.update_attributes(params[:listing])
format.html { redirect_to @listing, :notice => 'Listing was successfully updated.' }
format.json { head :ok }
else
format.html { render :action => "edit" }
format.json { render :json => @listing.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /listings/1
# DELETE /listings/1.json
def destroy
@listing = Listing.find(params[:id])
@listing.destroy
respond_to do |format|
format.html { redirect_to listings_url }
format.json { head :ok }
end
end
end
鉴于此:
上传文件的参数不属于params数组的一种情况可能是因为您使用的是类似JQuery Mobile的组件,该组件通过AJAX发布文件数据。
您可以使用internet浏览器开发人员工具验证上传数据是否通过AJAX发送(firebug、ie开发工具、chrome inspect元素等工具)
在您的视图中,添加它,而不是:html=>{:multipart=>true}(在模型生成的表单中,默认情况下它已经是multipart=true)
(@model,html:{data:{ajax:false}})的表单_
希望有一天能帮助别人!
祝你好运
Yohann上传文件的参数不属于params数组的一种情况可能是因为您使用的是类似JQuery Mobile的组件,该组件通过AJAX发布文件数据。 您可以使用internet浏览器开发人员工具验证上传数据是否通过AJAX发送(firebug、ie开发工具、chrome inspect元素等工具) 在您的视图中,添加它,而不是:html=>{:multipart=>true}(在模型生成的表单中,默认情况下它已经是multipart=true) (@model,html:{data:{ajax:false}})的表单_ 希望有一天能帮助别人! 祝你好运
Yohann请为您的剪纸模型添加代码。请显示您的控制器代码好吗?@ISOTOX使用列表更新了\u controller.rb我的大部分代码非常少,如果有的话,从脚手架生成的代码中删除了。更新:我开始查看工作版本和非工作版本的HTTP请求,而且似乎是坏版本上的某些东西导致浏览器将“坏”版本表单解释为application/x-www-form-urlencoded,而不是多部分/表单数据。我已经为每个应用程序添加了rails生成的html,以及每个应用程序的HTTP请求信息。请为您的剪纸模型添加代码。请显示您的控制器代码好吗?@同位素更新了清单。\u controller.rb我的大部分代码非常少,如果有的话,从脚手架生成的内容中删除。更新:我开始查看工作版本和非工作版本的HTTP请求,似乎是坏版本上的某些内容导致浏览器将“坏”版本表单解释为application/x-www-form-urlencoded,而不是multipart/form数据。我为每个应用程序添加了rails生成的html,以及每个应用程序的HTTP请求信息。