0

The following controller private method

def set_promotion
  @promotion = Promotion.find(params[:id])
  if !current_shopkeeper.nil?
    @current_user = User.where(['email = ?', current_shopkeeper.email]).first
  end
end

generates the @current_user, but not the promotion instance variable. In fact when inspecting the action

puts params[:id].class
puts @promotion
puts @current_user.id
puts Promotion.find(params[:id])

the values returned are

string
[blank_line]
16292
#<Promotion:.... >

even though the controller has the before_action defined, which kicks in given the current_user.id being returned

  before_action :set_promotion, only: [:show, :edit, :update, :destroy]

the action literally required to call

@promotion = Promotion.find(params[:id])

to render the proper action.

Note: this is a controller that does not normally have access to devise's current_user and thus requires its own private method. Not sure if this impacts anything.

def pundit_user
  User.where(['email = ?', current_shopkeeper.email]).first
end

What could be going on here?

Jerome
  • 5,583
  • 3
  • 33
  • 76
  • Is this working for some actions or not at all? – Eyeslandic Sep 16 '20 at 10:25
  • Apparently not at all: the edit function does not pick up the instance variable. the argument in the form for the instance variable is nil (or empty) – Jerome Sep 16 '20 at 10:59
  • Well, the code is correct as far as I can tell. Strange things... – Eyeslandic Sep 16 '20 at 11:19
  • Yes!! Placing the exact same strong sequence within the action itself DOES generate the instance variable. – Jerome Sep 16 '20 at 11:24
  • Can you post the controller in its entirety, rather than slicing bits out of it? Impossible to see if you have an error with what you're showing us. – Jon Sep 16 '20 at 21:11
  • The show action was defined `def show \r end` It's utterly weird that part of the before_action method fires and another does not. I had to force the call by including `@promotion = Promotion.find(params[:id])` in the show action method. – Jerome Sep 17 '20 at 09:34

0 Answers0