I have seen quite a few posts on this topic and apologise for not being able to use them for my example but i can seem to get it to work
I have a form that can be pre populated with the users information if the data exisits
def new
BraintreeTransaction::PopulateForm.new(@user).populate_form
end
module BraintreeTransaction
class PopulateForm
def initialize(user)
@user = user
end
def populate_form
return if Transaction.where(user_id: @user.id, completed: false).empty?
user_details = Transaction.where(user_id: @user.id, completed: false).order(created_at: :desc).first
@first_name = user_details.first_name if user_details.first_name.present?
@last_name = user_details.last_name if user_details.last_name.present?
end
end
My Form
<%= text_field_tag :first_name, @first_name, placeholder: 'First Name', required: true %>
<%= text_field_tag :last_name, @last_name, placeholder: 'Last Name', required: true %>
How do i access the instance variable in this situation?
Thanks
@user? You need and accessor, likeattr_reader :user(or manually definedef user; @user end)@user?@useris an instance variable, so I supposed you were asking how to access it outside ofPopulateForm. If I made the wrong assumption let me know.