显示随机数据库项目以查看 – Rails

我已经研究了这个问题的答案了一段时间,但无法弄明白。 我正在尝试构建一个显示数据库中随机项的视图(我最终将通过按钮单击进行未来的实现)。 我不知道什么是错的。 我有一个名为Answers的脚手架,只显示文字。 在我的Answers Controller中,我构建了一个名为random的自定义方法: def index @answers = Answer.all end def show end def random # @randitem = @items[rand(items.count)] @randanswer = Answer.random_answer end 在我的模特中: class Answer < ActiveRecord::Base def self.random_answer Answer.order("RAND()").first end end 最后,在我的观点/ random.html.erb中: 文件夹截图: 我只是以我的智慧结束,这令人沮丧,因为我觉得这是一个非常简单的问题。 任何建议或有用的资源将不胜感激。

searchkick索引相关的模型字段

我有一个rails应用程序,我正在从Sphinx切换到ElasticSearch并使用gem searchkick。 我有一个模型教师和模型标签(通过gem),教师可以有多个标签关联。 在Teacher模型中,我已经定义了这样的索引: def search_data { name: name, intro: intro, bio: bio, tag_name: tags.name } end Name,intro和bio是教师属性,但我想索引与教师关联的标签的名称。 我怎样才能做到这一点? 它现在的方式,它索引对象的名称(关系),我如何索引标签对象内的属性名称?

Mac OS X Lion – mysql:找不到命令

