标签: controller

在Rails 3中,当资源创建操作失败并调用render:new时,为什么URL必须更改为资源的索引URL?

我有一个名为Books的资源.它在我的路线文件中正确列为资源.

我有一个新动作,它给新视图标准:

@book = Book.new
Run Code Online (Sandbox Code Playgroud)

在模型上,有一些属性通过在线验证,因此如果保存操作失败,将生成错误.

在我的控制器中:

@book = Book.create
...  # some logic
if @book.save
  redirect_to(@book)
else
  render :new
end
Run Code Online (Sandbox Code Playgroud)

这是非常标准的; 以及使用render的基本原理:new是为了将对象传递回视图,并且可以报告错误,重新填充表单,等等.

这是有效的,除了每次我发送回表单(通过渲染:新),我的错误显示,但我的URL是INDEX URL,这是

/books
Run Code Online (Sandbox Code Playgroud)

而不是

/books/new
Run Code Online (Sandbox Code Playgroud)

这是我从一开始就开始的地方.我看过其他几个关于这个问题的帖子,但没有答案.至少,人们会认为它会落在/ books/create,我也有一个视图文件(在这种情况下与new相同).

我可以做这个:

# if the book isn't saved then
flash[:error] = "Errors!"
redirect_to new_book_path
Run Code Online (Sandbox Code Playgroud)

但随后@book数据以及错误消息将丢失,这是表单和操作等的全部要点.

为什么渲染:新的登陆我的/ books,我的索引动作,通常该URL调用INDEX方法,列出所有书籍?

forms controller routes ruby-on-rails view

57
推荐指数
2
解决办法
1万
查看次数

发布并获得相同的方法签名

在我的控制器中,我有两个叫做"朋友"的动作.执行的那个取决于它是否是"获取"而不是"帖子".

所以我的代码片段看起来像这样:

// Get:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Friends()
{
    // do some stuff
    return View();
}

// Post:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Friends()
{
    // do some stuff
    return View();
}
Run Code Online (Sandbox Code Playgroud)

但是,这不会编译,因为我有两个具有相同签名的方法(Friends).我该怎么做呢?我是否只需要创建一个操作但区分其中的"获取"和"发布"?如果是这样,我该怎么做?

asp.net-mvc post controller get asp-net-mvc-1

55
推荐指数
3
解决办法
3万
查看次数

如何从angularjs中的另一个控制器调用函数?

我需要在角度j的另一个控制器中调用函数.如果有可能,请提前帮助我

代码:

app.controller('One', ['$scope',
    function($scope) {
        $scope.parentmethod = function() {
            // task
        }
    }
]);
app.controller('two', ['$scope',
    function($scope) {
        $scope.childmethod = function() {
            // Here i want to call parentmethod of One controller
        }
    }
]);
Run Code Online (Sandbox Code Playgroud)

controller angularjs angularjs-scope

55
推荐指数
5
解决办法
15万
查看次数

如何判断控制器已解析为呈现的格式

在使用以下代码的rails控制器操作中:

respond_to do |format|
  format.json{ render :json=>  {:status => 200, :response=>@some_resource} }
  format.html { redirect_to(some_resource_path)}
end
Run Code Online (Sandbox Code Playgroud)

如何记录控制器将解析的格式,即"HTML"或"json"?formatCollector类型.有没有办法获得表示格式的字符串?

controller ruby-on-rails respond-to ruby-on-rails-3

49
推荐指数
2
解决办法
2万
查看次数

为什么"没有找到与请求URI匹配的HTTP资源"?

我的控制器中有代码,如下所示:

[Route("api/deliveryitems/InsertIntoPPTData/{stringifiedRecord}")]
Run Code Online (Sandbox Code Playgroud)

......我正在通过Postman这样称呼它:

    http://localhost:21609/api/deliveryitems/InsertIntoPPTData?
stringifiedRecord=serNum77;tx2;siteNum2;bla2.xml;ppt_user2;tx_memo2;file_beg2;file_end2
Run Code Online (Sandbox Code Playgroud)

......但得到:

{
Message: "No HTTP resource was found that matches the request URI     
'http://localhost:21609/api/deliveryitems/InsertIntoPPTData?   stringifiedRecord=serNum77;tx2;siteNum2;bla2.xml;ppt_user2;tx_memo2;file_beg2;file_end2'."
MessageDetail: "No type was found that matches the controller named 'deliveryitems'."
}
Run Code Online (Sandbox Code Playgroud)

以相同方式创建和调用的其他REST方法也很好 - 为什么不是这个?与其他人没有共同点的唯一事情是它是一个HttpPost,而其他的是HttpGet.这有什么不同吗?我正在从邮差下拉试图调用此方法REST时选择"邮报".

UPDATE

是的,它显然与它是一个Post并在URI中传递args无关,因为我现在使用HttpGet方法获得相同的东西:

{
Message: "No HTTP resource was found that matches the request URI     
'http://localhost:21609/api/department/getndeptsfromid?firstId=2&countToFetch=12'."
MessageDetail: "No action was found on the controller 'Department' that matches the request."
}
Run Code Online (Sandbox Code Playgroud)

