我正在努力做我认为应该是一项非常简单的任务,但在过去一小时内没有这样做.如果用户属性与值匹配,我想默认选择一个选择选项.
<select name="myName">
{{#each addKeys myTable}} <!-- addKeys creates variables for keys and values -->
<option value="{{key}}" {{#if currentUser.property === key}}selected="selected"{{/if}}>{{value}}</option>
{{/each}}
</select>
Run Code Online (Sandbox Code Playgroud)
现在我觉得这很容易实现.但事实证明,Spacebars不允许除否定感叹号之外的条件运算符,因此等号无关紧要.然后我尝试了一些可怕的尝试:
在模板中myTemplate
:
<select name="myName">
{{#each addKeys myTable}}
<option value="{{key}}" {{isSelected currentUser.property key}}>{{value}}</option>
{{/each}}
</select>
Run Code Online (Sandbox Code Playgroud)
在mytemplate.js
:
Template.myTemplate.helpers({
isSelected: function(v1, v2) {
if (v1 === v2)
return "selected=\"selected\"";
return '';
}
});
Run Code Online (Sandbox Code Playgroud)
这段代码不仅可怕,看起来很糟糕,它不起作用:
Exception in Meteor UI: String contains an invalid character
Run Code Online (Sandbox Code Playgroud)
我不明白为什么这么简单的事情似乎无法实现.我错过了什么吗?
在最新的Symfony 2版本(使用FOSUserBundle和SonataUserBundle)上,我试图让当前用户在我的新实体控制器中预填充表单.
在mycontroller.yml上:
my_controller_new:
pattern: /new
defaults: { _controller: "MySiteBundle:MyController:new" }
Run Code Online (Sandbox Code Playgroud)
在MyController.php上:
public function newAction()
{
$user = $this->getUser();
// ...
}
Run Code Online (Sandbox Code Playgroud)
但是当我打电话时$this->getUser()
,该方法返回null
.即使我已登录(我可以/profile
毫无问题地访问和更新我的用户信息)
然后我试着检查会话是否已启动:
public function newAction()
{
var_dump($this->get('session')->isStarted());
// ...
}
Run Code Online (Sandbox Code Playgroud)
它返回false.这有什么不对?
我试图使用正则表达式来匹配单词,但遗憾的是,单词边界字符(\ b)不包含足够的字符,因此我想添加更多字符.(在那个精确的情况下,"+"字符)
这是我过去常常拥有的(它是C#,但不是很相关):
string expression = Regex.Escape(word);
Regex regExp = new Regex(@"\b" + expression + @"\b", RegexOptions.IgnoreCase);
Run Code Online (Sandbox Code Playgroud)
这个特殊的正则表达式与"C++"不匹配,我认为这真是一个真正的无赖.所以我尝试在字符类中使用\ w字符,以及+字符:
string expression = Regex.Escape(word);
Regex regExp = new Regex(@"(?![\w\+])" + expression + @"(?![\w\+])", RegexOptions.IgnoreCase);
Run Code Online (Sandbox Code Playgroud)
但现在,没有任何东西可以匹配......我有什么遗漏的吗?
我正在使用Meteor 0.8.2和accounts-facebook.我以这种方式为用户设置了一个有限的出版物:
Meteor.publish('users', function () {
return Meteor.users.find({}, {fields: {'profile.picture': 1, 'profile.gender':1, 'profile.type':1}, sort: {'profile.likes': -1}});
});
Run Code Online (Sandbox Code Playgroud)
现在这很好用:当我从客户端请求用户列表时,我得到所有用户的列表,其中显示当前用户的所有字段,并且仅显示其他用户的3个已发布字段.除外:登录后立即
当我登录并输入时Meteor.user()
,这是我得到的:
_id: "uACx6sTiHSc4j4khk"
profile: Object { gender="male", type="1", picture="http://....jpg"}
Run Code Online (Sandbox Code Playgroud)
这一直保持不变,直到我使用浏览器按钮刷新页面.刷新后,Meteor.user()
提供所有可用字段,同时Meteor.users.find()
仍然给出正确的限制.(当然除了当前用户)
为什么我当前的用户不能立即获得所有字段?我读到了一个Meteor.userLoaded()
用于等待用户加载的方法,但它似乎在最新版本中已经过时了.
我有以下代码:
def register_learner
@event = Event.find(params[:event_id])
@registation = EventRegistration.new first_name: params[:first_name], last_name: params[:last_name], email: params[:email], event_id: params[:event_id]
if !@registation.valid?
@registation.errors.full_messages.delete("Event has already been taken"
flash[:notice] = @registation.errors.full_messages.to_sentence
redirect_to(event_path(@event))
else
@registation.save
end
end
Run Code Online (Sandbox Code Playgroud)
请注意@registation.errors.full_messages.delete("Event has already been taken")
我尝试从full_messages数组中删除此特定消息的行,但它不起作用.下一行是flash消息,仍然显示消息"已经采取了事件".
这是通过控制台进行的健全性检查......
2.1.5 :001 > errors = ["Event has already been taken", "Last name can't be blank"]
=> ["Event has already been taken", "Last name can't be blank"]
2.1.5 :002 > errors.delete "Event has already been taken"
=> "Event has already been …
Run Code Online (Sandbox Code Playgroud)