Question

I'm overriding the create method of InvitationsController in devise_invitable the original code of which looks like this:

  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

I'm trying to add some fields_for to the invitation form so that I can build some objects associated with the member when I create the member in this controller. I can do it explicitly like by adding something like

    resource.my_associations.build(params[:my_association])

Is there a way to generalize the build statement? Perhaps imply the class name from teh params and build the association without knowing what it's called beforehand?

Was it helpful?

Solution

If the params hash contains only your association name(s), then you could write something like

params.each do |association, attributes|
  resource.send(association.to_s.pluralize).build(attributes)
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top