我从邮递员这样称呼它:

http://localhost:21609/api/department/getndeptsfromid?firstId=2&countToFetch=12
Run Code Online (Sandbox Code Playgroud)

......它确实出现在我的控制器中:

[HttpGet]
[Route("api/department/getndeptsfromid/{firstId}/{countToFetch}")]
public List<Department> GetNDepartmentsFromID(int FirstId, int CountToFetch)
{
    return CCRService.GetNDepartmentsFromID(FirstId, …
Run Code Online (Sandbox Code Playgroud)

c# controller http-post-vars request-uri postman

49
推荐指数
4
解决办法
14万
查看次数

如何从ApplicationHelper调用ApplicationController方法

我想在视图中提供csv链接,我将csv生成代码放入ApplicationHelper.但是我收到了这个错误:

undefined method `send_data' for #<#<Class:0x0000010151c708>:0x0000010151a070>
Run Code Online (Sandbox Code Playgroud)

引用这个:

send_data content, :type => "text/plain",
  :filename => filename,
  :disposition => 'attachment'
Run Code Online (Sandbox Code Playgroud)

如果我将csv代码放在控制器中它工作正常.我希望使用帮助器来避免为每个我想为csv选项提供csv选项的控制器定义路由(我有一堆).如何send_data为助手提供(和其他必要的方法)?

ruby controller ruby-on-rails view-helpers ruby-on-rails-3

48
推荐指数
2
解决办法
3万
查看次数

AngularJS:如何将参数/函数传递给指令?

看看这个小提琴,我需要改变什么,模板中的表达式使用我在HTML中定义的参数进行评估?SAVE按钮应该调用blabla()控制器的-function,因为我通过它?

var myApp = angular.module('MyApp',[])
myApp.directive('editkeyvalue', function() {
    return {
        restrict: 'E',
        replace: true,
        scope: {
            accept: "expression"
        },
        template : '<div><label class="control-label">{{key}}</label>' +
        '<label class="control-label">{{key}}</label>' +
          '<input type="text" ng-model="value" />'+
        '<button type="button" x-ng-click="cancel()">CANCEL</button>' +
        '<button type="submit" x-ng-click="save()">SAVE</button></div>',

      controller: function($scope, $element, $attrs, $location) {
        $scope.save= function() {
          $scope.accept();
        };
      }
    }
});
Run Code Online (Sandbox Code Playgroud)

我真的没有看透.感谢帮助!

javascript html5 scope controller angularjs

46
推荐指数
2
解决办法
10万
查看次数

Backbone.js控制器中的默认路由?

我想为我的backbone.js控制器设置一个默认路由.目前我这样做:

class DealSearchController extends Backbone.Controller
    routes:
        'list' : 'showListView'
        'photos' : 'showPhotoView'
        'map' : 'showMapView'

    initialize: ->
        ....        
            window.location.hash = 'list' if ! _.include( _.keys(@routes),(window.location.hash || '').replace('#',''))
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法呢?

controller backbone.js

45
推荐指数
1
解决办法
2万
查看次数

AngularJs:使用$ routeProvider调用控制器两次

模块路线:

var switchModule = angular.module('switchModule', []);

switchModule.config(['$routeProvider', function($routeProvider) {
    $routeProvider.
    when('/build-content', {templateUrl: 'build-content.html', controller: BuildInfoController});
}]);
Run Code Online (Sandbox Code Playgroud)

控制器:

function BuildInfoController($http, $scope){
    alert("hello");
}
Run Code Online (Sandbox Code Playgroud)

HTML:

<html ng-app="switchModule">
...
<body>
    <ul>
        <li><a href="#build-content"/></a></li>
    </ul>
    <div class="ng-view"></div>
</body>
...
Run Code Online (Sandbox Code Playgroud)

每次当我点击超链接''时,'BuildInfoController'将被调用两次.我错过了什么吗?

controller angularjs

45
推荐指数
5
解决办法
4万
查看次数

Angular JS可调整大小的div指令

我的网站将有多个部分,每个部分我打算可以调整大小.为了实现这一点,我制定了一个"可调整大小"的指令,例如:

<div class="workspace" resize="full" ng-style="resizeStyle()">
<div class="leftcol" resize="left" ng-style="resizeStyle()">
Run Code Online (Sandbox Code Playgroud)

使用类似于以下内容的指令:

lwpApp.directive('resize', function ($window) {
    return {
        scope: {},

        link: function (scope, element, attrs) {
            scope.getWinDim = function () {
                return {
                    'height': window.height(),
                    'width': window.width()
                };
            };

            // Get window dimensions when they change and return new element dimensions
            // based on attribute
            scope.$watch(scope.getWinDim, function (newValue, oldValue) {
                scope.resizeStyle = function () {
                    switch (attrs.resize) {
                    case 'full':
                        return {
                            'height': newValue.height,
                            'width': (newValue.width - dashboardwidth)
                        };

                    case 'left':
                        return …
Run Code Online (Sandbox Code Playgroud)

scope resize controller directive angularjs

45
推荐指数
2
解决办法
7万
查看次数