Rails beginner
Rails beginner

Reputation: 14504

Rails " problem

I am using Intype:

I have a problem with this:

def convert_html_entities(text)
    text = text.gsub(/["]/, '"')   
end 

The Intype colors it all green because there is missing a "

I have tried this solution it removed all the green text and the code appeared normal:

def convert_html_entities(text)
    text = text.gsub(/['"']/, '"') 
end 

But it just gave an error in view:

undefined method `convert_html_entities' for #<XmlController:0x448cef0>

Rails.root: C:/Rails/kimnew
Application Trace | Framework Trace | Full Trace

lib/update_xml.rb:21:in `block in update_xml'
lib/update_xml.rb:19:in `update_xml'
app/controllers/xml_controller.rb:21:in `test'

Upvotes: 0

Views: 81

Answers (2)

Richard.P
Richard.P

Reputation: 802

Where did you define your method ? Your problem is because your controller can't find it, so should define it in your ApplicationController if you want to use it with multiple controllers, or in a helper that you can include in your controller if you also want to use it in views.

Also if you want to safely convert HTML entities you can use CGI::escapeHTML.

Upvotes: 0

sepp2k
sepp2k

Reputation: 370092

Apparently you did not define convert_html_entities as an instance method of XmlController, so you can't use it as one.

Another problem with your code is that reassigning a method parameter, doesn't have any effect on the outside. So text = text.gsub(/['"']/, '&quot;') is the same as text.gsub(/['"']/, '&quot;'). If you want to mutate your argument, you need to use gsub!. That being said: Don't mutate method arguments. That's bad style.

Upvotes: 1

Related Questions