我的webapp中有管理员和普通用户.我想根据他们的身份使他们的根(/)不同.从许多不同的页面访问根,所以如果我能在routes.rb文件中实现这一点会容易得多.这是我目前的档案.
ProjectManager::Application.routes.draw do
root :to => "projects#index"
end
Run Code Online (Sandbox Code Playgroud)
有人可以把我链接到一个可以告诉我进入方向的例子吗?有没有办法将逻辑放入路径文件?谢谢你的帮助.
当我对内置类进行子类化时,为什么检查中的行为会发生变化.但是,当我将自定义子类化为子类时,却看不到.
class MainError
end
class AnotherTestError < StandardError
def initialize
@label_test = "hey!"
end
end
class TestError < MainError
def initialize
@label_test = "hey!"
end
end
a = AnotherTestError.new
puts a.inspect # output: #<AnotherTestError: AnotherTestError>
t = TestError.new
puts t.inspect # output: #<TestError:0x007f99e12409f0 @label_test="hey!">
Run Code Online (Sandbox Code Playgroud) 我有一个使用HTML,CSS和JS制作的聊天窗口.我想从下到上定位消息.示例:底部的第一个消息div,第一个上面的第二个消息div,依此类推.可能吗?
我知道这个答案在这里,但我一直无法找到它(或者至少在我看到它时就认出来了!).我是jquery的亲戚,所以请耐心等待.
我有20张图片,名为1.png 到20.png.我想在每次用户点击按钮时随机显示不同的图像.
我生成随机数的javascript代码如下所示:
var randomImage = 1 + Math.floor(Math.random() * 20);
Run Code Online (Sandbox Code Playgroud)
我想要做的是将结果传递给我的HTML文档作为图像的名称,所以这是这样的:
<img id="randImg" class="img_answer" src="randomImage.png">
Run Code Online (Sandbox Code Playgroud)
我尝试连接,但似乎无法解决这个问题.我是否需要创建一个影响img ID或类的函数?谁能指出我最干净的方法呢?
目前我正在为我的项目进行表自定义.我正在使用Twitter Bootstrap rails gem(2.2.6).但是我认为这不应该是CSS定制的问题.
所以基本上我想实现以下模型:

然而,当我把半径边框上没有任何反应,除非我float这left或块显示它,我没有看到圆角边框.请查看以下jsfiddle:http:
//jsfiddle.net/zaEFz/2/
但问题是:当浮动内容时,我松开了表格的结构.这意味着标题不再与内容匹配.这么几个问题:
如何将内容与标题对齐?如果使用float:left
如何围绕每一行的角落?如果不使用float:left和display:block
如果你回答其中一个应该没问题的问题:)
这应该很容易......我正在尝试将css文件中的3个十六进制数字与ruby匹配.这就是我所拥有的......
File.open(ARGV[0], 'r') do |source|
source.each { |line|
puts line if line =~ /\h{3}/
}
end
Run Code Online (Sandbox Code Playgroud)
这不会在具有多个此类值的文件中返回任何内容.如果我改变线,line =~ /\h/那么几乎每一行都会被返回.我知道我必须遗漏一些基本的东西,但它是什么?
编辑.这是一些示例输入.有效的十六进制颜色当然可以是三个十六进制值组合,但是现在我只关注六个值的组合.
#captcha fieldset{border-top:1px solid #c0c0c0;border-bottom:1px solid#c0c0c0;margin:0;padding:10px}
#captcha legend{color:gray}
#captcha .divider{display:none}
#captcha .captcha_refresh{font-size: 9px;color:gray}
#captcha .captcha_other_options{padding-top:5px;font-size: 9px}
#captcha .recaptcha_text{font-size: 11px;line-height:16px}
#captcha .captcha_optout{font-size: 11px;padding:10px 0 5px}
#captcha #recaptcha_image{font-weight:bold;margin:10px 0 0 0}
#captcha #recaptcha_image a.recaptcha_audio_cant_hear_link{font-size: 9px;font-weight:normal}
#captcha .captcha_loading{border:0}
#captcha .captcha_image img{border:1px solid #c0c0c0}
#captcha .captcha_input input{direction:ltr;margin-top:4px;width:137px}
#captcha .captcha_input label{margin-right:4px}
.register #captcha .captcha_input label{color:#666;font-weight:bold}
#generic_dialog.captcha .generic_dialog_popup{width:340px}
Run Code Online (Sandbox Code Playgroud) 这里是Rails的新手.关于迁移的几个问题:
我创建了一个我不再需要的迁移.我想删除它.简单的命令是rails destroy migration AddMyColumnToMyModel什么?
假设我错误地输入了我要销毁的迁移名称......这就是当我试图销毁不存在的迁移时会发生什么.
$ rails destroy migration Blah
invoke active_record
remove migration.rb
Run Code Online (Sandbox Code Playgroud)
它说它正在消失migration.rb......这是件坏事吗?
试图找出为什么我的nil检查在调用没有param的方法或者没有产生记录的param id时失败.
@game = Game.where(:id => params[:id]).first
if @game.nil?
redirect_to root_path
end
Run Code Online (Sandbox Code Playgroud)
在控制台中这很好用.
>> pp @game.nil?
=> true
Run Code Online (Sandbox Code Playgroud)
在应用程序中,这失败了(它永远不会重定向!),为什么?
在控制台中(param id为nil或不存在记录值):这有效:
unless Game.exists?(params[:id])
raise('ok')
end
Run Code Online (Sandbox Code Playgroud)
但不是在现实生活中的应用程序:(我几乎用各种方式检查记录是否存在或有效,代码只是通过此检查并继续原样
看看其他一些代码,我注意到我使用了一个返回语句,IT似乎用它解决了这个问题.
作品:
unless Game.exists?(params[:id])
redirect_to root_path
return
end
Run Code Online (Sandbox Code Playgroud)
失败:
unless Game.exists?(params[:id])
redirect_to root_path
end
Run Code Online (Sandbox Code Playgroud)
不太确定为什么它需要在redirect_to explicit之后返回
我在我的Rails 4项目中安装了Fabrication和Faker
我创建了一个fabrarication对象:
Fabricator(:course) do
title { Faker::Lorem.words(5) }
description { Faker::Lorem.paragraph(2) }
end
Run Code Online (Sandbox Code Playgroud)
我在我的courses_controller_spec.rb测试中调用了Faker对象:
require 'spec_helper'
describe CoursesController do
describe "GET #show" do
it "set @course" do
course = Fabricate(:course)
get :show, id: course.id
expect(assigns(:course)).to eq(course)
end
it "renders the show template"
end
end
Run Code Online (Sandbox Code Playgroud)
但由于某种原因,测试在第6行失败:
course = Fabricate(:course)
Run Code Online (Sandbox Code Playgroud)
错误消息是:
Failure/Error: course = Fabricate(:course)
TypeError:
can't cast Array to string
Run Code Online (Sandbox Code Playgroud)
不知道为什么这会失败.有没有人遇到与Faker相同的错误消息?
我一直在尝试根据这些说明安装Ruby on Rails .但是,运行时出现以下错误gem install rails -v 4.2.4:
Fetching: rack-1.6.4.gem (100%)
Successfully installed rack-1.6.4
Building native extensions. This could take a while...
ERROR: Error installing rails:
ERROR: Failed to build gem native extension.
/home/falak/.rvm/rubies/ruby-2.2.3/bin/ruby -r ./siteconf20150909-22683-172bl7d.rb extconf.rb
checking if the C compiler accepts ... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
Provided …Run Code Online (Sandbox Code Playgroud)