0

In my controller I am trying to move my create method into a background process, as it takes too much time. I can't get it right.

My option 1:

First I went with creating a self method for my controller so it looked like this:

  def create
    MyController.delay.create_and_email(current_app_user)

    respond_to do |format|
      format.js {}
    end
  end

  def create_and_email(user)
    @user = AppUser.find_by(id: user.id)
    @link_share = @user.link_shares.build(link_share_params)
    @link_share.save
  end

I had to throw in the current_app_user as the self method could not see it, but I got totally puzzled on what to do with my link_share strong params, because the self method does not see the link_share_params. I guess I could make this option work, if anyone would point me how to throw in strong params into a self. controller method.

My option 2:

I then decided to move the whole action into my User model like this

Contoller:

  def create
    @user = current_app_user
    @user.delay.create_and_email(link_share_params)

    respond_to do |format|
      format.js {}
    end
  end

Model:

  def create_and_email
    link_share = self.link_shares.build(**params**)
    link_share.save
  end

Again, trouble with the strong params. How would I go with importing my params into the model? Should I just chain them as arguments?

Any advice appreciated

The Whiz of Oz
  • 6,763
  • 9
  • 48
  • 85
  • params should be just fine with model as well, what is the error thrown in 2nd option? – Nithin Feb 25 '15 at 14:35
  • Job AppUser#create_and_email (id=15) FAILED (0 prior attempts) with ActiveModel::ForbiddenAttributesError: ActiveModel::ForbiddenAttributesError – The Whiz of Oz Feb 27 '15 at 11:50

0 Answers0