Stoob
Stoob

Reputation: 121

If statement not evaluating to true as expected

I'm trying to check if a param ':postcode' of the current '@inquiry' exists in a model called 'Post' as per the create method below.

def create
    @inquiry = Inquiry.new(inquiry_params)
    p = @inquiry.postcode
  if @inquiry.save && Post.exists?(pcode:'#{p}')
    redirect_to '/signup'
  else
    redirect_to '/'
  end
end

If I replace #{p} with a literal string (hope thats the right terminology) for example "SE9" that I know exists in the 'Post' model, the if statement evaluates to true but when I use the code above it does not even if I know the @inquiry.postcode exists.

Upvotes: 0

Views: 38

Answers (1)

NickGnd
NickGnd

Reputation: 5197

Interpolate the String with double quotes " instead of single quotes '

An example with Ruby 2.2.3:

p = "value"
#=> "value" 

'#{p}'
#=> "#{p}" 

"#{p}"
#=> "value" 

Upvotes: 4

Related Questions