有没有一种简单的方法在Ruby应用程序中创建Word文档(.docx)?实际上,在我的情况下,它是从Linux服务器提供的Rails应用程序.
我有以下HTML:
<html>
<body>
<h1>Foo</h1>
<p>The quick brown fox.</p>
<h1>Bar</h1>
<p>Jumps over the lazy dog.</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我想将其更改为以下HTML:
<html>
<body>
<p class="title">Foo</p>
<p>The quick brown fox.</p>
<p class="title">Bar</p>
<p>Jumps over the lazy dog.</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
如何查找和替换某些HTML标记?我可以使用Nokogiri宝石.
我正在使用authlogic-example-app,当我注册用户时,我无法从OpenID提供商(在我的情况下是谷歌和雅虎)获得电子邮件地址.我得到一个空响应而不是电子邮件地址(请查看下面代码中的注释).
这就是我的用户模型的样子(其他一切看起来像上面提到的authlogic-example-app的"with_openid" - 分支).除了缺少"电子邮件"之外,openid-authentication-process按预期工作:
class User < ActiveRecord::Base
acts_as_authentic do |c|
# not needed because I use OpenID
c.validate_login_field = false
# avoid failed validation before OpenID request
c.validate_email_field = false
# this one sets 'openid.sreg.required=email'
c.required_fields = [:email]
end
private
# overwriting the existing method in '/lib/authlogic_openid/acts_as_authentic.rb'
def map_openid_registration(registration)
# this is my problem: 'registration' is an empty hash
self.email ||= registration[:email] if respond_to?(:email) && !registration[:email].blank?
end
end
Run Code Online (Sandbox Code Playgroud)
不知道怎么解决这个问题?有没有人在使用authlogic之前完成此操作?甚至更好:你有一个有效的例子吗?
更新:我检查了Google帐户身份验证API,并将authlogic(使用 …
我正在使用Rubyist的AASM为AR对象构建一个4步向导.根据对象的状态,需要进行不同的验证.根据某个过渡的状态验证对象的最智能方法是什么?
我正在尝试填补变量parent_element_h1和parent_element_h2.任何人都可以帮助我使用Nokogiri将我需要的信息输入到这些变量中吗?
require 'rubygems'
require 'nokogiri'
value = Nokogiri::HTML.parse(<<-HTML_END)
"<html>
<body>
<p id='para-1'>A</p>
<div class='block' id='X1'>
<h1>Foo</h1>
<p id='para-2'>B</p>
</div>
<p id='para-3'>C</p>
<h2>Bar</h2>
<p id='para-4'>D</p>
<p id='para-5'>E</p>
<div class='block' id='X2'>
<p id='para-6'>F</p>
</div>
</body>
</html>"
HTML_END
parent = value.css('body').first
# start_here is given: A Nokogiri::XML::Element of the <div> with the id 'X2
start_here = parent.at('div.block#X2')
# this should be a Nokogiri::XML::Element of the nearest, previous h1.
# in this example it's the one with the …Run Code Online (Sandbox Code Playgroud) 有没有办法实现类似于聊天的东西而不使用Flash或Silverlight?
不,尚未实现但非常好用的HTML5 Stream API不是一个有效的答案.;-)
我需要在Ruby on Rails中实现无向图G =(V,E),并考虑构建Vertex has_many Edges的Vertex和Edge模型.
由于边缘恰好连接两个顶点,您将如何在Rails中强制执行此操作?
你知道任何有助于实现这种图形的宝石或图书馆(对重新发明轮子不感兴趣;-))?
我使用Nokogiri(Rubygem)css搜索来查找<div>我的html中的某些内容.看起来Nokogiri的css搜索不喜欢正则表达式.我想切换到Nokogiri的xpath搜索,因为这似乎支持搜索字符串中的正则表达式.
如何在xpath搜索中实现下面提到的(伪)css搜索?
require 'rubygems'
require 'nokogiri'
value = Nokogiri::HTML.parse(<<-HTML_END)
"<html>
<body>
<p id='para-1'>A</p>
<p id='para-22'>B</p>
<h1>Bla</h1>
<p id='para-3'>C</p>
<p id='para-4'>D</p>
<div class="foo" id="eq-1_bl-1">
<p id='para-5'>3</p>
</div>
</body>
</html>"
HTML_END
# my_block is given
my_bl = "1"
# my_eq corresponds to this regex
my_eq = "\/[0-9]+\/"
# FIXME The following line should be changed to an xpath search.
if my_div = value.css("div#eq-#{my_eq}_bl-#{my_bl}.foo").first
# doing some stuff with the <p> inside the div
end
Run Code Online (Sandbox Code Playgroud) 我正在尝试让ActionView-Helpercollection_select获取一个将在下拉菜单中预先选择的值.
两者都没有(:selected在html-option-hash中)
<%= collection_select(:my_object, :my_method, @my_collection, :id, :description_string, {}, {:selected => @my_collection_object.id}) %>
Run Code Online (Sandbox Code Playgroud)
也不是(:selected在选项 - 哈希)
<%= collection_select(:my_object, :my_method, @my_collection, :id, :description_string, {:selected => @my_collection_object.id}, {}) %>
Run Code Online (Sandbox Code Playgroud)
似乎工作.
我究竟做错了什么?任何人都可以帮我这个吗?