Tag: ruby

has_many通过复选框

鉴于以下型号 class Feed :destroy has_many :users , :through => :alerts end class Alert < ActiveRecord::Base belongs_to :user belongs_to :feed has_and_belongs_to_many :alerttypes end class Alerttype < ActiveRecord::Base has_and_belongs_to_many :alerts end 我可以创建一个表单来列出添加新Feed时的Alerttypes并正确提交。 但是,我无法成功更新这些Alerttypes。 我似乎无法找到定义复选框命名的正确方法 检查“已检查”的Alerttypes有效,但编辑时这些Alerttypes中的任何更改都不会更新,并且它们将保持为首次创建时的状态。

如何使用rspec测试CLI的stdin

我正在制作一个小型的Ruby程序,无法弄清楚如何编写模拟多个用户命令行输入的RSpec规范(function本身可行)。 我认为这个StackOverflow的答案可能涵盖了最接近我的地方,但这并不是我需要的。 我使用Thor作为命令行界面,但我不认为这是Thor的任何问题。 程序可以从文件或命令行读取命令,并且我已经能够成功编写测试以读取执行它们。 这是一些代码: cli.rb class CLI ” while line = gets break if line =~ /EXIT/i yield [line] print “> ” end end end # .. end 我已经成功测试了使用以下代码执行文件中包含的命令: 投机/ cli_spec.rb describe CLI do let(:cli) { CLI.new } subject { cli } describe “executing instructions from a file” do let(:default_file) { “instructions.txt” } let(:output) […]

无法使用“关注”按钮与Acts_As_Follower一起使用

我正在使用我正在处理的心愿单应用中的Acts_as_follower。 我认为它应该工作,但实际上跟随另一个用户的按钮不是。 跟随按钮位于我的用户索引视图中,我可以在那里看到它,但它没有响应被点击。 任何帮助,将不胜感激。 这是我的代码: index.html.erb followers_controller.rb class FollowersController < ApplicationController before_action :authenticate_user! respond_to :js def create @user = User.find(params[:user_id]) current_user.follow(@user) end def destroy @user = User.find(params[:user_id]) current_user.stop_following(@user) end end users_controller.rb class UsersController < ApplicationController def show @user = User.find(params[:id]) @gifts = @user.gifts end def index @users = User.all if params[:search] @users = User.search(params[:search]).order("created_at DESC") […]

在rails中的will_paginate的page_entries_info中提供自定义消息

我是铁杆新手。 我想为page_entries_info显示我的自定义消息。 我已经通过以下链接但不能理解。 任何人都可以详细解释。 怎么办-I-指定定制-措辞-IN-A-意志PAGINATE视辅助

如何在使用RVM时为Ruby on Rails安装SQlite3

我是编程和Ruby on Rails的新手。 设置我的开发环境一直很糟糕。 我目前的问题是安装Sqlite 3。 当我做 $ bundle install 我明白了 Fetching source index for http://rubygems.org/ Using rake (0.8.7) Using abstract (1.0.0) Using activesupport (3.0.1) Using builder (2.1.2) Using i18n (0.4.2) Using activemodel (3.0.1) Using erubis (2.6.6) Using rack (1.2.1) Using rack-mount (0.6.13) Using rack-test (0.5.7) Using tzinfo (0.3.24) Using actionpack (3.0.1) Using mime-types (1.16) […]

覆盖默认访问者时更新属性的麻烦

我正在使用Ruby on Rails 4,我用这种方式覆盖了一些默认的访问器方法: class Article < ActiveRecord::Base def title self.get_title end def content self.get_content end end self.get_title和self.get_content方法返回一些计算值,如下所示(注意: has_one_association是a :has_one ActiveRecord::Association ) def get_title self.has_one_association.title.presence || read_attribute(:title) end def get_content self.has_one_association.content.presence || read_attribute(:content) end 当我从数据库中找到并读取@article实例时,所有实例都按预期工作: title和content值分别使用self.has_one_association.title和self.has_one_association.content输出。 但是,我发现当属性分配给@article ,@ @article对象不会按预期更新。 也就是说,在我的控制器中,我有: def update # params # => {:article => {:title => “New title”, :content => “New […]

伪造Ruby中的类型转换?

鉴于以下Ruby代码,并且鉴于我有一个Klass实例,如何在Klass实例上调用do_stuff方法。 我想将我的Klass实例转换为Subklass。 我理解类型转换这在Ruby中是不可能的 – 有没有办法伪造它? class Klass … end class Subklass < Klass … def do_stuff … end end inst = Klass.new inst.magically_convert_to_subklass_instance # Need help here inst.do_stuff

为什么bash -l -c“CMD”让ruby找到我的gem?

在我的ruby脚本中,我需要gmail gem: require ‘rubygems’ require ‘gmail’ 在shell中运行时,它可以正常工作: ruby my-script.rb 当我把它放在一个cron作业时,它无法执行: * * * * * cd /to/script/directory;/usr/local/rvm/rubies/ree-1.8.7-2011.03/bin/ruby ./my-script.rb 日志显示无法加载gmail gem: /usr/local/rvm/rubies/ree-1.8.7-2011.03/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require’: no such file to load — gmail (LoadError) 好吧,当我这样做时(把cmd放在bash -l -c’CMD’中): * * * * * /bin/bash -l -c ‘cd /to/script/directory;/usr/local/rvm/rubies/ree-1.8.7-2011.03/bin/ruby ./my-script.rb’ 它再次正常工作。 为什么? ps.I知道arg -l使bash成为登录shell,但这有什么不同吗?

如何在Ruby脚本中为命令shell获取环境变量?

我正在尝试从我的ruby程序中运行一些第三方bash脚本。 在我运行它们之前,它们需要我来源文件。 在命令行上它一切正常但在Ruby中它不起作用。 我发现系统命令将打开一个新的子shell进程,任何源代码都将在那里完成,并且无法从运行Ruby脚本的父shell进程中看到。 当系统调用结束时,子shell也被杀死。 我如何解决这个问题?

对于AWS,如何使用ruby aws-sdk将标记设置为资源?

我很擅长使用亚马逊的ruby-sdk(gem install aws-sdk),并且我只是试图简单地为快照资源创建一个标签。 这就是我正在做的事情: ec2.tags.create(:resource_id => “snap-7d3aa701”, :key => “My Test Tag”, :value => “My test value”) ArgumentError: wrong number of arguments (1 for 2) 注意,ec2 = AWS :: EC2.new(在我设置我的凭据之后)。 我有什么想法我做错了吗? 我无法在网上找到使用ruby aws-sdk进行标记的单一示例。