Tag: ruby on rails

Geocoder,当ip为127.0.0.1时如何在本地测试?

我无法让地理编码器正常工作,因为我的本地IP地址是127.0.0.1所以它无法找到我正确的位置。 request.location.ip显示“127.0.0.1” 我怎样才能使用不同的IP地址(我的互联网连接ip),这样可以带来更多相关数据?

database.yml和引用无效

我们刚刚将虚拟机升级到我认为相同的ruby配置(通过RVM …… Ruby 1.9.2,Rails 3.0.7,DataMapper 1.1.0)。 最大的区别是我们从MySQL 5.0到5.1。 出于某种原因,在我们的旧虚拟机上运行的完全相同的代码/ database.yml现在在我们尝试连接到数据库时的新代码上失败了。 问题是这个YAML: mysql_defaults: &mysql_defaults adapter: mysql encoding: UTF-8 username: user password: pass host: localhost development: <<: *mysql_defaults database: devdb production: <<: *mysql_defaults database: productiondb host: master.db.site.com 只是扩展到: “mysql_defaults” => { “adapter”=>”mysql”, “encoding”=>”UTF-8”, “username”=>”user”, “password”=>”pass”, “host”=>”localhost” }, “development” => { “adapter”=>”mysql”, “encoding”=>”UTF-8”, “username”=>”user”, “password”=>”pass”, “host”=>”localhost” }, “production” […]

猴子修补Devise(或任何Rails gem)

我在我的Rails项目中使用Devise身份validationgem,我想更改它在闪存警报中使用的密钥。 (设计使用:通知和:警告闪存键,但我想将它们更改为:成功和:错误,以便我可以使用Bootstrap显示漂亮的绿色/红色框。) 所以我希望能够以某种方式覆盖set_flash_message中的set_flash_message方法。 这是新方法: def set_flash_message(key, kind, options = {}) if key == ‘alert’ key = ‘error’ elsif key == ‘notice’ key = ‘success’ end message = find_message(kind, options) flash[key] = message if message.present? end 但我只是不知道该把它放在哪里。 更新: 基于答案,我使用以下代码创建了config / initializers / overrides.rb文件: class DeviseController def set_flash_message(key, kind, options = {}) if key == ‘alert’ key […]

在Ubuntu 14.04上安装Jekyll

我在Ubuntu上安装Jekyll时遇到了一些问题。 以下是我的一些版本: trevor~$ ruby -v ruby 2.2.4p230 (2015-12-16 revision 53155) [x86_64-linux] trevor~$ gem -v 2.5.1 trevor~$ rvm -v rvm 1.26.11 (latest) by Wayne E. Seguin , Michal Papis [https://rvm.io/] 但是,当我尝试安装Jekyll时,我收到以下错误: trevor~$ gem install jekyll Building native extensions. This could take a while… ERROR: Error installing jekyll: ERROR: Failed to build gem native extension. current directory: […]

仅在root_path上登录后才重定向用户

我的Rails应用程序上有一个root_path ,它不受用户保护,即它是一个简单的门户网站主页,带有登录表单。 用户登录后,我希望它转到dashboard_path 。 我做到了这个: def signed_in_root_path(scope_or_resource) dashboard_path end 这显然应该在用户登录时使用,并且我不希望它转到root_path ,同时仍然保持用户返回到前一页,如果它尝试到达受限区域并且它要么超时还是没有超时登录。 即: restricted_pa​​ge – > login – > restricted_pa​​ge_but_logged_in 我不想改变这种行为,这就是为什么我没有使用after_sign_in_path ,但是如果它在root_path ,或者想要在不需要用户身份validation的任何路由上重定向它。 我的问题是这不起作用。 登录后,我将被重定向回root_path ,我认为这是因为之前触发了after_sign_in_path 。 有没有办法做到这一点? 谢谢! 编辑:这是我第二次登录时工作,即我转到root_path ,登录,获取闪存消息,说明我已登录,并在root_path上的表单上再次输入用户名和密码。 我成功地被重定向到dashboard_path 。 不过,不是我想要的行为。

rails – 在link_to上传递id参数

我正在尝试将当前页面(id)的参数传递给下一页,以便创建依赖模型条目。 即项目有投标,投标属于项目。 所以在项目的展示页面上我添加了链接 这创建并执行url….“http:// localhost:3000 / bid / new.2” 我有 def new @bid = Bid.new @project = Project.find(params[:id]) end 在出价控制器,但我不断收到错误“无法找到没有ID的项目” ??? 怎么回事,为什么我不能通过身份证?

像Twitter粉丝这样的关系/在ActiveRecord中跟随

我试图在我的应用程序中表示用户之间的关系,其中用户可以拥有许多关注者并且可以关注其他用户。 我想有像user.followers()和user.followed_by()这样的东西你能告诉我如何使用ActiveRecord来表示这个吗? 谢谢。

使用Rspec + Capybara在Rails中测试错误页面

在Rails 3.2.9我有自定义错误页面定义如下: # application.rb config.exceptions_app = self.routes # routes.rb match ‘/404’ => ‘errors#not_found’ 哪个效果如预期。 当我在development.rb设置config.consider_all_requests_local = false时,在访问/foo时我得到了not_found视图 但是如何用Rspec + Capybara测试呢? 我试过这个: # /spec/features/not_found_spec.rb require ‘spec_helper’ describe ‘not found page’ do it ‘should respond with 404 page’ do visit ‘/foo’ page.should have_content(‘not found’) end end 当我运行此规范时,我得到: 1) not found page should respond with 404 page Failure/Error: […]

在Ruby / Rails中PHP的print_r是否相同?

在PHP中,您可以: print_r($var)或vardump($var) 它打印有关变量的“人类可读”信息。 Ruby / Rails中是否有等效的函数/帮助器?

Rails 2到Rails 3:使用link_to而不是link_to_remote(包括远程和更新)

我敢肯定,这是一个快速简单的答案。 我正在将Rails项目从版本2升级到版本3,并根据Rails 3更新用link_to替换link_to_remote的加载。 即使是这样简单的事情: :get_time}, :remote=>true, :update=>’current_time’ %> 似乎没有用。 请求(使用get方法)正常,渲染的html是: Check Time Routes.rb条目: get “monitoring/get_time” 正如我所说,我确信这对我来说是一个非常明显的问题!