我试图将自定义错误添加到我的用户模型的实例,但是当我调用有效时?它正在擦除自定义错误并返回true.
[99] pry(main)> u.email = "test@test.com"
"test@test.com"
[100] pry(main)> u.status = 1
1
[101] pry(main)> u.valid?
true
[102] pry(main)> u.errors.add(:status, "must be YES or NO")
[
[0] "must be YES or NO"
]
[103] pry(main)> u.errors
#<ActiveModel::Errors:[...]@messages={:status=>["must be YES or NO"]}>
[104] pry(main)> u.valid?
true
[105] pry(main)> u.errors
#<ActiveModel::Errors:[...]@messages={}>
Run Code Online (Sandbox Code Playgroud)
如果我validate在模型中使用该方法,那么它可以工作,但是这个特定的验证是从不同的方法中添加的(这需要传递参数):
User
def do_something_with(arg1, arg2)
errors.add(:field, "etc") if arg1 != arg2
end
Run Code Online (Sandbox Code Playgroud)
由于上述原因,user.valid?即使将错误添加到实例,也会返回true.
我是Grails的新手,我正在努力解决许多简单的问题.
例如,当我从页面顶部布局上显示的模板视图登录/注销时,我还没有找到回到上次访问页面的正确方法.
我解决此问题的最后一次尝试是将$ {params.controller}和$ {params.action}保存在发送到注销操作的参数中,然后重定向.嗯......即使这样也失败了.这是gsp片段:
<g:link controller="user" action="logout" params="[currentController: ${params.controller}, currentAction: ${params.action}]">Logout</g:link>
Run Code Online (Sandbox Code Playgroud)
最后一个代码行抛出以下异常:
ERROR errors.GrailsExceptionResolver - Error evaluating expression [[currentController: ${params.controller}, currentAction: ${params.action}]]
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:
1 - 如何在登录/注销操作后重新加载上次访问的页面?
2 - 为什么我的代码有例外?
谢谢
编辑:关于问题#2,似乎以下代码工作:
<g:link controller="user" action="logout" params="[currentController: params.controller, currentAction: params.action]">Logout</g:link>
Run Code Online (Sandbox Code Playgroud)
但我真的不明白原因......
EDIT2:我还找到了重定向到上次访问页面的解决方案:
redirect(url: request.header('referer'))
Run Code Online (Sandbox Code Playgroud)
但不幸的是,在登录后执行此操作时,我页面中呈现的内容是重复的.任何想法或任何其他安全解决方案?
在寻找生成真正随机数的最佳尝试时,我偶然发现了这个代码示例.
在这个片段上寻找意见.
using System;
using System.Security.Cryptography;
private static int NextInt(int min, int max)
{
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] buffer = new byte[4];
rng.GetBytes(buffer);
int result = BitConverter.ToInt32(buffer, 0);
return new Random(result).Next(min, max);
}
Run Code Online (Sandbox Code Playgroud)
资料来源:http://www.vcskicks.com/code-snippet/rng-int.php
这比使用滴答计数种子更受欢迎,例如:
Random rand = new Random(Environment.TickCount);
rand.Next(min, max);
Run Code Online (Sandbox Code Playgroud)
注意:
我不是在寻找第三方随机数据提供者,例如Random.org,因为这种依赖对应用程序来说是不现实的.
我不明白使用DLL的.def文件的意义.
它似乎取代了在您的DLL代码中使用显式导出的需要(即显式__declspec(dllexport))但是我不能在不使用这些时生成lib文件,然后在使用DLL时创建链接器问题.
那么在与客户端应用程序链接时如何使用.def,它们是否取代了使用头文件或.lib文件的需要?
在将输入字段可能通过javascript更改为其他值之后,我需要能够将输入字段重置为其原始颜色.问题是我不想在样式表发生变化时对值进行硬编码.我想使用页面上使用的默认颜色.
重置颜色就好了,还是有更好的方法来做到这一点?
$('#theinput').css('color', '');
Run Code Online (Sandbox Code Playgroud) 我需要能够在a的表行中有一个按钮/链接QTableView.这是打开一个对话框,以便更有效地编辑该行.
经过几个小时的网络搜索,我还没有找到一个体面的例子.
我知道这很可能是使用a来完成的QItemDelegate,但我不确定如何在行中创建一个功能小部件,而不必先将项目强制转换为编辑模式.
任何帮助将不胜感激.
当毫秒存储为'long'时,我正在寻找向Java Date添加毫秒的最佳方法.Java日历有一个add函数,但它只需要一个'int'作为数量.
这是我提出的一个解决方案......
Calendar now = Calendar.getInstance();
Calendar timeout = Calendar.getInstance();
timeout.setTime(token.getCreatedOn());
timeout.setTimeInMillis(timeout.getTimeInMillis() + token.getExpiresIn());
Run Code Online (Sandbox Code Playgroud)
还有其他建议吗?
我只是想在控制器规范上测试ajax请求.产品代码如下.我正在使用Devise进行身份验证.
class NotesController < ApplicationController
def create
if request.xhr?
@note = Note.new(params[:note])
if @note.save
render json: { notice: "success" }
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
规格如下.
describe NotesController do
before do
user = FactoryGirl.create(:user)
user.confirm!
sign_in user
end
it "has a 200 status code" do
xhr :post, :create, note: { title: "foo", body: "bar" }, format: :json
response.code.should == "200"
end
end
Run Code Online (Sandbox Code Playgroud)
我希望响应代码为200,但它返回401.我想这肯定是因为rspec抛出的请求缺少authenticity_token或其他东西.我该如何存根?
任何帮助将不胜感激.
我有一个Project模型,它接受Task的嵌套属性.
class Project < ActiveRecord::Base
has_many :tasks
accepts_nested_attributes_for :tasks, :allow_destroy => :true
end
class Task < ActiveRecord::Base
validates_uniqueness_of :name end
Run Code Online (Sandbox Code Playgroud)
任务模型中的唯一性验证在更新Project时会出现问题.
在编辑项目时,我删除任务T1,然后添加一个同名T1的新任务,唯一性验证限制了项目的保存.
params hash看起来像
task_attributes => { {"id" =>
"1","name" => "T1", "_destroy" =>
"1"},{"name" => "T1"}}
Run Code Online (Sandbox Code Playgroud)
在销毁旧任务之前完成对任务的验证.因此验证失败.任何想法如何验证,它不会考虑任务被销毁?
我想在Grails应用程序中使用服务.但是,它始终为null.我使用的是Grails 1.1版.我怎么解决这个问题?
示例代码:
class A{
String name;
def testService;
static transients=['testService']
}
Run Code Online (Sandbox Code Playgroud)
我可以在域类中使用服务吗?