我正在尝试使用(geocoder gem),将语言环境适当地设置为客户端IP地址.request.location
这就是我所拥有的:
应用程序/控制器/ application_controller.rb
before_action :set_locale
private
def set_locale
# get location with geocoder
location = request.location
# set locale through URL
if params.has_key?(:locale)
I18n.locale = params[:locale] || I18n.default_locale
# set locale through user preference
elsif current_user && current_user.locale.present?
I18n.locale = current_user.try(:locale) || I18n.default_locale
# set locale through geocoder detection of location
elsif location.present? && location.country_code.present? && I18n.available_locales.include?(location.country_code)
if location.country_code.include? "-"
I18n.locale = location.country_code
else
I18n.locale = location.country_code.downcase
end
# set locale through HTTP …Run Code Online (Sandbox Code Playgroud) 在heroku上的Rails 5.1应用程序中加载模式时,会引发以下异常:
ActiveRecord :: StatementInvalid:PG :: SyntaxError:ERROR:"ENGINE"第1行或附近的语法错误第1行:... estamp NOT NULL,"updated_at"timestamp NOT NULL)ENGINE = Inn ...
细节:
跟踪
-- create_table("ads_dashboard_campaigns", {:force=>:cascade, :options=>"ENGINE=InnoDB DEFAULT CHARSET=utf8"})
(5.0ms) DROP TABLE IF EXISTS "ads_dashboard_campaigns" CASCADE
(7.3ms) CREATE TABLE "ads_dashboard_campaigns" ("id" bigserial primary key, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8
rails aborted!
Run Code Online (Sandbox Code Playgroud)
配置/ database.yml的
# # SQLite version 3.x
# # gem install sqlite3-ruby (not necessary on OS X Leopard)
# development:
# adapter: sqlite3
# database: db/development.sqlite3
# pool: 5 …Run Code Online (Sandbox Code Playgroud) 我的 gem 在config/my_gem.yml 中生成以下文件:
test: true
Run Code Online (Sandbox Code Playgroud)
现在我想将test的值加载到我的 gems 模块中以便能够调用:
MyGem.test
=> true
Run Code Online (Sandbox Code Playgroud)
那是我的尝试:
在 Rails 应用程序中 - config/initializers/my_gem.rb:
CONFIG_PATH = "#{Rails.root}/config/my_gem.yml"
MY_GEM = YAML.load_file(CONFIG_PATH)[Rails.env]
Run Code Online (Sandbox Code Playgroud)
在 gem - lib/my_gem.rb 中:
def self.test
::MY_GEM[:test]
end
Run Code Online (Sandbox Code Playgroud)
但是我得到一个例外,因为MY_GEM它没有被识别为一个常量(在lib/my_gem.rb 中)
c:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/my_gem-0.1.0/lib/my_gem.rb:11:in `test': 未初始化的常量 MY_GEM (NameError)
当我User在我的应用程序中创建一个新的时,我运行一个after_create方法然后异步触发Sidekiq工作者.
这个工人......
def perform user_id
@user = ::User.find user_id
# ...
end
Run Code Online (Sandbox Code Playgroud)
...始终返回以下错误:
ActiveRecord :: RecordNotFound:找不到'id'= 315928979197048617的用户
但是使用rails控制台来检查User id是否可以找到:
User.find(315928979197048617)
=> #<User id: 315928979197048617>
Run Code Online (Sandbox Code Playgroud)
这是否会发生,因为新用户的创建需要一些时间,并且工作人员在完成后已经执行了该方法?如果,我将如何解决这种行为?
我正在阅读一个空的 HTML 文件,如下所示:
file = File.read("file_path/file.html", "wb")
Run Code Online (Sandbox Code Playgroud)
为什么会抛出这个 TypeError?
没有将字符串隐式转换为整数
日志:
Completed 500 Internal Server Error in 8ms (ActiveRecord: 0.0ms)
TypeError (no implicit conversion of String into Integer):
app/controllers/abc_controller.rb:49:in `read'
app/controllers/abc_controller.rb:49:in `build'
Run Code Online (Sandbox Code Playgroud) 我有以下用户模型:
class User < ApplicationRecord
# Associations
has_many :issues
# Instances
def has_issues?
issues.resolved.exists?
end
end
Run Code Online (Sandbox Code Playgroud)
和问题模型:
class Issue < ApplicationRecord
# Associations
belongs_to :user
# Scopes
scope :resolved, -> { where(:resolved => true) }
end
Run Code Online (Sandbox Code Playgroud)
如果我现在在我的观点中这样称呼:
unless current_user.has_issues
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Controller#index 中的 NoMethodError
未定义方法 `has_issues' for #<User:0x72dd7c0>
我究竟做错了什么?
我通读了很多文章,讨论了如何BIGINT在Rails中用作主键,但是似乎所有这些都已过时。
如何将BIGINT用作主键,最好仅在全局范围内进行设置。(我知道性能上的差异)
我尝试过的事情:
database ruby-on-rails biginteger primary-key ruby-on-rails-5
我有这个表格:
= form_tag(path, name: "name") do |f|
= hidden_field_tag "abc", content
= submit_tag "" do
%i.icon.ion-android-sync
Run Code Online (Sandbox Code Playgroud)
如您所见,我正在尝试将一个Ionicon放在Form Submit标签内。
我尝试了很多方法,但没有一种起作用。
在给定的示例中,我得到一个空按钮,宽度大约为3px。
是否可以在submit_tag中嵌入孩子?如果可以,您将如何去做?
非常感谢每个答案!
看看这个小提琴
我试图将div #popup放在页面的底部,重叠任何以前的内容和
#content)我不确定,但我认为position: absolute在这种情况下不起作用,至少不是我实现它的方式.
我该怎么办?
我有这个简单的模型:
class Post < ApplicationRecord
after_create_commit :process
before_save :re_process, on: :update
has_one :processed, class_name: 'Post::Process'
def process
self.processed.destroy if self.processed
processed = self.build_processed
processed.save!
end
private
def re_process
self.process if self.title_changed?
end
end
Run Code Online (Sandbox Code Playgroud)
Stack level to deep每当我创建一个新的时候我都会收到错误Post.
现在当我删除before_save :re_process, on: :update一切正常.
这条线不应该只在我更新帖子时受到影响吗?
我在网站上添加了一些页面转换.现在,只要有人点击浏览器后退按钮,将其重定向到他以前的网站,他在该页面上的滚动位置就会自动恢复.如果用户在上一页上向下滚动,则此行为会导致当前窗口中的页面跳转不到一秒,然后才会淡出.
1)我能否延迟此默认浏览器行为?
2)如果1不可能,我是否可以禁用默认行为并手动存储和恢复滚动位置?
我有这个查询:
includes(:foo).where(foo: { price: 0 })
Run Code Online (Sandbox Code Playgroud)
我从一个名为Recipe.
Foo 有十进制类型列price:
>> Recipe.first.foo
=> #<Foo id: 1, price: #<BigDecimal:928b000,'0.0',9(18)>, slug: "something", created_at: "2017-12-05 07:54:29", updated_at: "2017-12-05 13:00:51">
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
Mysql2::Error: 'where 子句'中的未知列 'foo.price': SELECT
recipes。idAS t0_r0,recipes.foo_idAS t0_r1,recipes.parent_recipe_idAS t0_r2,recipes.nameAS t0_r3,recipes.descriptionAS t0_r4,quantities.slugAS t0_r5,recipes.created_atAS t0_r6,quantities.updated_atAS t0_r7,foos.idAS t1_r0, …
ruby-on-rails ×10
ruby ×8
css ×2
activerecord ×1
back ×1
before-save ×1
biginteger ×1
constants ×1
database ×1
file ×1
firefox ×1
forms ×1
geocoding ×1
heroku ×1
html ×1
javascript ×1
locale ×1
methods ×1
mysql ×1
postgresql ×1
primary-key ×1
readfile ×1
rubygems ×1
scope ×1
sidekiq ×1
sql ×1
stack-level ×1