I have a form with a field that does not live permanently on the model so I am using attr_accessor on the model to make it available for use, however it is always returning nil when I try to access it.
The form field:
<%= text_field_tag :discount_code, nil, maxlength: "12", class: "span2" %>
The controller:
def an_action
@job = Job.find(session[:job_id])
@job.some_method
end
The model:
class Job < ActiveRecord::Base
attr_accessor :discount_code
def some_method
if !self.discount_code.blank?
do some logic
end
end
The above statement never executes as it is always evaluating to nil.
The value of the field is available in the controller as a param w/out issue:
discount_code: test50
action: process_payment
controller: jobs
Any pointers would be appreciated.
Actually, you could autoset the corresponding form value to the instance of the class. But you used a text_field_tag, which sets params[:discount_tag], when what you needed was a param[:job][:discount_tag]
For that, you could use:
Which is indeed a lot more elegant.