rspec 测试中没有路由匹配

发布于 2024-11-25 07:34:04 字数 1534 浏览 0 评论 0原文

我有以下测试:

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸印温柔 2024-12-02 07:34:04

该请求将进行更新操作,而不是创建,因此请仔细检查您的模板以及生成的表单及其 URL

The request goes on update action, not create, so double check your templates with generated forms, their URLs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文