【问题标题】:How to set Rails routes on nested resources?如何在嵌套资源上设置 Rails 路由?
【发布时间】:2016-07-19 03:12:43
【问题描述】:

这似乎是多余的,因为herehere 已经提出了类似的问题,但我还没有找到解决方案。

我正在运行 RSpec 来测试 :update for api。当我运行 RSpec 时,它在我的第一次测试中显示 No Route Matches。我所需要的只是对未经身份验证的用户进行测试以 have_http_status(401) 。 Rails 无法确定路由。 这是错误的内容:

Failures:

  1) PostsController unauthenticated user PUT update returns http unauthenticated
     Failure/Error: put :update, topic_id: my_topic.id, post_id: my_post.id, post: {title: my_post.title, body: my_post.body}

     ActionController::UrlGenerationError:
       No route matches {:action=>"update", :controller=>"posts", :post=>{:title=>"Xdiwbu zuitsom prubmlhd oxmgtkb swphb ukije salhvk.", :body=>"Pjlb ywlzqv igdesqmw oqjgy mrwpye ujierxtn owqxbvt. Wzxu sjcikthg xare tcawzx tedmiqwf lewab. Twkeoun mos ophta fvae krmnsqe. Jxefyo ncd agj ieyanvt uehazwnk mtsi fbsm."}, :post_id=>"1", :topic_id=>"1"}
     # ./spec/api/v1/controllers/posts_controller_spec.rb:11:in `block (3 levels) in <top (required)>'

这里是 RSpec (spec/api/v1/controllers/posts_controller_spec.rb)

require 'rails_helper'

RSpec.describe Api::PostsController, type: :controller do
  let(:my_user) { create(:user) }
  let(:my_topic) { create(:topic) }
  let(:my_post) { create(:post, topic: my_topic, user: my_user) }

context "unauthenticated user" do

it "PUT update returns http unauthenticated" do
  put :update, topic_id: my_topic.id, post_id: my_post.id, post: {title: my_post.title, body: my_post.body}
  expect(response).to have_http_status(401)
end
...

路线如下:

  namespace :api do
    namespace :v1 do
      resources :users, only: [:index, :show, :create, :update]
      resources :topics, except: [:edit, :new] do
        resources :posts, only: [:update, :create, :destroy]
      end
    end
  end

这是测试的第一部分:

class Api::V1::PostsController < Api::V1::BaseController
  before_action :authenticate_user, except: [:index, :show]
  before_action :authorize_user, except: [:index, :show]

     def update
       post = Post.find(params[:id])

       if post.update_attributes(post_params)
         render json: post.to_json, status: 200
       else
         render json: {error: "Post update failed", status: 400}, status: 400
       end
     end
...

无论我如何更改 RSpec,我都无法让它与路线相匹配。你们介意帮忙吗?

谢谢!!

【问题讨论】:

  • 错误信息似乎与控制器规范不一致。你确定这两个都正确吗?
  • 你是对的。当我键入此内容时,我仍在对 RSpec 进行更改,并且我复制了错误的代码。我编辑了 RSpec 代码并添加了 topic_id: my_topic.id;无论如何,我仍然收到 No route ratches 错误。

标签: ruby-on-rails rspec routes nested


【解决方案1】:

有两件事让我印象深刻。

1:控制器本身的命名空间在Api::V1 下。然而,规范中的控制器被命名为 Api。这应该更新以匹配。

2:如果你运行rake routes,你会注意到这样一行:

PUT /api/v1/topics/:topic_id/posts/:id(.:format) api/v1/posts#update

请务必注意该消息中: 之后给出的名称。在这里,它声明主题的 ID 应该作为topic_id 提供给控制器,而帖子的 ID 应该作为 id 提供。如果您将put 语句修改为更像put :update, topic_id: my_topic.id, id: my_post.id, post: {title: my_post.title, body: my_post.body},它应该可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 2011-05-23
    • 1970-01-01
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多