我有这条路线:
resources :items, path: 'feed', only: [:index], defaults: { variant: :feed }
Run Code Online (Sandbox Code Playgroud)
它嵌套在api和v1名称空间中.(request_source参数来自Api命名空间).
我想在我的控制器规范中测试索引操作.我试过了:
get :feed, community_id: community.id, :request_source=>"api"
Run Code Online (Sandbox Code Playgroud)
不起作用,也是如此:
get :index, community_id: community.id, :request_source=>"api", variant: 'feed'
Run Code Online (Sandbox Code Playgroud)
他说:
ActionController::RoutingError:
No route matches {:community_id=>"14", :request_source=>"api", :variant=>"feed", :controller=>"api/v1/items"}
Run Code Online (Sandbox Code Playgroud)
- - - -编辑 - - - - -
我想使用变量将参数发送到控制器的原因是因为我有所有这些路径:
resources :items, path: 'feed', only: [:index], defaults: { variant: 'feed' }
resources :items, path: 'popular', only: [:index], defaults: { variant: 'popular' }
Run Code Online (Sandbox Code Playgroud)
然后,在ItemsController中,我有一个前置过滤器"get_items"用于索引操作:
def get_items
if params[:variant] == 'feed'
....
elsif params[:variant] == 'popular' …Run Code Online (Sandbox Code Playgroud) 从http上下文类是否有一种获取当前Controller名称的方法?
model-view-controller controller namespaces httpcontext asp.net-mvc-3
我的控制器出了问题
Ext.define('app.controller.myController', {
init: function() {
this.control({
'#myinputfield': {
change: this.textFieldChange(parameter)
}
})
},
textFieldChange: function(parameter) {
//do something
}
});
Run Code Online (Sandbox Code Playgroud)
它看起来像这样的问题是我在这里给出参数
change: this.textFieldChange(parameter)
Run Code Online (Sandbox Code Playgroud)
然后它在站点加载后点火,我不知道为什么.
没有参数它等待改变事件,它应该可以帮助我吗?
基本上,我试图在用户点击链接(或按钮或某种类型的交互元素)时执行rails方法.
我试着把它放在视图中:
<%= link_to_function 'Add' , function()%>
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用.它最终只是简单地调用该函数而没有用户甚至点击"添加"链接.
我也尝试使用link_to,但这也不起作用.我开始认为没有一种干净的方法可以做到这一点.无论如何,谢谢你的帮助.
PS.我在ApplicationController中定义了方法,它是一个帮助方法.
我目前正在使用一个函数来获取我的存储库:
public function getRepositories()
{
// Tasks
$tasks = $this->getDoctrine()
->getRepository('Model:Task');
// Task Info
$taskInfos = $this->getDoctrine()
->getRepository('Model:TaskInfo');
return array(
'tasks' => $tasks,
'taskInfos' => $taskInfos,
);
}
Run Code Online (Sandbox Code Playgroud)
所以在我的控制器动作中,我可以像这样调用一些SQL:
$repositories = $this->getRepositories();
$task = $repositories['tasks']->findOneById($id);
Run Code Online (Sandbox Code Playgroud)
但是现在我有一些控制器,我注意到我正在将相同的getRepositories()函数复制到每个控制器中,因为它重复复制代码.
我想要的是拥有一个帮助类,这可能是其中的一个功能.
但是我如何做到这一点,因为助手类不会是一个控制器,$this->getDoctrine()显然不会有效?
谢谢
我正在为我的Rails控制器编写一些测试,我正在使用RSpec 3.0.2
收到此错误后,当我尝试模拟POST对路由的调用时:
NoMethodError:
undefined method `post' for # <RSpec::ExampleGroups::WebApplicationsController::ProcessPayment:0x00000106214ce0>
Run Code Online (Sandbox Code Playgroud)
然后我读了他的SO线程,我在下面添加了以下代码spec_helper.rb
RSpec.configuration.infer_spec_type_from_file_location!
Run Code Online (Sandbox Code Playgroud)
我运行时出现以下错误rspec spec:
code/fsa/spec/spec_helper.rb:4:in `<top (required)>': undefined method `infer_spec_type_from_file_location!' for #<RSpec::Core::Configuration:0x0000010420e040> (NoMethodError)
Run Code Online (Sandbox Code Playgroud)
我不确定发生了什么事.
在这个小提琴http://jsfiddle.net/FlavorScape/fp1kktt9/我尝试在控制器上设置属性,而不是直接在$ scope.在模板中(在生产中)我们只做myAliasCtrl.somePropertyList和ng-repeat工作.
但是,这在测试中不起作用.我不知道如何在控制器上获取/分配属性.
更新: 我的测试环境中一定有一些奇怪的本地化问题,因为我确实让ng-repeat工作.注意有两个子元素,即使该属性在别名控制器上.http://jsfiddle.net/FlavorScape/2adn983y/2/
但是,我的问题仍然是,我如何获得对该编译控制器的引用来说,创建一个间谍?(或者只是获得别名控制器的引用)?
这是来源:
angular.module('myApp', [])
.directive('myTestDirective', function () {
return {
restrict: 'E',
scope: {
myBinding: '='
},
replace: true,
template: '<div ng-if="isRendered">TEST<div ng-repeat="foo in myCtrl.fooList">{{foo}}</div></div>',
controller: 'myTestController',
controllerAs: 'myCtrl'
};
})
.controller('myTestController', function($scope) {
$scope.isRendered = true;
// if i reference this, ng-repeat works
$scope.fooList = ["boob","cat", "Tesla"];
});
describe('myTest directive:', function () {
var scope, compile, validHTML;
validHTML = '<div><my-test-directive my-binding="isRendered"></my-test-directive></div>'; //i
beforeEach(module('myApp'));
beforeEach(inject(function($compile, $rootScope){
scope = $rootScope.$new();
scope.isRendered = …Run Code Online (Sandbox Code Playgroud) Ruby 1.9.3,RSpec 2.13.0,WebMock 1.17.4,Rails 3
我正在为公司应用编写测试.有问题的控制器显示客户已拨打电话的表格,并允许排序/过滤选项.
编辑测试失败,因为使用我当前的设置,路径不会呈现,因为recorder_server它未在本地运行,或者未正确设置.请帮忙.
A Errno::ECONNREFUSED occurred in recordings#index:
Connection refused - connect(2)
/usr/local/lib/ruby/1.9.1/net/http.rb:763:in `initialize'
-------------------------------
Request:
-------------------------------
* URL : http://www.recorder.example.com:8080/recorded_calls
* IP address: 127.0.0.1
* Parameters: {"controller"=>"recordings", "action"=>"index"}
* Rails root: /var/www/rails/<repository>
Run Code Online (Sandbox Code Playgroud)
到目前为止,这是我的规格.
require 'spec_helper'
include Helpers
feature 'Exercise recordings controller' do
include_context "shared admin context"
background do
canned_xml = File.open("spec/support/assets/canned_response.xml").read
stub_request(:post, "http://recorder.example.com:8080/recorder/index").
with(body: {"durations"=>["1"], "durations_greater_less"=>["gt"], "filter_from_day"=>"29", "filter_from_hour"=>"0", "filter_from_minute"=>"0", "filter_from_month"=>"12", "filter_from_year"=>"2014", …Run Code Online (Sandbox Code Playgroud) 有没有办法可以覆盖一个属性但仍然在Rails中使用强参数?比如在下面的例子中,如果我想每次都覆盖一个人的名字,还要使用该person_params方法.必须有比手动设置更好的方法@person.name,对吗?
class PeopleController < ActionController::Base
def create
@person = Person.new(person_params)
@person.name = "ABC"
@person.save
end
private
def person_params
params.require(:person).permit(:name, :age)
end
end
Run Code Online (Sandbox Code Playgroud) 如果有人问过这个道歉,但我似乎没有找到正确的答案.
我需要从Controller在DB中创建一个表.
我很天真,相信下面会有效:
Schema::connection('mysql')->create('mytable_'.$id, function($table)
{
$table->increments('id');
$table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)
$id传递给控制器函数的动态值在哪里.
我把下面放在了最上面:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
Run Code Online (Sandbox Code Playgroud)
没有运气 - 如何从控制器创建数据库表呢?有任何想法吗?
解决方案
正如Ben Swinburne在第一条评论中所说的那样,use Schema失踪了!