Rails content_tag插入额外的“<”和“>”。人物 [英] Rails content_tag inserts extra "<" and ">" characters

查看:104
本文介绍了Rails content_tag插入额外的“<”和“>”。人物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当做这个时:

  def user_log 
如果logged_in? == false
form_tag session_path,:id => :mformdo
content_tag(:span,content_tag(text_field_tag:email,email@domain.com),:class =>memail)+
content_tag(:span,content_tag(password_field_tag :密码12345678912):class =>mpass)+
content_tag(:span,content_tag(submit_tag'Login'),:class =>mbutton)
end
else
...
end
end
end

我得到这个:



堆栈溢出不会让我发布图片



由于我不想额外使用<和>,我做错了什么?



编辑:作为额外的信息,在我看来我只是在做:

 <%= user_log%> 


解决方案

基本问题是您在使用content_tag两次你不需要。 content_tag 主要调用 content_tag_string 。这是 content_tag_string 的来源:

  def content_tag_string(name,content, options,escape = true)
tag_options = tag_options(options,escape)if
<#{name}#{tag_options}>#{content}< /#{name}> .html_safe
end

调用 content_tag(text_field_tag:email, email@domain.com)看起来像:

 <#{text_field_tag:email ,email@domain.com}> 

和text_field_tag已经生成完整的HTML标签(包含&和>) 。

所有你需要做的以除掉额外的角括号是省略第二个 content_tag

  content_tag(:span,text_field_tag(:email,email@domain.com),:class =>memail )+ 


When doing this:

def user_log
    if logged_in? == false
        form_tag session_path, :id => "mform" do
            content_tag(:span, content_tag(text_field_tag :email, "email@domain.com"), :class => "memail")+
            content_tag(:span, content_tag(password_field_tag :password, "12345678912"), :class => "mpass")+
            content_tag(:span, content_tag(submit_tag 'Login'), :class => "mbutton")
        end
    else
        ...     
        end
    end
end

I get this:

stack overflow doesn't let me post pictures

Since I don't want the extra "<" and ">", what am I doing wrong?

EDIT: As extra information, on my view I am just doing:

<%= user_log %>

解决方案

The fundamental problem is that you are using content_tag twice when you don't need to. content_tag essentially calls content_tag_string. Here's content_tag_string's source:

def content_tag_string(name, content, options, escape = true)
  tag_options = tag_options(options, escape) if options
  "<#{name}#{tag_options}>#{content}</#{name}>".html_safe
end

Calling content_tag(text_field_tag :email, "email@domain.com") looks like:

"<#{text_field_tag :email, "email@domain.com"}>"

and text_field_tag already produces a full HTML tag (it includes the "<" and ">").

All you need to do to get rid of the extra angled brackets is to leave out the second content_tag:

content_tag(:span, text_field_tag(:email, "email@domain.com"), :class => "memail")+

这篇关于Rails content_tag插入额外的“&lt;”和“&gt;”。人物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