在我的博客应用程序中,一些帖子显示为摘录 - 即,用户看到第一个(比如说)500个字符,并且可以单击链接查看整个帖子.以下是相关部分:
<% href = url_for post_path(:id => post) %>
<h1 class="title"><%= post.title %></h1>
<h2 class="published_on"><%= post.author %> wrote this <%= time_ago_in_words(post.published_on)%> ago</h2>
<div class="body">
<% if defined?(length) %>
<%= truncate_html(post.body, :length => length, :omission => "…<h1><a class='more' href=\"#{href}\">Click here for more!</a></h1>") %>
<% else %>
<%= post.body %>
<% end %>
</div>
Run Code Online (Sandbox Code Playgroud)
但是,而不是"点击此处获取更多信息!" 将用户带到一个单独的页面,我希望它能够填充内联的其余部分.目前,我已经通过将上面的代码片段放在以下div中来实现这一点:
<div class="post" id="post_<%= post.id %>">
<%= render :partial => 'post_content', :locals => { :post => post, :length => 500 } %>
</div>
Run Code Online (Sandbox Code Playgroud)
然后我在我的application.js中使用这个div的id来做AJAX:
$(document).ready(function() …Run Code Online (Sandbox Code Playgroud) 每当我使用Ajax将博客帖子加载到页面上时,我将页面设置<title>为"我的博客 - BLOGPOST_TITLE".
当然,"我的博客 - "也出现在我的应用程序布局中.
问题是,我怎么告诉我的Javascript字符串"我的博客 - "而不是在我的代码中重复它?
我想将一个简单的表解析为Ruby数据结构.该表如下所示:
alt text http://img232.imageshack.us/img232/446/picture5cls.png http://img232.imageshack.us/img232/446/picture5cls.png
编辑: 这是HTML
我想把它解析成一系列哈希.例如,:
schedule[0]['NEW HAVEN'] == '4:12AM'
schedule[0]['Travel Time In Minutes'] == '95'
Run Code Online (Sandbox Code Playgroud)
有关如何做到这一点的任何想法?Perl有HTML :: TableExtract,我认为它可以完成这项工作,但我找不到任何类似的Ruby库.
如果不是从数组中删除重复元素,我想删除具有共同特定属性的元素,该怎么办?
具体来说,我想从具有重复"精华"的数组中删除所有字符串,其中精华定义如下:
class String
def essence
downcase.gsub('&', 'and').gsub(/[^a-z0-9]/, '')
end
end
Run Code Online (Sandbox Code Playgroud)
我想要这样的东西:
['a', 'A', 'b'].uniq_by(&:essence)
# => ['a', 'b'] (or ['A', 'b']; I don't really care)
Run Code Online (Sandbox Code Playgroud)
实现这一目标的最佳方法是什么?
我想从字符串中删除"un-partnered"括号.
也就是说,(除非它们跟)在字符串中的某处,否则应该删除所有内容.同样,应该删除字符串中某个地方)之前的所有内容(.
理想情况下,算法也会考虑嵌套.
例如:
"(a)".remove_unmatched_parents # => "(a)"
"a(".remove_unmatched_parents # => "a"
")a(".remove_unmatched_parents # => "a"
Run Code Online (Sandbox Code Playgroud) Heroku 的数据库关注者非常酷。它们允许您创建一个单独的数据库,自动与您的主数据库保持同步。
我想使用这种技术来访问我的追随者数据库而不是我的主数据库以进行昂贵的读取操作(但显然我总是想写入我的主数据库)
当我在生产环境中时,是否可以访问两个数据库(写入的主数据库和读取的跟随数据库)?我理想的 API 应该是类似Post.use_db(:follower).find(1)或Post.find(1, :use_db => :follower)?
我只是git pull origin branch意外地做了,现在我将所有这些变化合并到了master
我尝试恢复提交w/github for mac并得到此错误:
fatal: Commit d0fbfb0f7d3ea8.. is a merge but no -m option was given.
Run Code Online (Sandbox Code Playgroud) 在我的在线商店,我想实现像亚马逊这样的界面来选择产品尺寸和颜色.即,不是用于不同尺寸/颜色的单选按钮,用户应该看到一堆小盒子,每个盒子包含一个颜色样本或一个大小的名称.当用户单击一个框时,边框应该更改以指示它已被选中.
我已经在使用jQuery了,所以我认为jQuery可选小部件是实现它的一种自然方式.不幸的是,它似乎无法让您控制用户可以选择的最大项目数,因此我无法限制用户在UI中选择在线一种颜色/尺寸.
以jQuery友好的方式(如果可能)实现此功能的最佳方法是什么?
我的州模型看起来像这样:
class State < ActiveRecord::Base
belongs_to :country
named_scope :order_by_name, :order => :name
validates_presence_of [:country, :name]
def <=>(other)
name <=> other.name
end
end
Run Code Online (Sandbox Code Playgroud)
我的国家模型看起来像这样:
class Country < ActiveRecord::Base
has_many :states
named_scope :order_by_name, :order => :name
def <=>(other)
name <=> other.name
end
end
Run Code Online (Sandbox Code Playgroud)
这适用于SQLite环境:
>> Country.find_by_iso("US").states
=> [#<State id: 1, name: "Alaska", abbr: "AK", # .....
Run Code Online (Sandbox Code Playgroud)
但是在postgresql环境中,我得到了这个:
>> Country.find_by_iso("US").states
ActiveRecord::StatementInvalid: PGError: ERROR: operator does not exist: character varying = integer
LINE 1: SELECT * FROM "states" WHERE ("states".country_id = 214)
^
HINT: …Run Code Online (Sandbox Code Playgroud) 我用Spreadhsheet :: ParseExcel成功解析了Perl中的Excel文档(在Perl中解析Excel文件的最佳方法是什么?),但我无法弄清楚如何提取单元格注释.
有任何想法吗?Perl或Ruby的解决方案是理想的.