我有一个名为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方法,列出所有书籍?
在我的控制器中,我有两个叫做"朋友"的动作.执行的那个取决于它是否是"获取"而不是"帖子".
所以我的代码片段看起来像这样:
// 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).我该怎么做呢?我是否只需要创建一个操作但区分其中的"获取"和"发布"?如果是这样,我该怎么做?
我需要在角度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) 在使用以下代码的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"?format是Collector类型.有没有办法获得表示格式的字符串?
我的控制器中有代码,如下所示:
[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时选择"邮报".
是的,它显然与它是一个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) 我想在视图中提供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为助手提供(和其他必要的方法)?
看看这个小提琴,我需要改变什么,模板中的表达式使用我在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)
我真的没有看透.感谢帮助!
我想为我的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)
有没有更好的方法呢?
模块路线:
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'将被调用两次.我错过了什么吗?
我的网站将有多个部分,每个部分我打算可以调整大小.为了实现这一点,我制定了一个"可调整大小"的指令,例如:
<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) controller ×10
angularjs ×4
scope ×2
asp.net-mvc ×1
backbone.js ×1
c# ×1
directive ×1
forms ×1
get ×1
html5 ×1
javascript ×1
post ×1
postman ×1
request-uri ×1
resize ×1
respond-to ×1
routes ×1
ruby ×1
view ×1
view-helpers ×1