Ruby on rails 在RubyonRails中构建抽象关联
我正在覆盖Desive_invitable中InvitationController的创建方法,其原始代码如下所示:Ruby on rails 在RubyonRails中构建抽象关联,ruby-on-rails,devise,devise-invitable,Ruby On Rails,Devise,Devise Invitable,我正在覆盖Desive_invitable中InvitationController的创建方法,其原始代码如下所示: def create self.resource = resource_class.invite!(resource_params, current_inviter) if resource.errors.empty? set_flash_message :notice, :send_instructions, :email => self
def create
self.resource = resource_class.invite!(resource_params, current_inviter)
if resource.errors.empty?
set_flash_message :notice, :send_instructions, :email => self.resource.email
respond_with resource, :location => after_invite_path_for(resource)
else
respond_with_navigational(resource) { render :new }
end
end
我正在尝试向邀请表单中添加一些字段,\u,以便在该控制器中创建成员时可以构建与该成员关联的一些对象。我可以通过添加以下内容来明确地实现
resource.my_associations.build(params[:my_association])
params.each do |association, attributes|
resource.send(association.to_s.pluralize).build(attributes)
end
有没有一种方法可以概括build语句?可能暗示来自params的类名,并在不事先知道调用了什么的情况下构建关联?如果params散列只包含您的关联名,那么您可以编写如下内容
resource.my_associations.build(params[:my_association])
params.each do |association, attributes|
resource.send(association.to_s.pluralize).build(attributes)
end