rspec 测试中没有路由匹配
我有以下测试:
it "should respond with a json object" do
xhr :post, :create, :upload => @attr_upload, :format => :json
end
这是我的路线:
resources :uploads, :except =>[:new, :show]
我有一个表单,用户可以使用 jquery-file-upload 插件上传图像:
<%= form_for @upload, :html => { :multipart => true } do |f| %>
<div class="fileupload-buttonbar">
<label class="fileinput-button">
<span>Add files... or drop them to upload</span>
<%= f.file_field :photo, :id => "upload_photo" %>
</label>
</div>
<% end %>
在我的控制器中,我加载文件并生成 json 响应:
def create
@upload = Upload.new(params[:upload])
respond_to do |format|
if @upload.save
set_current_upload(@upload)
format.json {render :json => [ @upload.to_jq_upload ].to_json}
else
format.json {render :json => [ @upload.to_jq_upload.merge({ :error => "custom_failure" }) ].to_json}
end
end
end
当我运行测试时,我是得到以下信息:
No route matches {:action=>"update", :controller=>"uploads", :id=>#<Upload id: nil, created_at: nil, updated_at: nil, photo_file_name: nil, photo_content_type: nil, photo_file_size: nil, photo_updated_at: nil, uploadable_id: nil, uploadable_type: nil>}
任何人都可以帮助我让这个测试工作吗? 我正在提交创建操作,那么为什么它要尝试执行更新操作?
任何帮助将不胜感激,我已经为此工作了两天。
I have the following test:
it "should respond with a json object" do
xhr :post, :create, :upload => @attr_upload, :format => :json
end
Here is my routes:
resources :uploads, :except =>[:new, :show]
I have a form that a user can upload images using the jquery-file-upload plugin:
<%= form_for @upload, :html => { :multipart => true } do |f| %>
<div class="fileupload-buttonbar">
<label class="fileinput-button">
<span>Add files... or drop them to upload</span>
<%= f.file_field :photo, :id => "upload_photo" %>
</label>
</div>
<% end %>
In my controller I load the file and generate a json response:
def create
@upload = Upload.new(params[:upload])
respond_to do |format|
if @upload.save
set_current_upload(@upload)
format.json {render :json => [ @upload.to_jq_upload ].to_json}
else
format.json {render :json => [ @upload.to_jq_upload.merge({ :error => "custom_failure" }) ].to_json}
end
end
end
When I run my test I am getting the following:
No route matches {:action=>"update", :controller=>"uploads", :id=>#<Upload id: nil, created_at: nil, updated_at: nil, photo_file_name: nil, photo_content_type: nil, photo_file_size: nil, photo_updated_at: nil, uploadable_id: nil, uploadable_type: nil>}
Can anyone help me get this test working?
I am submitting to the create action so why is it trying to go to the update action?
Any help would be appreciated I have been working on this for two days.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该请求将进行更新操作,而不是创建,因此请仔细检查您的模板以及生成的表单及其 URL
The request goes on update action, not create, so double check your templates with generated forms, their URLs