我在我的Rails 3应用程序中有一个模型,它有一个date字段:
class CreateJobs < ActiveRecord::Migration
def self.up
create_table :jobs do |t|
t.date "job_date", :null => false
...
t.timestamps
end
end
...
end
Run Code Online (Sandbox Code Playgroud)
我想用随机日期值预填充我的数据库.
生成随机日期的最简单方法是什么?
我使用RVM网站上提到的单个指令安装RVM(使用git).
然后我使用以下命令安装Ruby版本1.9.2和1.8.7:
rvm install 1.9.2
rvm install 1.8.7
Run Code Online (Sandbox Code Playgroud)
但是,我找不到Ruby二进制文件.当我尝试执行命令时,出现以下错误:
[root@server1 support]# rvm use 1.9.2
Using /usr/local/rvm/gems/ruby-1.9.2-p136
[root@server1 support]# ruby
-bash: ruby: command not found
Run Code Online (Sandbox Code Playgroud)
这是输出rvm info:
[root@server1 support]# rvm info
system:
system:
uname: "Linux server1.myserver.com 2.6.18-194.26.1.el5.028stab070.14 #1 SMP Thu Nov 18 16:34:01 MSK 2010 x86_64 x86_64 x86_64 GNU/Linux"
bash: "/bin/bash => GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)"
zsh: " => not installed"
rvm:
version: "rvm 1.2.6 by Wayne E. Seguin (wayneeseguin@gmail.com) [http://rvm.beginrescueend.com/]"
homes:
gem: "not set"
ruby: "not set" …Run Code Online (Sandbox Code Playgroud) 我的路线中定义了资源.
resources :categories
Run Code Online (Sandbox Code Playgroud)
我的类别控制器中有以下内容:
def show
@category = Category.find(params[:id])
respond_to do |format|
format.json { render :json => @category }
format.xml { render :xml => @category }
end
end
Run Code Online (Sandbox Code Playgroud)
控制器动作适用于json和xml.但是,我不希望控制器响应html格式请求.我怎么才能只允许json和xml?这应该只在show动作中发生.
实现这一目标的最佳方法是什么?还有什么好的提示来干掉respond_to块吗?
谢谢你的帮助.
request.fullpath给出fullpath加上传递的任何参数,例如
/page?param1=value¶m2&value
Run Code Online (Sandbox Code Playgroud)
如何才能获得没有参数的页面?喜欢
/page
Run Code Online (Sandbox Code Playgroud)
谢谢.
我在Ruby中使用DSL,其工作原理如下:
desc 'list all todos'
command :list do |c|
c.desc 'show todos in long form'
c.switch :l
c.action do |global,option,args|
# some code that's not relevant to this question
end
end
desc 'make a new todo'
command :new do |c|
# etc.
end
Run Code Online (Sandbox Code Playgroud)
一位开发人员建议我将DSL增强到不需要传递c给command块,因此不需要c.内部的所有方法; 据推测,他暗示我可以使下面的代码工作相同:
desc 'list all todos'
command :list do
desc 'show todos in long form'
switch :l
action do |global,option,args|
# some code that's not relevant to this question
end
end …Run Code Online (Sandbox Code Playgroud) 我正进入(状态
$ rake routes
rake aborted!
ArgumentError: Missing :action key on routes definition, please check your routes.
/usr/local/rvm/gems/ruby-2.1.2/gems/actionpack-4.1.5/lib/action_dispatch/routing/mapper.rb:243:in `default_controller_and_action'
/usr/local/rvm/gems/ruby-2.1.2/gems/actionpack-4.1.5/lib/action_dispatch/routing/mapper.rb:117:in `normalize_options!'
/usr/local/rvm/gems/ruby-2.1.2/gems/actionpack-4.1.5/lib/action_dispatch/routing/mapper.rb:65:in `initialize'
/usr/local/rvm/gems/ruby-2.1.2/gems/actionpack-4.1.5/lib/action_dispatch/routing/mapper.rb:1487:in `new'
/usr/local/r................
Run Code Online (Sandbox Code Playgroud)
这是我的Routes.rb
Rails.application.routes.draw do
get 'script/index'
get 'landing/index'
root 'landing/index'
end
Run Code Online (Sandbox Code Playgroud)
是什么导致了这个问题,我该如何解决它.
我试图在windows中连接localhost:3000
rails server在vagrant中运行(ubuntu 14.10)
portforwarding已完成
什么时候,我lynx 127.0.0.1:3000在ubunutu 尝试
,它的工作原理
但是,在Windows中,我无法连接localhost:3000,127.0.0.1:3000
刚刚显示ERR_CONNECTION_REFUSED
在cmd中,我运行的netstat -t结果是:3000 TIME_WAIT
我怎么解决这个问题?
这是我的'Vagrantfile'
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|
# The most common configuration options are documented and commented below.
# …Run Code Online (Sandbox Code Playgroud) ruby ruby-on-rails vagrant ruby-on-rails-4 ruby-on-rails-4.2
假设我有以下规范:
...
describe Thing do
it 'can read data' do
@data = get_data_from_file # [ '42', '36' ]
expect(@data.count).to eq 2
end
it 'can process data' do
expect(@data[0].to_i).to eq 42 # Fails because @data is nil
end
end
...
Run Code Online (Sandbox Code Playgroud)
我想要的只是在给定的描述或上下文中共享一个变量.我会在一个例子中写一个值,然后在另一个例子中读取它.我怎么做?
有一个奇怪的问题,需要帮助.
我正在尝试在生产服务器上启动rails控制台,它的行为类似于rails c命令不存在.
FWIW,我已经成为一名铁路开发人员已有4年,并且一直在很多其他服务器上这样做而没有问题.在这台服务器上,我可以毫无问题地删除,创建,迁移,播种数据库(使用RAILS_ENV =生产),并且应用程序可以正常运行而不会出现任何问题.
建立:
Ubuntu 14.04(racksapce第二代性能1服务器)
Nginx with Passenger(我通常使用Unicorn,但从未在我使用Passenger部署的任何应用程序上遇到过问题)
Ruby 2.1.5(使用rvm)
Rails 4.1.7
Postgres
Capistrano 3(使用rvm,迁移,资产预编译等扩展)
我尝试过的:
进入app目录:
cd /home/deployer/app_name/current
Run Code Online (Sandbox Code Playgroud)
哪个加载.rvmrc并显示我在正确的gemset中,运行bundle只是为了踢.
rails c production # (which usually works no problem)
bundle exec rails c production # (sometimes have to do this on older apps that do not have the newer capistrano 3 and rvm setup)
rails c production RAILS_ENV=production # (getting desperate here)
RAILS_ENV=production rails c production # (haha, surely this won't work, but out of options)
RAILS_ENV=production …Run Code Online (Sandbox Code Playgroud)