我从这个源安装了MySQL,安装成功完成。 但是当我在终端中运行哪个mysql时 ,输出为空。 当mysql时 ,输出是 -bash:mysql:找不到命令 另外,我更新了bundle并运行了应用程序 – 结果: /Users/adam/.rvm/gems/ruby-1.9.3-p385/gems/mysql2-0.3.11/lib/mysql2.rb:9:in `require’: Incorrect MySQL client library version! This gem was compiled for 5.5.28 but the client library is 5.6.12. (RuntimeError) 我可以请你帮忙解决这个问题吗? 先感谢您 编辑:我也尝试将gem安装到mysql目录,如 gem install mysql2 — –with-mysql-config=/usr/local/Cellar/mysql/5.5.28/bin/mysql_config 但不幸的是同样 – 成功安装,但当我运行应用程序时,仍然是同样的错误

ActiveRecord Sum Max连接导致具有多个nil id的数组

与Rails activerecord相同的问题:sum,max和join 除了得到奇怪的结果。 class CustomResumeline < ActiveRecord::Base has_many :resumeline_words end class ResumelineWord < ActiveRecord::Base belongs_to :custom_resumeline end CustomResumeline.joins(:resumeline_words).where(resumeline_words: {name: ["time", "data"]}).select('sum(resumeline_words.weight) as nb_votes').group('resumeline_words.custom_resumeline_id').order('nb_votes ASC') 结果 CustomResumeline Load (0.8ms) SELECT sum(resumeline_words.weight) as nb_votes FROM “custom_resumelines” INNER JOIN “resumeline_words” ON “resumeline_words”.”custom_resumeline_id” = “custom_resumelines”.”id” WHERE “resumeline_words”.”name” IN (‘time’, ‘data’) GROUP BY resumeline_words.custom_resumeline_id ORDER BY nb_votes ASC => #<ActiveRecord::Relation […]

是否可以从事务块内重新引发ActiveRecord :: Rollbackexception?

在我的rails应用程序中,我在控制器的创建操作中有一个事务块,我尝试在其中创建两个记录。 def create ActiveRecord::Base.transaction do @foo = Foo.new(params[:foo].except(:bar)) raise ActiveRecord::Rollback unless @foo.save @bar = Bar.new(params[:foo][:bar]) raise ActiveRecord::Rollback unless @bar.save end end 我希望能够从Rollback中解救,这样我就可以返回一个错误,指出哪个保存失败了。 rescue ActiveRecord::Rollback => e @foo.errors = e if @foo.errors.blank? ensure respond_with @foo end 但是,我从未进入救援区。 我假设这是因为,正如rails文档所述,事务块会占用Rollbackexception并且不会重新引发它。 有没有办法强制重新引发此exception?

具有多态关联的灯具不起作用

我正在尝试实现Rolify gem但是无法添加具有范围的灯具。 下面的(模型)测试的最后一行失败,因为目前主持人角色似乎全局给予@user ,而不是仅给予组织1。 下面的灯具没有使用resource_id和resource_type ,它们在gem文件中提到了夹具,但我不知道如何使用它们。 如何将主持人角色的范围设置为仅限组织1? roles.yml moderator: id: 1 resource: one (Organization) users.yml里 one: email: example@example.com roles: moderator, organizations(:one) # I was hoping this would set the scope of the role to organization one but it isn’t (seems to set the role globally). test.rb def setup @moderator_role = roles(:moderator) @organization1 = organizations(:one) @organization2 […]

Ruby on Rails 3:如何分别检索POST和GET参数?

我知道你可以使用params获取参数,但问题是它合并了所有参数,无论它们是通过GET还是POST发送的: 如果您通过GET发送请求,没问题,参数只能在URL中挤压。 但是如果你发送一个像/blabla?foo=bar&foo2=bar2这样的URL的POST请求,有没有办法告诉通过URL发送的变量和通过实际POST方法发送的变量之间的区别?

Rails + Authlogic @current_user问题 – “显示”方法不再有效

我在我的rails应用程序中实现了Authlogic,它的工作和测试非常好。 用户可以登录,注销等 但是,由于我在users_controller中更改了我的方法以使用@current_user变量,因此我的用户方法或表单(除了“索引”奇怪的……)都不起作用! 例如,这个“显示”方法可以工作并显示任何用户的用户配置文件(注意:我想最终以某种方式将此限制为当前登录的用户): def show @user = User.find(params[:id]) end 如果我按照Authlogic rails cast和官方示例中所示的show方法执行: def show @user = @current_user end 我得到各种undefined method METHOD for nil:NilClass错误。 尽管@current_user变量不包含任何用户信息。 我假设@current_user从current_user方法中提取信息,如下面的应用程序控制器中所定义: def current_user return @current_user if defined?(@current_user) @current_user = current_user_session && current_user_session.record end 我错过了什么? 我的预感是用户模型的User对象和current_user方法之间没有映射。 任何帮助将不胜感激! 提前致谢! 〜丹

Rails教程:RSpec测试解耦

我正在尝试在Michael Hartl的Ruby on Rails教程中 进行第8.5章练习2 。 演习如下: 按照第8.3.3节中的示例,浏览用户和身份validation请求规范(即当前在spec / requests目录中的文件)并在spec / support / utilities.rb中定义实用程序函数,以将测试与实现分离。 额外功劳:将支持代码组织到单独的文件和模块中,并通过在规范帮助文件中正确包含模块来使一切工作正常。 例8.3.3:utilities.rb include ApplicationHelper def valid_signin(user) fill_in “Email”, with: user.email fill_in “Password”, with: user.password click_button “Sign in” end RSpec::Matchers.define :have_error_message do |message| match do |page| page.should have_selector(‘div.alert.alert-error’, text: message) end end 定义的valid_signin(user)函数在authentication_pages_spec.rb的以下块中使用,并且工作正常。 describe “with valid information” do let(:user){FactoryGirl.create(:user)} before { valid_signin(user) […]

查看rails在行单击时记录bootstrap模式中的详细信息

我一直坚持这个问题很长一段时间,并查看了几个post,但是我无法完全达到我想要的Rails应用程序。 基本上,我希望能够点击页面上的表格行并弹出一个模式,显示该特定记录的所有信息。 以下是我一直想到/尝试过的场景: 使用一些JS在表行中设置data-link属性,如下所示 HTML: <tr data-link="”> … JS: $(“tr[data-link]”).dblclick(function() { window.location = $(this).data(“link”) }) 这很好地打开了脚手架生成的show path页面,但是我无法修改它以使用模态并且在模态中为孩子提供相同的数据。 使用data-id和JavaScript加载到模态 HTML: <tr data-id="”> … JS: $(function () { $(‘#showModal’).modal({ keyboard: true, backdrop: “static”, show: false, }).on(‘show’, function () { }); $(“.table-striped”).find(‘tr[data-id]’).on(‘click’, function () { debugger; $(‘#showDetails’).html($(” + ‘Kid ID: ‘ + $(this).data(‘id’) + ” + ”)); $(‘#showModal’).modal(‘show’); […]