我在rails 3模型中有一个方法,用nokogiri解析XML.如何在控制台中调用此方法以测试它.
这是全班(我试着调用generate_list):
class Podcast < ActiveRecord::Base
validates_uniqueness_of :name
serialize :hosts
def generate_list
# fetch the top 300 podcasts from itunes
itunes_top_300 = Nokogiri.HTML(open("http://itunes.apple.com/us/rss/toppodcasts/limit=300/explicit=true/xml"))
# parse the returned xml
itunes_top_300.xpath('//feed/entry').map do |entry|
new_name = entry.xpath("./name").text
podcast = Podcast.find(:all, :conditions => {:name => new_name})
if podcast.nil?
podcast = Podcast.new(
:name => entry.xpath("./name").text,
:itunesurl => entry.xpath("./link/@href").text,
:category => entry.xpath("./category/@term").text,
:hosts => entry.xpath("./artist").text,
:description => entry.xpath("./summary").text,
:artwork => entry.xpath("./image[@height='170']").text
)
podcast.save
else
podcast.destroy
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
编辑:哇,1000次观看.我希望这个问题对人们起到了帮助作用.当我回顾这一点时,令我惊讶的是,仅仅一年多以前,我无法弄清楚实例方法和类方法之间的区别.现在我在ruby,Rails和许多其他语言/框架中编写复杂的面向服务的应用程序和后端. Stack Overflow就是这个原因.非常感谢这个社区赋予人们解决问题和理解他们的解决方案的权力.
我正在尝试input通过将其类别属性与Javascript变量中的类别顺序进行比较来重新排序标记的子元素category_sort_order.然后我需要删除其类别属性不出现的div category_sort_order.
预期结果应该是:
任何
产品1
产品2
下载
代码:
<div id="input">
<div category="download">download</div>
<div category="video">video1</div>
<div category="video">video2</div>
<div category="product">product1</div>
<div category="any">any</div>
<div category="product">product2</div>
</div>
<script type="text/javascript">
var category_sort_order = ['any', 'product', 'download'];
</script>
Run Code Online (Sandbox Code Playgroud)
我真的甚至不知道从哪里开始这项任务,但如果你能提供任何帮助,我将非常感激.
我在Rails 3应用程序中使用Devise和OmniAuth(Facebook).我刚刚开始注意到这种行为.
当用户登录时,他被重定向到他的仪表板,但是,字符"#_"被附加到URL.我现在唯一能想到的是由以下方法创建的路线之间的冲突:
resources :users
Run Code Online (Sandbox Code Playgroud)
和
# User Authentication
devise_for :users,
:singular => :user,
:controllers => {:registrations => 'registrations'} do
get 'logout' => 'devise/sessions#destroy'
end
Run Code Online (Sandbox Code Playgroud) 我有一个Rails 3应用程序,当用户点击页面时,它使用delayed_job获取一些数据(使用类方法).如何向ajax进程指示类方法已运行(以便我可以停止轮询)?
编辑:
为了澄清,我想知道delayed_job何时运行,而不是在ajax进程成功时.然后我想将delayed_job完成状态传递给正在运行的ajax进程.
我在使用Sunspot Solr搜索Ruby on Rails方面有很棒的经验,但是,它的日志文件正在变得非常大,我似乎无法找到一种方法来旋转,覆盖或关闭这些日志(除了非常我宁愿不追求的hacky方法.
我在config /中有一个文件sunspot.yml,我尝试将log_level标志设置为SEVERE,但这没有任何效果.
我尝试使用标准的Logger.config旋转方法,但是,它只是将我的开发日志输出发送到新创建的文件.
我非常感谢您提供的任何建议.
我在我的Rails 3应用程序中使用active_admin gem,它具有依赖inherited_resources.我有点像newb,宁愿为我自己的控制器避免使用inherited_resources的黑盒子质量,但是,当我运行默认的rails g scaffold命令时,生成的控制器继承自inherited_resources.我知道我可以通过继承ApplicationController手动覆盖它,但是,如果可能的话,我希望能够生成默认的rails支架.