- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
当我尝试测试目标 Controller 时,出现 ActionController::UrlGenerationError: No route matches (:action => "edit", :controller => "goals") 错误
这是我的 goals_controller_test.rb
require 'test_helper'
class GoalsControllerTest < ActionController::TestCase
test "should be redirected when not logged in" do
get :new
assert_response :redirect
assert_redirected_to new_user_session_path
end
test "should render the new page when logged in" do
sign_in users(:guillermo)
get :new
assert_response :success
end
test "should get edit" do
get :edit
assert_response :success
end
test "should get show" do
get :show
assert_response :success
end
end
这是我的routes.rb
Rails.application.routes.draw do
devise_for :users
authenticated :user do
root 'du#dashboard', as: "authenticated_root"
end
resources :goals
root 'du#Home'
end
我的goals_controller.rb
class GoalsController < ApplicationController
before_filter :authenticate_user!, only: [:new]
def new
end
def edit
end
def show
end
private
def find_user
@user = User.find(params[:user_id])
end
def find_goal
@goal = Goal.find(params[:id])
end
end
我觉得很奇怪,如果我使用 get 'goals/edit' 而不是 resources :goals 测试通过。
非常感谢您提供任何指南。
最佳答案
当您使用 resources :goals
时,Rails 会为您生成以下路由(RESTful):
goals GET /goals(.:format) goals#index
POST /goals(.:format) goals#create
new_goal GET /goals/new(.:format) goals#new
edit_goal GET /goals/:id/edit(.:format) goals#edit
goal GET /goals/:id(.:format) goals#show
PATCH /goals/:id(.:format) goals#update
PUT /goals/:id(.:format) goals#update
DELETE /goals/:id(.:format) goals#destroy
如您所见,要点击 edit
操作 /goals/:id/edit
,您需要传递一个 :id
。这样,在您的 Controller 中,您将能够通过给定的 :id
=> Goal.find(params[:id])
找到记录。因此,在您的测试中,您需要传递此 :id
,类似于:
get :edit, id: 1 # mapping to /goals/1/edit
如果您手动添加此路由 get 'goals/edit'
,它会起作用,因为它直接映射到 /goals/edit
(注意没有 : id
).
顺便说一句,我建议您查看官方路由指南:http://guides.rubyonrails.org/routing.html
关于ruby-on-rails - ActionController::UrlGenerationError:尝试测试 Controller 时没有路由匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33377396/
我在玩 cucumber 时发现我的 Rspec 测试开始失败。在我安装 cucumber 之前一切都运行良好。有任何想法吗? 日志 /Users/szymonborucki/.rbenv/versi
我知道这已经被提出 a few times在SO上,但我无法取得进展。我在 Rails 5.0.7.2 上。 我收到 ActionController::InvalidCrossOriginReque
以下是由我的 Rails 应用程序中的表单引起的错误: Processing UsersController#update (for **ip** at 2010-07-29 10:52:27) [P
我有这个遗留的 Spree 应用程序,带有 2 个带有继承回调的装饰 Controller : Spree::Api::BaseController.class_eval do before_ac
我很好奇这一般意味着什么。 但这里是细节.. 我正在做一个可排序的 jquery 项目,涉及这个 rails Action : def update_order params[:media].ea
我收到来自 "button" %> 线路的无路由匹配错误. 在我添加 gem 之前,这曾经有效,但现在它停止工作了。我尝试过收藏,但我没有运气,因为那是以前的地方。 路线: resources :
我发现使用诸如 foo.bar.uk.com 之类的域会导致 Rails扔一个 ActionController::InvalidAuthenticityToken提交任何表格时。 foo.bar.c
我有一个使用 ActionController::Metal on Rails 4.1.6 的 Api Controller ,如下所示: class Api < ActionController::
在我的 ActionController::Live 监听大约 5 分钟后,我在浏览器中看到以下内容,并且我的 EventSource 断开了连接。 EventSource 的响应具有不是“text/
我正在使用 Rail4 并尝试通过 GET 请求发送一些纯文本: def get_text respond_to do |format| format.text { render :tex
我的道路有问题,我不明白他为什么告诉我“Dashboard::TasksController”因为在我的 Controller 文件中包含文件 dashbaord/tasks.rb class Tas
我的 Controller 页面有问题。顺便说一句,我想执行 localhost:3000/article/donat?author_id=4,这意味着我只想查看 author_id = 4 的文章我
我正在运行一个 rails 应用程序,我有一个简单的显示操作,其中的代码类似于以下内容: @post = Post.find(params[:id]) 所以如果你去帖子/1 例如,如果有帖子,您将看到
在 Rails 4.1.6 中,我收到 InvalidAuthenticityToken运行使用脚手架生成的测试时出错。有没有办法禁用特定测试的真实性 token 检查? rails g scaffo
我正在努力使 ActionController::UnknownFormat不会在生产中引发异常报告。我正在使用 Rails 4 并认为这样的事情可以解决问题,但似乎没有什么区别: applicati
我正在尝试使用 Rails 4 的实时流实现文本/事件流。它工作得很好,我遇到的唯一麻烦是我无法在不发送任何消息的情况下检查连接是否有效。 我想出的唯一解决方案是使用循环滴答发生器制作支持 chann
我已经阅读了所有可以找到的类似问题,但仍然无法弄清我的问题。 # routes.rb Rails.application.routes.draw do resources :lists, only
我在 application_contoller.rb 中有一个方法,其中包含对 send_file 的调用。我从 javascript 渲染的部分调用它,并收到以下错误 - ActionView::
所以...第一次向 StackOverflow 提问... 我按照 Rails Guides 和 RailsApps 项目将现有的 Rails 4.2.5 应用程序(使用 Ruby 2.2.4)转换为
我有一个关于 Rails ActionController::Live 的问题 最后我想向用户展示 FFMPEG 的进展,但现在我想让这个最小的示例运行: Rails media_controller
我是一名优秀的程序员,十分优秀!