假设我有以下字符串:
name1=gil;name2=orit;
Run Code Online (Sandbox Code Playgroud)
我想找到所有匹配项,name=value并确保整个字符串与模式匹配.
所以我做了以下事情:
确保整个模式符合我的要求.
Pattern p = Pattern.compile("^((\\w+)=(\\w+);)*$");
Matcher m = p.matcher(line);
if (!m.matches()) {
return false;
}
Run Code Online (Sandbox Code Playgroud)迭代模式 name=value
Pattern p = Pattern.compile("(\\w+)=(\\w+);");
Matcher m = p.matcher(line);
while (m.find()) {
map.put(m.group(1), m.group(2));
}
Run Code Online (Sandbox Code Playgroud)有没有办法用一个正则表达式做到这一点?
我有一个我写的宝石,我在我的rails应用程序中使用它.
我想从我的gem写入rails logger,但显然那里没有标准的rails logger.
实现我想做的事的正确方法是什么?
我将EmailValidator放在lib/validators/email_validator中并且它不起作用(我将root/lib放在load_path中)
这是代码..我把模块验证器中的类作为父文件夹名称
class Validators::EmailValidator < ActiveModel::EachValidator
def validate_each(object, attribute, value)
unless value =~ /^([^@\s]+)@([a-z0-9]+\.)+[a-z]{2,}$/i
object.errors[attribute] << (options[:message] || "is not formatted properly")
end
end
end
Run Code Online (Sandbox Code Playgroud)
我收到错误未知验证器:'email'
我正在使用TestUnit,我想测试记住我的功能(当用户登录时).
cookies变量(以及require/response .cookies)仅包含没有过期时间的cookie值.
当cookie应该过期时,Rails会以某种方式告诉Web浏览器,因此我认为必须有一种方法来检查cookie过期时间.
test "set permanent cookie" do
post :create, email: 'email', password: 'password', remember_me: true
# cookies[:auth_token] = random_string
# @request.cookies[:auth_token] = also_random_string
# @response.cookies[:auth_token] = also_random_string
end
Run Code Online (Sandbox Code Playgroud)
问题是我只能得到cookie的值而不是包含过期时间的哈希值.
我在application_controller中有before_filter:方法,我希望这个方法在继承类中的before_filter方法之后运行.
我该怎么做?
例
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :run_second
end
class SessionsController < ApplicationController
before_filter :run_first
end
Run Code Online (Sandbox Code Playgroud) 我有一个用户和故事模型,他们都有评论.
我宣布以下模型如下:
class Comment
belongs_to :commentable, polymorphic: true
belongs_to :user
end
class User
end
class Story
end
Run Code Online (Sandbox Code Playgroud)
现在,我想声明一个具有FactoryGirl的注释对象,该对象属于与值得称赞的用户和用户相同的用户.
到目前为止,这是我的代码:
FactoryGirl.define do
factory :user do
sequence(:email) {|n| "person#{n}@exmaple.com"}
sequence(:slug) {|n| "person#{n}"}
end
factory :comment do
occured_at { 5.hours.ago }
user
association :commentable, factory: :user
end
end
Run Code Online (Sandbox Code Playgroud)
这里的问题是编写评论的用户和值得称道的用户不一样.
我为什么要解决这个问题?
很多TNX
我试图在我的rails 3.0.8应用程序中伪造伪造请求但没有成功
我有常规表单,我用Tamper更改了auth键,然后提交它,我希望rails重置会话,因此注销current_user但是没有发生,操作成功完成,用户保持登录状态
我在应用程序控制器中有protect_from_forgery语句,我试图将config.consider_all_requests_local更改为false