0
votes

Quite new to rails/spree and am trying to create a pdf uploader where users can upload images that will then be sent to the company but when I try to send it I get an undefined local variable or method for 'spree_current_user'.

This is the line that gives the error. I know this would normally grab the id (I have used the same line in controllers) but I am not sure why it wouldn't work within the order mailer.

pdfs = Pdf.where(user_id: spree_current_user.id, created_at: Date.today-1.hour)

Basically I'm just wondering if anybody knows what else I could do? I tried to pass it through the controller as a session[] but this didn't work either.

If there are any other files that you might need to look at then please let me know. Would very much appreciate any help !

Thanks in advance.

2

2 Answers

2
votes

You can't access the session or Controller helpers (which spree_current_user is one of). You must pass controller-level information to the objects (mailers) you want them in.

So the calling code for the uploader object must pass in the user id into the object where you want it. Since you didn't specify what object that is (exactly) I can't really answer out of context. But as a general rule session-level concepts (like the current user) belong to the controller, and if you want them elsewhere (outside of the controller) you need to pass them along with the call to the objects that are called from the controller.

If that's not possible, a first-order association (like adding user_id to the object in question) is also a good solution, depending on your domain model.

0
votes

I have solved this issue if anyone else is needing it. What I did was pass the user id through a hidden field in the view:

hidden_field_tag 'current_user', spree_current_user.id

Then I added it in the controller OrderMailer.send_to_client(..., ..., ..., params[:current_user]).deliver

I then changed my original pdf line to account for pdfs created in between certain times:

pdfs = Pdf.where(user_id: current_user, pdf_updated_at: Time.now-1.hour..Time.now)

As well as sending it through in this method. def send_to_client( ..., ..., ..., current_user)

Hope this helps if anyone is experiencing a similar issue :).