0

I am looking to see how I would set up my :notice to incorporate a helper method, at the moment I have this and get 'undefined method to_dollars':

notice: "Thank you for your payment of #{to_dollars(amount)}"

Controller

def create
  @donation = @campaign.donations.create(donation_params)
    if @donation.save_with_payment
      amount = @donation.donation_amount
      redirect_to @campaign, notice: "Thank you for your payment of #{to_dollars(amount)}"
    else
     flash[:notice] = @donation.errors
     render :new
  end
end

My helper method is simply (unless anyone has a better way of doing it)

def to_dollars(amount)
  convert = amount / 100
  number_to_currency(convert, unit: "$", separator: ".", delimiter: "")
end

All donation_amounts are saved in cents so just want to convert them to dollars.

halfer
  • 19,824
  • 17
  • 99
  • 186
Richlewis
  • 15,070
  • 37
  • 122
  • 283

1 Answers1

1

It is not a good practice to use helper methods in controller as helpers are meat for UI help functionality. If at all you want to use this to_dallers helper method in controller then you can include simply the helper module in controller.

Please check this answer

If you have to use this to_dollers method in controller then you can use either of following ways.

  • you can write this method in an ActiveRecord consern or separate module and include that in the model it self so that you can call the to_dollers method on object itself.
  • you can include it in separate module like AmountUtility module and include such methods in this module. Then this module can be included anywhere as you need.

Hope this helps.

Community
  • 1
  • 1
raviture
  • 959
  • 7
  • 11
  • 2
    please mark as duplicate instead – Ven Jun 26 '14 at 08:44
  • I don't agree with that. First you may want to use formatting code in controller in some very specific cases (flash notice are a very good example of that). Second, the referred answer does not mention the use of `AbstractController::Helpers#helper_method`, which is the correct answer in this case. – kik Jun 26 '14 at 08:53
  • The answer that I referred states to include the module of helper in controller itself. ie say that you having to_dollers method in `module DonationAmountHelpers` then write `include DonationAmountHelpers` in controller, thats it, you will be having to_doller method in controller. ------------------------------------------------------------------ Regarding your view of accessing the helper method in controller. I am updating my answer please check. – raviture Jun 26 '14 at 09:01
  • Yep, that's the problem :) This is fine when you want to include third party helpers, but if you write a helper yourself then load it in controller, it's probably that it should not have been defined as helper in first place, but as lib or concern which methods are loaded as helpers (with `#helper_method`). Of course, app won't break if done the other way, but this is precisely what the guideline "do not include helpers in controllers" mean. – kik Jun 26 '14 at 09:06