Tag: ruby on rails 3

如何从Rails中的date_select或select_date获取日期?

使用select_date会给我一个带有year , month和day属性的params[:my_date] 。 如何轻松获取Date对象? 我希望像params[:my_date].to_date这样的东西。 我也很高兴使用date_select 。

Ruby on Rails中和之间的区别是什么?

它与输出有关吗? 因此, 用于在执行代码后输出, 仅用于执行代码?

创建具有树结构的模型

我有树结构的类别。 我试图通过为每个人定义父母来将他们联系在一起。 (我无法弄清楚如何调用属性parent所以它现在只是category ,但它意味着父级)。 class Category < ActiveRecord::Base has_one :category # the parent category end 但这种关系最终走错了路。 getter函数在子类别上(正确),但category_id存储在父类中: parent = Category.create(:name => “parent”) child = Category.create(:name => “child”, :category => parent) parent.id # 1 child.id # 2 child.category_id # nil parent.category_id # 2 child.category.name # “parent” (!!) 父母需要能够有多个孩子,所以这不会起作用。

如何在Rails 3中生成带尾部斜杠的链接?

我将现有的网站从PHP移植到Ruby on Rails 3,我必须保持url不变。 我有路线: get ‘companies/’ => ‘companies#index’, :as => :companies 在视图文件中我有: link_to ‘Companies’, companies_path 这会生成URL“http://website.com/companies”而不是“http://website.com/companies/”。 我希望url末尾有斜杠。 可能吗?

在初始化程序中设置cache_store

我正在尝试使用redis-store作为我的Rails 3 cache_store。 我还有一个初始化器/ app_config.rb,它为配置设置加载一个yaml文件。 在我的初始化程序/ redis.rb中,我有: MyApp::Application.config.cache_store = :redis_store, APP_CONFIG[‘redis’] 但是,这似乎不起作用。 如果我做: Rails.cache 在我的rails控制台中,我可以清楚地看到它正在使用 ActiveSupport.Cache.FileStore 作为缓存存储而不是redis-store。 但是,如果我在application.rb文件中添加配置,如下所示: config.cache_store = :redis_store 它运行得很好,除了在app.rb之后加载app config初始化程序,所以我没有访问APP_CONFIG。 有没有人经历过这个? 我似乎无法在初始化程序中设置缓存存储。

使用Rails3和Ruby 1.9.2运行Mongrel的LoadError

我是编程(以前只做html / css / design)的新手,试图通过Simply Rails 2开始学习RoR。但是我想从最新版本的Ruby(1.9.2)和Rails(3)开始我被迫在没有这本书的情况下完成所有配置。 我通过谷歌和stackoverflow看了很多。 当我运行rails server它自动使用Webrick,但我想使用Mongrel。 我通过gem install mongrel –pre安装了预发布的Mongrel并删除了以前的版本。 我读到为了让Mongrel与rails server一起工作,我不得不在Gemfile中添加gem’mongrel gem ‘mongrel’ ,但我不知道应该将它添加到哪一个(我尝试了几个但没有改变)。 所以我决定通过git查看mongrel文档并了解mongrel_rails start在我运行时mongrel_rails start我得到了这个: C:\Ruby192\shovell>mongrel_rails start ** Starting Mongrel listening at 0.0.0.0:3000 ** Starting Rails with development environment… C:/Ruby192/lib/ruby/gems/1.9.1/gems/bundler-1.0.0.rc.6/lib/bundler/runtime.rb:27:in ‘block in setup’: You have already activated mongrel 1.2. 0.pre2, but your Gemfile requires mongrel 1.1.5. Consider using […]

关于将实例变量传递给redirect_to方法的困惑。 正如Rails指南中所见

我正在研究rails指南上的ruby,即http://guides.rubyonrails.org/layouts_and_rendering.html上的“布局和渲染”主题 我很困惑将实例变量传递给redirect_to方法。 这怎么可能? 我认为redirect_to与重定向到另一个网页或url有关。 在指南中给出的示例中,它说明如下: 2.2.2渲染动作视图 如果要在同一模板中呈现与不同操作对应的视图,可以使用带有视图名称的render: def update @book = Book.find(params[:id]) if @book.update_attributes(params[:book]) redirect_to(@book) else render “edit” end end 渲染“编辑”完全有意义,它将再次渲染新forms。 但是redirect_to(@book)在世界上发生了什么? 究竟是什么呈现以及如何将书籍对象重定向到? 顺便说一句,书籍模型有专栏,名称,作者,页面等…

Ruby on Rails Rake资产:预编译错误

我是Rails开发的新手,这是我第一次部署到Heroku。 当我执行我的应用程序(部署后)时出现错误: 2011-09-18T21:05:54+00:00 app[web.1]: Completed 500 Internal Server Error in 10ms 2011-09-18T21:05:54+00:00 app[web.1]: 2011-09-18T21:05:54+00:00 app[web.1]: ActionView::Template::Error (application.css isn’t precompiled): 我搜索了这个,发现我必须预编译我的资产,但是当我尝试这样做时,我发现了另一个错误: $ bundle exec rake assets:precompile RAILS_ENV=production rake aborted! No such process – /usr/lib/ruby/gems/1.8/gems/pg-0.11.0/lib/pg_ext.so (See full trace by running task with –trace) 我还没有找到解决这个问题的方法。 是否有人知道如何解决这个问题?

nil的未定义方法`eq’:nilClass with rails 3和ruby enterprise on ubuntu hardy

我不确定是什么原因导致这个错误但是因为我在乘客中遇到了这个问题而现在在控制台中我也在想,也许mysql驱动程序是问题所在? 在osx / 10.6上,一切都按预期工作。 它只是不会在Ubuntu 8.04上耐用。 我把问题钉在了“destroy”方法上,或者当我直接调用activerecord实例的方法时。 为什么会这样? 有任何想法吗? 系统: Ubuntu 8.04耐用。 ruby 1.8.7(2010-04-19 patchlevel 253)[i686-linux],MBARI 0x8770,Ruby Enterprise Edition 2010.02 rails 3.0.0-beta4 mysql Ver 14.12 Distrib 5.0.51a,debian-linux-gnu(i486)使用readline 5.2 这是错误输出 root@gehege:/var/www/foobar.com/api.foobar.com/v1# rake cleanup:status_cleanup RAILS_ENV=production –trace (in /var/www/foobar.com/api.foobar.com/v1) ** Invoke cleanup:status_cleanup (first_time) ** Invoke environment (first_time) ** Execute environment ** Execute cleanup:status_cleanup rake aborted! undefined method `eq’ […]

迭代Ruby中深度嵌套的哈希级别

所以我有一个哈希,对于每个哈希级别,我想存储它的键和值。 问题是,值可以是另一个哈希数组。 此外,该哈希可以包含键值对,其中值又是另一个哈希数组等等。另外,我不知道每个哈希的嵌套程度有多深。 举个例子: { :key1 => ‘value1’, :key2 => ‘value2’, :key3 => { :key4 => ‘value4’, :key5 => ‘value5’ }, :key6 => { :key7 => ‘value7’, :key8 => { :key9 => ‘value9’ } } } ..等等。 我想要做的是保存每个键,值对和其父级的ID。 我认为这可能是递归完成的,我只是不确定如何因为我不熟悉递归函数。 我知道如何正常迭代数据: myHash.each {|key, value| …Do something with the key and value … } 所以我猜测递归调用将是这样的: def save_pair […]