Reputation: 11
I'm trying to write code that will let me search the content submitted through a form and find the email address in the content. Below is the code, and the error message I'm receiving.
Error: undefined method `match' for {"content"=>"this is a test [email protected]"}:ActionController::Parameters
Code:
class ChallengesController < ApplicationController
def create
@challenge = current_user.challenges.build(challenge_params)
challenge_params.match(/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i)
# ...
end
private
def challenge_params
params.require(:challenge).permit(:content)
end
end
Upvotes: 0
Views: 270
Reputation: 17
I'm new to ruby so my answer may be very "amateur-ish", but sometimes the simplest ideas are the best... The way I would do this is search for index of @ sign. Then analyze each character before and after the sign until you get to blank space and save those index numbers. Use those index numbers to extract the email address from your string.
def email_address(str)
at_index = str.index('@')
temp = (at_index - 1)
while str[temp] != ' '
temp -= 1
end
begining_index = (temp + 1)
temp = (at_index + 1)
while str[temp] != ' '
temp += 1
end
end_index = temp
return str[begining_index..end_index]
end
Upvotes: 0
Reputation: 16506
You are applying match
on a Hash.
challenge_params
is a Hash. Going by the error message, this hash contains a key content
which is where you want to use with match
, hence rewriting the match
line to:
challenge_params["content"].match(/\b[A-Z0-9\._%+-]+@[A-Z0-9\.-]+\.[A-Z]{2,4}\b/i)
Upvotes: 1