开发者

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:

开发者_JAVA百科
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'


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