首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >嵌套路由规范

嵌套路由规范
EN

Stack Overflow用户
提问于 2016-09-28 10:53:28
回答 1查看 33关注 0票数 0

我的路线是:

代码语言:javascript
复制
resources :posts
  resources :images
end

我想为ImagesController编写规范:

代码语言:javascript
复制
it "renders the index template" do
  get :index, {:id => @post.id}
  expect(response).to render_template("index")
end

错误是

代码语言:javascript
复制
No route matches {:action=>"index", :controller=>"images", :id=>"19"}

我该怎么解决这个问题?

编辑:

修正了post_id,但它似乎仍然没有得到参数:

新的错误是

代码语言:javascript
复制
Failure/Error: get :index, {post_id: @post.id}
     NoMethodError: undefined method `+' for nil:NilClass

索引视图:

代码语言:javascript
复制
  def index
    @post = Post.find(params[:post_id])
    @calculations = @post.something + 1
  end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-28 11:42:27

您应该传递post_id参数:

代码语言:javascript
复制
get :index, post_id: @post.id

编辑

在您用我们可以看到的正确信息更新了问题之后,您的代码中存在一些问题:

代码语言:javascript
复制
@post = Post.find(params[:post_id]) # should be Post.find(params[:id])
@calculations = @post.something + 1 # thus this is `nil` + 1 - error
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39745433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档