我在控制器中有一些逻辑,如果满足某些条件,则设置对象的状态:
if params[:concept][:consulted_legal] == 0 && params[:concept][:consulted_marketing] == 1
@concept.attributes = {:status => 'Awaiting Compliance Approval'}
elsif params[:concept][:consulted_marketing] == 0 && params[:concept][:consulted_legal] == 1
@concept.attributes = {:status => 'Awaiting Marketing Approval'}
elsif params[:concept][:consulted_marketing] == 0 && params[:concept][:consulted_legal] == 0
@concept.attributes = {:status => 'Awaiting Marketing & Legal Approval'}
else
@concept.attributes = {:status => 'Pending Approval'}
end
Run Code Online (Sandbox Code Playgroud)
我在创建和更新操作之间共享.你会如何重构这种肮脏的东西?寻找最佳实践.
编程新手并热衷于清理我的代码.
TIA.
我想在没有通知视图类型的控制器中使用updateModel.我有不同的视图,它们有不同的类型,但都有一个ExternalBase类作为继承类型.
所以在我的控制器中我总是有一个ExternalBase,但控制器不知道正确的类型.
在保存时,我调用一个获取正确对象的方法,但它将其作为externalBase返回.innertype是我的正确类型.如果我将鼠标悬停在我的对象上,则调用保存的视图类型.现在,如果我调用updateModel,它就不会填充属性.
举个例子:
// MyExternalBase是一个空类
班上的人
public class Person
: MyExternalBase
{
public string FirstName { get; set; }
public string LastName { get; set; }
public List<TheParameters> Parameters { get; set; }
public Address Address { get; set; }
public TheParameters[] OtherParameters { get; set; }
}
public class Address
{
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public TheParameters Parameter { get; set; …Run Code Online (Sandbox Code Playgroud) 我如何获得控制器的路径?例如,我可以像这样得到HtmlHelper的路径:
private static string GetVirtualPath(HtmlHelper htmlhelper)
{
string virtualPath = null;
TemplateControl tc = htmlhelper.ViewDataContainer as TemplateControl;
if (tc != null)
{
virtualPath = tc.AppRelativeVirtualPath;
}
return virtualPath;
}
Run Code Online (Sandbox Code Playgroud) (警告:Clueless Rails Newbie!)
在我的专辑视图的show.html.erb中,我在相册控制器中调用了一个公共方法:
<% albums_feature = find_albums_with_feature(feature.id) %>
Run Code Online (Sandbox Code Playgroud)
它会生成NoMethodError.
所以我将方法复制到我的Album模型中并尝试从视图中调用它:
<% albums_feature = Album.find_albums_with_feature(feature.id) %>
Run Code Online (Sandbox Code Playgroud)
但这也会得到一个NoMethodError.
我应该在哪里定义这种方法?
对于它的价值,该方法如下所示:
def find_albums_with_feature(feature_id)
albums_for_feature = Albums.find_by_sql(
["select al.* from albums al, albums_features alfe
where al.id = alfe.album_id
and alfe.feature_id = ?", feature_id])
end
Run Code Online (Sandbox Code Playgroud) 我是Rails的新手,不知道如何指定form_tag提交的位置?
<% form_tag do %>
<%= submit_tag "checkout_submit" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
对于上面的示例,此表单是否从呈现此视图的同一控制器提交给"checkout_submit"操作?
我一直在尝试在asp.net mvc中的控制器中做一个消息框,但每次我输入MessageBox或MsgBox时它都没有给我导入命名空间的选项......
我可以使用哪些类似于MessageBox?
我在所有控制器的beforeInterceptor块中都有以下相同的代码块:
def beforeInterceptor = {
request.someField = Foo.someFoo(request)
if (!request.someField) {
redirect(...)
return
}
}
Run Code Online (Sandbox Code Playgroud)
在所有控制器中重复完全相同的代码片段会违反DRY.是否有一些标准的Grails方法来定义"全局"beforeInterceptor?
我正在尝试测试一个只使用列表和创建的非常简单的表单.这是控制器:
public class PositionsController : Controller
{
private readonly IPositionRepository _positions;
// default constructor
public PositionsController()
{
_positions = new TestPositionRepository();
}
// DI constructor
public PositionsController(IPositionRepository positions)
{
_positions = positions;
}
// get a list of all positions
public ActionResult Index()
{
return View(_positions.GetAllPositions());
}
// get initial create view
public ActionResult Create()
{
return View();
}
// add the new Position to the list
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Position positionToAdd)
{
try
{
_positions.AddPosition(positionToAdd);
return RedirectToAction("Index");
}
catch …Run Code Online (Sandbox Code Playgroud) 我不知道这是不是坏形式,但我需要设置一个文件路径,可以访问控制器中操作中的所有对象.控制器中的一个操作会创建一个文件并将其存储在路径中.另一个操作使用send_file为文件服务.我存储变量的唯一地方是模型中的对象.然而,将URL存储在任意第一个对象中,或者将URL复制到所有对象上似乎真的很愚蠢.最好的方法是什么?
我希望这很清楚.
我正在为iPhone编程,我想知道何时使用视图的addSubview方法以及何时使用模态视图控制器(presentModalViewController).更复杂的是,如果你使用的是导航控制器(我不是),你可以使用pushViewController方法吗?
你什么时候使用每个?为什么?
谢谢.
controller ×10
asp.net-mvc ×3
ruby ×3
.net ×1
constructor ×1
dry ×1
dynamic ×1
grails ×1
iphone ×1
messagebox ×1
model ×1
namespaces ×1
path ×1
push ×1
refactoring ×1
sdk ×1
updatemodel ×1
view ×1
webforms ×1