如何在没有设计的情况下设置acts_as_votable?

我正在关注这篇文章 (我试图让人们投票而不登录,1投票/ ip)当我按下upvote按钮时,我收到以下错误: CommentsController中的NoMethodError #upvote未定义方法`find_or_create_by_ip’for# 提取的来源(第7行): 5 @comment = Comment.find(params[:id]) 6 session[:voting_id] = request.remote_ip 7 voter = Session.find_or_create_by_ip(session[:voting_id]) 8 voter.likes @comment 9 flash[:message] = ‘Thanks for voting!’ 10 respond_to do |format| 我跟踪了post中的所有内容,创建了一个Session模型并将所有代码添加到我的文件中。 这是我的代码: #routes.rb Rails.application.routes.draw do resources :posts do resources :comments do member do post :upvote end end end root “posts#index” end #models: class Post […]

MySQL,Rails ActiveRecord日期分组和时区

我想按创建日期计算用户数。 当我查询我的上一个用户时,我有: > User.last.created_at => Thu, 07 Aug 2014 21:37:55 BRT -03:00 当我计算每个日期的用户时,我得到这个: > User.group(“date(created_at)”).count => {Fri, 08 Aug 2014=>1} 创建日期是8月7日,但结果是8月8日。这是因为组条件是UTC,我的时区是’Brasilia’。 我在我的application.rb有这个: config.time_zone = ‘Brasilia’ config.active_record.default_timezone = :local 怎么解决这个?

在Shell脚本上运行.rb(Ruby)文件

我创建了一个shell脚本来自动化一些进程,类似于: #!/bin/bash ruby RubyFile.rb 但是,当我运行此脚本时,我收到此错误: ruby(2882):不允许操作 谁知道这到底是什么?

这段代码是否在Ruby中创建了循环内存引用?

我有以下假设代码: class User ‘token’, :secret => ‘secret’) # I want to get the user’s posts in thread #12345. user.oauth_consumer.get_posts_in_thread(12345) 我想知道这是否创建了一个循环内存引用,其中user有oauth_consumer的引用,而oauth_consumer有一个对user的引用,因此无法进行垃圾回收? 或者底层GC实现(REE 1.8.7)是否处理​​这种情况?

Rails测试失败,使用Sqlite3

当我在rails中运行我的测试时,我似乎遇到了一个奇怪的错误,它们都因为同样的原因而失败,并且没有一个在线文档对于这个特定错误似乎特别有用: SQLite3::SQLException: cannot rollback – no transaction is active 这个错误削弱了我测试​​我的应用程序的能力,似乎突然出现了。 我有最新版本的sqlite3(3.6.2),最新的sqlite3-ruby(1.2.4)gem和最新的rails(2.1.1)。

RubyMine 3.1上的Rails 2.x应用程序

我已升级到rubymine 3.1,现在rubymine将所有项目作为rails 3项目,但我的不是。 你有没有人面对这个问题。 当我尝试运行应用程序时,Ruby mine会激活此消息 运行配置错误:找到Rails 2.x启动器而不是Rails 3.x一个。 您需要’/ script / rails’脚本来启动Rails服务器。 请根据Rails 3.x文档更新服务器启动器 我的最终状态是我可以从rubymine运行rails 2.x app server 你知道怎么做吗?

如何在非rails应用程序中使用ActiveRecord创建新的MySQL数据库?

我正在构建一个使用ActiveRecord的非rails纯ruby应用程序。 我想写一个rake文件,为它创建一个数据库和表。 我尝试以下代码 namespace :db do task :create do conn = ActiveRecord::Base.connection create_db = “CREATE DATABASE foo_dev” conn.execute(create_db) end end 但这给了我 ActiveRecord::ConnectionNotEstablished: ActiveRecord::ConnectionNotEstablished 错误。 嗯,这很明显,因为我没有将ActiveRecord连接到任何数据库。 我该怎么办? 编辑:我想创建一个MySQL数据库。

Heroku Ruby版本不升级?

我正在努力改变Heroku上的Ruby版本。 我使用带有rails 4的Ruby 2.0.0。 我的Gemfile有: source ‘https://rubygems.org’ ruby ‘2.0.0’ … Heroku中的路径指向: $ heroku config -s | grep PATH PATH=bin:vendor/bundle/ruby/1.9.1/bin:/usr/local/bin:/usr/bin:/bin 当我推动我的应用程序时,我可以看到: Fetching repository, done. Counting objects: 7, done. Delta compression using up to 4 threads. Compressing objects: 100% (4/4), done. Writing objects: 100% (4/4), 431 bytes | 0 bytes/s, done. Total 4 (delta 3), reused 0 […]

Rails Sti:单路径,不同的控制器

有STI课程: class Page < ActiveRecord::Base belongs_to :user end class FirstTypePage < Page end class SecondTypePage < Page end 每个class级的控制器, class PageController < AplicationCorroller end class FirstTypePageController < PageController end class SecondTypePageController < PageController end 和路线: resources :user resource :page end 如何通过FirstTypePageController处理FirstTypePage,SecondTypePage由单一路径上的SecondTypePageController处理? 即 user / 1 / page / 2由以下处理:FirstTypePageController如果“page 2”类型是“FirstTypePage”,则通过SecondTypePageController如果“page 2”类型是“SecondTypePage”? 更新:我的解决方案: match ‘user/:user_id/page/:action’, :controller=>’page/first_type_page’, […]

为什么回调在Ruby on Rails中使用符号

我很难理解何时何时不在Rails中使用符号。 我知道符号与没有很多方法的字符串差别不大。 我也理解符号是好的键,因为同名的符号占用内存中的一个地址。 我很难理解为什么Rails决定在某些情况下使用符号。 如果我有回调 before_action :ask_stack_overflow_question def ask_stack_overflow_question puts “why did I just use a symbol?” end 我不太明白为什么把方法当作符号? 为什么我需要保持方法不变? 我在文档中找不到任何答案。 谢谢