什么是处理错误的最佳方法,然后找不到ID?我的控制器中有这个代码:
def show
@match = Match.find(params[:id])
end
Run Code Online (Sandbox Code Playgroud)
我在考虑这样的事情:
def show
if @match = Match.find(params[:id])
else
render 'error'
end
end
Run Code Online (Sandbox Code Playgroud)
但我仍然得到:
MatchesController#show中的ActiveRecord :: RecordNotFound
无法使用'id'= 2找到匹配
为什么?
什么是正确的解决方案?
我找到这样的东西:Rails:如何使用Rails控制台列出数据库表/对象?
这条线很好:
ActiveRecord::Base.connection.tables
Run Code Online (Sandbox Code Playgroud)
并返回所有表
但
ActiveRecord::Base.connection.table_structure("users")
Run Code Online (Sandbox Code Playgroud)
生成错误:
ActiveRecord::Base.connection.table_structure("projects")
Run Code Online (Sandbox Code Playgroud)
我觉得
table_structure
Run Code Online (Sandbox Code Playgroud)
不是Postgres方法.
如何在Postgres数据库的Rails控制台中列出表中的所有数据?
我username
在users
桌上有专栏.我可以像这样做一些事情:rails generate migration add_username_to_users username:string:uniq
但我有username
专栏(而且我已经运行了迁移).
那么我怎样才能让username
专栏独一无二呢?
date
我的模型中有字段User
。它应该只有未来的日期(从注册时刻开始)。在模型中验证它的最佳方法是什么?
我想这样的事情:
class User < ActiveRecord::Base
validates :name, presence: true
validates :date, presence: true
validate :future_event
private
def future_event
errors.add(:date, "Can't be in the past!") if date < Time.now
end
end
Run Code Online (Sandbox Code Playgroud)
可以吗?
你能想到一些更简单、更优雅的解决方案吗?
我有简单的rake文件.它将数据从.xls
文件导入数据库.但有些数据无效.然后数据无效停止执行脚本.但是我想跳过这一行并尝试下一行.
我的部分代码:
data.each do |row|
username = row[0].slice!(0..2) + row[1].slice!(0..2) + rand(100).to_s
username.downcase
password = "pass1234"
User.create!(
email: row[2].downcase,
username: username,
first_name: row[0],
last_name: row[1],
expiration_date: row[3],
password: password,
password_confirmation: password)
p 'done'
Run Code Online (Sandbox Code Playgroud)
结束
错误是关于验证,例如:
rake aborted!
ActiveRecord::RecordInvalid: Validation failed: Expiration date must be after 2015-10-27
Run Code Online (Sandbox Code Playgroud)
脚本已停止,并且未添加有效记录.
我有rails应用程序和本地一切正常(服务器启动等)但是当我不想在heroku上运行我的应用程序时我有问题(当我键入git push heroku master
:
remote:安装capybara 2.4.4 remote:remote:
Gem :: Ext :: BuildError:ERROR:无法构建gem native扩展.remote:remote:
/tmp/build_e1c43ae12e0eb81663efdbfcf4917456/vendor/ruby-2.1.5/bin/ruby extconf.rb remote: *extconf.rb failed* remote:
由于某些原因无法创建Makefile,可能缺少必要的远程:库和/或标题.检查mkmf.log文件以获取更多详细信息.您可以远程:需要配置选项.remote:remote:提供配置选项:remote: - with-opt-dir remote:
--without-opt-dir remote: - with-opt-include remote: - without-opt-include = $ {opt-dir}/include remote: - with-opt-lib remote: - without-opt -lib = $ {opt-dir}/lib remote: -
with-make-prog remote: - without-make-prog remote:--srcdir =.remote:--curdir remote:--ruby =/tmp/build_e1c43ae12e0eb81663efdbfcf4917456/vendor/ruby-2.1.5/bin/ruby remote: - with-gl-dir remote: - with-gl-dir remote:--
with -gl-include remote: - without-gl-include = $ {gl-dir}/include remote: - with-gl-lib remote: - without-gl-lib = $ {gl-dir}/lib remote …
我有HTML代码
<form id="answers">
blue
<input id="0" type="radio">
red
<input id="1" type="radio">
pink
<input id="2" type="radio">
<input type="submit">
</form>
Run Code Online (Sandbox Code Playgroud)
我想在新行中包含每个元素。我不想使用<br>
。在CSS文件中,我尝试执行此操作:display: block
但没有任何更改。