如果我从创建操作内部渲染更新操作,是否有办法让"更新"视图(即update.html.erb)知道哪个操作呈现了它.我希望更新视图在创建操作呈现时打印出操作名称"create",并在更新操作呈现时打印出"update"一词.问题是呈现似乎将控制推迟到被调用的操作,因此对于所有意图和目的,更新视图始终认为它来自更新操作.
class CtrlController < ApplicationController
def create
render(:action=>"update")
end
def read
end
def update
end
def delete
end
Run Code Online (Sandbox Code Playgroud) 我有一个控制器动作检查
this.User.Identity.IsAuthenticated
Run Code Online (Sandbox Code Playgroud)
你有什么建议如何处理这种行为的单元测试?
我最近开始阅读有关ASP.net MVC的内容,在对这个概念感到兴奋之后,我开始将我的所有webform项目迁移到MVC,但即使遵循了所有好的建议,我也很难保持我的控制器瘦身(或者也许我只是不明白......).我处理的网站有文章,视频,报价......每个实体都有类别,评论,可以与之关联的图像.我使用Linq来sql进行数据库操作,对于每个实体,我有一个Repository,对于每个存储库,我创建一个要在控制器中使用的服务.
所以我有 -
和相应的服务
你看到了这张照片.
我的问题是我有一个文章,类别和注释的控制器,因为我认为有ArticleController处理所有这可能是有道理的,但现在我必须传递Controller构造函数所需的所有服务.所以我想知道我做错了什么.我的服务设计不当吗?我应该创建更大的服务来封装更小的服务并在我的控制器中使用它们吗?或者我应该有articleCategory控制器和articleComment控制器?
用户查看的页面由所有这些组成,您要查看的文章,与之关联的评论,应用的女巫类别列表......如何有效地分解控制器以使其"保持" "解决我的头痛?
谢谢!我希望我的问题不会太长,无法阅读......
asp.net-mvc service design-patterns controller repository-pattern
实现编辑操作时,我为Get和Post添加了两个方法:Edit(string id)
理想情况下,他们需要具有相同的签名.但当然这不可编辑.所以我在HttpPost方法中添加了一个伪参数(在我的例子中为form):
[HttpGet]
public ActionResult Edit(string id)
{
var user = Entities.Users.SingleOrDefault(s => s.UserID == id);
return View(user);
}
[HttpPost]
public ActionResult Edit(string id, FormCollection form)
{
var user = Entities.Users.SingleOrDefault(s => s.UserID == id);
if (TryUpdateModel<User>(user, new[] { "Email", "FullName" }))
{
Entities.SaveChanges();
RedirectToAction("Index");
}
return View(user);
}
Run Code Online (Sandbox Code Playgroud)
任何更好/更清洁的方式来实现编辑操作?
我正在使用MVC PHP框架Codeigniter,我有一个关于从哪里调用redirect()的直接问题:Controller或Model?
场景:
用户导航到www.example.com/item/555.在我的模型中,我在项目数据库中搜索ID为555的项目.如果找到该项目,我会将结果返回给我的控制器.但是,如果找不到某个项目,我想将用户重定向到某个地方.这个对redirect()的调用是来自模型内部还是控制器?为什么?
(这个问题与Controller中的Ruby on Rails方法模拟类似,但是它使用的是旧stub语法,而且没有得到正确的答案.)
我想测试我的控制器代码与我的模型代码分开.不应该是rspec代码:
expect(real_time_device).to receive(:sync_readings)
Run Code Online (Sandbox Code Playgroud)
验证是否RealTimeDevice#sync_readings被调用,但是禁止实际调用?
我的控制器有一个#refresh方法调用RealTimeDevice#sync_readings:
# app/controllers/real_time_devices_controller.rb
class RealTimeDevicesController < ApplicationController
before_action :set_real_time_device, only: [:show, :refresh]
<snip>
def refresh
@real_time_device.sync_readings
redirect_to :back
end
<snip>
end
Run Code Online (Sandbox Code Playgroud)
在我的控制器测试中,我想验证(a)是否正在设置@real_time_device并且(b)调用#sync_reading模型方法(但我不想调用模型方法本身,因为它已被模型单元测试).
这是我的controller_spec代码不起作用:
# file: spec/controllers/real_time_devices_controller_spec.rb
require 'rails_helper'
<snip>
describe "PUT refresh" do
it "assigns the requested real_time_device as @real_time_device" do
real_time_device = RealTimeDevice.create! valid_attributes
expect(real_time_device).to receive(:sync_readings)
put :refresh, {:id => real_time_device.to_param}, valid_session
expect(assigns(:real_time_device)).to eq(real_time_device)
end
end
<snip>
Run Code Online (Sandbox Code Playgroud)
当我运行测试时,实际的RealTimeDevice#sync_readings方法被调用,即它试图在我的模型中调用代码.我认为这句话:
expect(real_time_device).to receive(:sync_readings) …Run Code Online (Sandbox Code Playgroud) 我不确定我是否正确接近这个但我正在建立一个电子商务网站 - 该网站的一部分有6个不同的产品网格页面,每个网页都可以使用相同的视图:
<ul class="products row">
<li class="product thumbnail col-1 grid" ng-repeat="whisky in whiskies | orderBy: sort">
<p class="picture">
<a ng-href="#/json/{{whisky.id}}"><img ng-src="images/scotch/{{whisky.imageUrl}}" alt="{{whisky.name}}"/></a>
</p>
<div class="desc">
<h2>{{whisky.name}}</h2>
<p class="price"><span>£{{whisky.price}}</span></p>
</div>
<div class="actions">
<button class="add-to-basket btn btn-primary btn-medium fa fa-shopping-cart" data-item='{"imageUrl" : "{{whisky.imageUrl}}", "price" : "{{whisky.price}}", "startPrice" : "{{whisky.price}}", "name" : "{{whisky.name}}", "totalItems" : "{{1}}"}' ng-click="updateMiniBasket($event)"></button>
</div>
</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
和相同的控制器:
whiskyControllers.controller('whiskyListCtrlr', ['$scope', 'whiskyList', '$http',
function($scope, whiskyList, $http){
whiskyList.getWhiskies().then(function(d) {
$scope.whiskies = d.data;
})
}
])
Run Code Online (Sandbox Code Playgroud)
但需要在路线提供者配置中有不同的路线,即一个去苏格兰威士忌,一个去爱尔兰,一个去日本等.
如何对路由进行编码,以便不同的页面共享同一个控制器和视图?是否可以将参数从路由器传递到控制器?
非常感谢
我正在使用Slim框架创建API.目前我使用单个文件来创建路由并向其传递闭包:
$app->get('/', function($req, $resp){
//Code...
})
Run Code Online (Sandbox Code Playgroud)
但我意识到我的文件发展迅速.我想要做的是使用控制器,所以我将有一个控制器类,只需将实例/静态方法传递给路由,如下所示
class HomeController
{
public static function index($req, $resp){}
}
Run Code Online (Sandbox Code Playgroud)
然后将函数传递给路由
$app->get('/', HomeController::index);
Run Code Online (Sandbox Code Playgroud)
我尝试了这个,但它不起作用,我想知道是否有一种方法可以用它来管理我的文件.
这个问题是不是重复这个,因为所有的细节都在这个问题充分说明,将是非常适合初学者
我有一个从模型继承的视图。我的字段是表单,我想用JQuery / Ajax将表单(或模型)发布到控制器并将其插入数据库中。我阅读了这篇文章,可以使用Ajax上传图像,但是我无法将整个表格或带有数据的模型发布到控制器。
我的看法 :
@model ajax1.Models.Info
<form method="post" enctype="multipart/form-data">
<div class="form-group">
<label asp-for="FirstName" class="col-lg-2 col-sm-2 control-label "></label>
<div class="col-lg-6">
<input asp-for="FirstName" class="form-control" />
<span asp-validation-for="FirstName"></span>
</div>
</div>
<div class="form-group">
<label asp-for="LastName" class="col-lg-2 col-sm-2 control-label"></label>
<div class="col-lg-6">
<input asp-for="LastName" class="form-control" />
<span asp-validation-for="LastName"></span>
</div>
</div>
<input type="file" id="files" name="files" multiple />
<input type="button" id="upload" value="Upload Selected Files" />
</form>
Run Code Online (Sandbox Code Playgroud)
视图底部的JQuery代码:
@section Scripts{
<script>
$("#upload").click(function (evt) {
var fileupload = $("#files").get(0);
var files = fileupload.files;
var …Run Code Online (Sandbox Code Playgroud) 我正在使用Rails 5,并且有一个带有Google Recaptcha的表单。我为此使用Rails宝石
gem "recaptcha", require: "recaptcha/rails"
Run Code Online (Sandbox Code Playgroud)
所以我有一个简单的表格
<%= form_for @comment, :html => {:class => "commentsForm"} do |f| %>
<div class="field">
<%= f.label :description, 'Your Comments' %><br>
<%= f.text_area :description %>
</div>
<%= recaptcha_tags %>
<div class="actions">
<%= submit_tag 'Submit', :class => 'btn-feedback' %>
</div>
<% end %>
Run Code Online (Sandbox Code Playgroud)
而我要处理的控制器是
def create
@comment = Comment.new(params[:comment].permit(:description))
@comment.user = current_user
if verify_recaptcha(model: @comment)
Run Code Online (Sandbox Code Playgroud)
但是我该如何编写单元测试来测试控制器方法呢?我不知道如何“伪造” recaptcha提交。这就是我所拥有的
test "do submit comment" do
post comments_url, params: { comment: { description: "Some comments"} }
# Verify we …Run Code Online (Sandbox Code Playgroud) controller ×10
asp.net-mvc ×3
unit-testing ×3
model ×2
action ×1
ajax ×1
angularjs ×1
asp.net ×1
codeigniter ×1
jquery ×1
php ×1
recaptcha ×1
redirect ×1
routes ×1
rspec ×1
service ×1
slim ×1
slim-3 ×1