在异步函数中,我可以得到一个异步值,如下所示:
const foo = await myAsyncFunction()
如果我想在结果上调用一个方法,使用同步函数我会做类似的事情 myAsyncFunction().somethingElse()
是否可以使用异步函数链接调用,或者是否必须为每个结果分配一个新变量?
我正在尝试构建一个文本格式化程序,它将基于换行符向文本添加p和br标记.我目前有这个:
s.replace(/\n\n/g, "\n</p><p>\n");
Run Code Online (Sandbox Code Playgroud)
这对于创建段落结尾和开头非常有效.但是,尝试查找
实例效果不佳.尝试进行匹配的组替换不起作用,因为它忽略括号并替换整个正则表达式匹配:
s.replace(/\w(\n)\w/g, "<br />\n");
Run Code Online (Sandbox Code Playgroud)
我已经尝试删除g选项(仍然替换了整场比赛,但仅限于第一场比赛).还有另一种方法吗?
谢谢!
我正在尝试分别验证表单的不同部分.不幸的是,表单是由CMS生成的,所以我的操作受到限制.
我尝试validate使用当前表单部分作为索引创建一个对象数组.即:
//initialize validation
validators = [
$('#donation_amount').validate({ rules:{ amount: { required: true } } }),
$('#personal_information').validate({ rules:{ Street: { required: true } } })
];
Run Code Online (Sandbox Code Playgroud)
并像这样切换到这些部分:
$('#btn-next').click(function() {
//if validation is true, show next page
if (validators[curOrder].valid()) {
var old = $('.active');
var oldOrder = old.attr('data-order');
var newOrder = parseInt(oldOrder) + 1;
old.removeClass('active');
$("[data-order='" + newOrder + "']").addClass('active');
curOrder = newOrder;
}else{
console.log("invalid");
}
});
Run Code Online (Sandbox Code Playgroud)
然而,验证总是返回true.
这是有问题的页面:https://salsa3.salsalabs.com/o/50388/p/salsa/donation/common/public/?donate_page_KEY = 8461
是否可以在Rails中向同一资源发送多个路径?
示例:将'/ foo-bars'和'/ foo_bars'路由到 resource :foo_bars
我已经看到了这个问题的其他问题,但到目前为止,答案对我没有用.我正在尝试使用一个注册用户的表单,并同时创建一个组织.用户和组织通过分配表关联.
这是我的表格:
= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f|
= devise_error_messages!
= f.fields_for :organizations do |f|
= f.label :name
= f.text_field :name
= f.label :email
= f.email_field :email
= f.label :password
= f.password_field :password
= f.label :password_confirmation
= f.password_field :password_confirmation
Run Code Online (Sandbox Code Playgroud)
我的注册控制器:
class Users::RegistrationsController < Devise::RegistrationsController
def new
@user = User.new
@user.organizations.build
end
def create
super
end
def update
super
end
end
Run Code Online (Sandbox Code Playgroud)
我的组织模型:
class Organization < ActiveRecord::Base
has_many :organization_assignments
has_many :users, :through => :organization_assignments
attr_accessible :name
end
Run Code Online (Sandbox Code Playgroud)
和我的用户模型: …
我在 postgres 函数中使用以下行:
regexp_replace(input, '[^a-z0-9\-_]+', sep, 'gi');
但是ERROR: invalid regular expression: invalid character range当我尝试使用它时,我得到了。正则表达式在 Ruby 中工作正常,是否有理由在 postgres 中有所不同?
我得到了Liquid error: undefined method 'to_liquid' for #<Email:0x007f9ca1a62d28>我的错误Email发送使用视图内的液体模板的电子邮件时对象.但是,当我在浏览器中自己渲染视图时,它工作得很好!
这是我的观点:
= raw @template.render('email' => @email, 'organization' => @organization)
%p= sanitize("<a href='{{ unsubscribe_url }}''>Click here to unsubscribe</a>")
%p{style:'text-align:center;'}
Sent with
= link_to 'Vocalem', 'http://vocalem.com'
Run Code Online (Sandbox Code Playgroud)
我的邮件的相关部分:
class BulkMailer < ActionMailer::Base
def bulk_email(recipients, email, organization)
...
@organization = organization
@email = email
@template = Liquid::Template.parse(organization.current_email_template.body)
Run Code Online (Sandbox Code Playgroud)
相关型号代码:
liquid_methods :subject, :body, :id
Run Code Online (Sandbox Code Playgroud)
最后但并非最不重要的是,Liquid的一个例子给出了错误(但在普通视图中正常工作!):
{{email.body}}
Run Code Online (Sandbox Code Playgroud)
奇怪的是,{{organization.display_name}}不会抛出错误,但在电子邮件中只是完全空白(尽管在普通视图中再次正常工作).
任何线索可能会发生在这里?
是否可以从 ActiveRecord::Relation 获取 has_many 记录?
即Book.where(fiction: true).pages相对于Book.where(fiction: true).collect { |book| book.pages }
理想情况下,我希望能够使用一个 ActiveRecord 查询获取所有记录,这样我就不必在内存中构建数组,并使代码更简洁,尤其是当关系具有多个级别时(即Book.where(fiction: true).pages.words
activerecord ruby-on-rails ruby-on-rails-4 rails-activerecord
javascript ×3
regex ×2
activerecord ×1
async-await ×1
devise ×1
forms ×1
jquery ×1
liquid ×1
postgresql ×1
validation ×1