这里似乎有一个类似的问题,但没有人能够回答。似乎也有些不同。
我正在从 Rails 4.2.4 升级到 Rails 5.0.0.1。当我尝试在 30 天后运行密码过期时,它会执行更新操作,其中第二行出现问题:
def update
resource.extend(Devise::Models::DatabaseAuthenticatablePatch)
if resource.update_with_password(resource_params)
warden.session(scope)['password_expired'] = false
set_flash_message :notice, :updated
sign_in scope, resource, :bypass => true
redirect_to stored_location_for(scope) || :root
else
clean_up_passwords(resource)
respond_with(resource, action: :show)
end
end
Run Code Online (Sandbox Code Playgroud)
这是运行的模块:
module Devise
module Models
module DatabaseAuthenticatablePatch
def update_with_password(params, *options)
new_password = params[:password]
new_password_confirmation = params[:password_confirmation]
result = if new_password.present? && new_password_confirmation.present?
update_attributes(params, *options)
else
self.assign_attributes(params, *options)
self.valid?
self.errors.add(:password, new_password.blank? ? :blank : :invalid)
self.errors.add(:password_confirmation, new_password_confirmation.blank? ? :blank : :invalid)
false
end …Run Code Online (Sandbox Code Playgroud) 我有这个确切的错误消息:undefined method to_model\' for #<Onduleur::ActiveRecord_Relation:0x007f8bcf631110>
我错过了一些东西......但是什么?\n感谢您的帮助:)
\n\n这是views/installations/_form.html.erb中的搜索表单
\n\n<%= simple_form_for :query, url: @onduleurs, method: :get, wrapper: :inline_form, html: {class: \'form-inline\'} do |f| %>\n <%= f.input :identifier, collection: Onduleur.group(:identifier).map {|o| o.identifier} , prompt: "Choississez un onduleur" %>\n <%= f.button :submit, "Chercher", class:"btn btn-success" %>\n <%= link_to "Voir tous les onduleurs", {controller: "installations", action: \'show\'}, class: "btn btn-success" %>\n<% end %>\nRun Code Online (Sandbox Code Playgroud)\n\n这是我的Installations_controller.rb
\n\n class InstallationsController < ApplicationController\n\n def index\n @installations = Installation.all\n end\n\n def show\n filter_onduleurs …Run Code Online (Sandbox Code Playgroud) 我正在使用 Rails 5。我正在尝试找出一种方法,根据我从锚标记的 href 属性中删除的值来获取绝对 URL。我想出了
url = a.attr("href")
if url !~ /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix
url = "http://#{url}"
end
Run Code Online (Sandbox Code Playgroud)
认为如果 href 只是“www.mydomain.com”,我可以通过附加“http://”来创建正确的 URL。然而,如果URl是相对的(例如“/abc/def”),则上述逻辑失败。有没有一种万无一失的方法可以从锚标记的 HREF 属性构建绝对 URL,该属性本身可能是也可能不是绝对 URL?请注意,我确实可以访问包含带有锚标记的页面的原始 URL。
编辑: 但是你是说,嘿,对不起,SOB,这不是浏览器处理 href 的方式。如果它看到“www.whatever.com”,它会将其视为相对路径!所以你的问题是假的!好吧,纸杯蛋糕,尽管你是对的,但这不是我的问题。我想识别一个域并将其转换为有效的 URL,并识别以“/”开头的路径并将其转换为有效的 URL。你说不可能?以此作为你的答案,当其他人同意你的观点时,你的分数就会上升。
我在使用 Ubuntu + Apache +Passenger 时遇到此错误。问题是我没有使用任何数据库或模型。
undefined method `has_one_attached' for #<Class:0x0000555c6ad94fd8> (NoMethodError)
/usr/local/rvm/gems/ruby-2.5.1/gems/activerecord-5.2.1/lib/active_record/dynamic_matchers.rb:22:in `method_missing'
/usr/local/rvm/gems/ruby-2.5.1/gems/activestorage-5.2.1/app/models/active_storage/blob/representable.rb:7:in `block in <module:Representable>'
/usr/local/rvm/gems/ruby-2.5.1/gems/activesupport-5.2.1/lib/active_support/concern.rb:122:in `class_eval'
/usr/local/rvm/gems/ruby-2.5.1/gems/activesupport-5.2.1/lib/active_support/concern.rb:122:in `append_features'
/usr/local/rvm/gems/ruby-2.5.1/gems/activestorage-5.2.1/app/models/active_storage/blob.rb:23:in `include'
/usr/local/rvm/gems/ruby-2.5.1/gems/activestorage-5.2.1/app/models/active_storage/blob.rb:23:in `<class:Blob>'
/usr/local/rvm/gems/ruby-2.5.1/gems/activestorage-5.2.1/app/models/active_storage/blob.rb:16:in `<main>'
/usr/local/rvm/gems/ruby-2.5.1/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
/usr/local/rvm/gems/ruby-2.5.1/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
/usr/local/rvm/gems/ruby-2.5.1/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/loaded_features_index.rb:83:in `register'
/usr/local/rvm/gems/ruby-2.5.1/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
/usr/local/rvm/gems/ruby-2.5.1/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
/usr/local/rvm/gems/ruby-2.5.1/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:287:in `block in require'
/usr/local/rvm/gems/ruby-2.5.1/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:253:in `load_dependency'
/usr/local/rvm/gems/ruby-2.5.1/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:287:in `require'
/usr/local/rvm/gems/ruby-2.5.1/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:374:in `block in require_or_load'
/usr/local/rvm/gems/ruby-2.5.1/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:37:in `block in load_interlock'
/usr/local/rvm/gems/ruby-2.5.1/gems/activesupport-5.2.1/lib/active_support/dependencies/interlock.rb:14:in `block in loading'
/usr/local/rvm/gems/ruby-2.5.1/gems/activesupport-5.2.1/lib/active_support/concurrency/share_lock.rb:151:in `exclusive'
/usr/local/rvm/gems/ruby-2.5.1/gems/activesupport-5.2.1/lib/active_support/dependencies/interlock.rb:13:in `loading'
/usr/local/rvm/gems/ruby-2.5.1/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:37:in `load_interlock'
/usr/local/rvm/gems/ruby-2.5.1/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:352:in `require_or_load'
/usr/local/rvm/gems/ruby-2.5.1/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/active_support.rb:48:in `block in require_or_load'
/usr/local/rvm/gems/ruby-2.5.1/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/active_support.rb:16:in `allow_bootsnap_retry'
/usr/local/rvm/gems/ruby-2.5.1/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/active_support.rb:47:in `require_or_load'
/usr/local/rvm/gems/ruby-2.5.1/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:330:in `depend_on'
/usr/local/rvm/gems/ruby-2.5.1/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/active_support.rb:82:in `depend_on' …Run Code Online (Sandbox Code Playgroud) 在我的 Rails 5 应用程序中,我有这样的内容:
a = [1,2,3]
a.map do |entry|
entry.delete if condition == true
end
Run Code Online (Sandbox Code Playgroud)
如果条件为真,则删除该条目。
现在我有这个:
a = [[1,2],[2,3],[3,4]]
a.map do |entry|
entry.delete if condition == true
end
Run Code Online (Sandbox Code Playgroud)
这循环遍历 a 但现在entry是一个数组,删除应该删除整个数组entry,但我得到:
wrong number of arguments (given 0, expected 1)
Run Code Online (Sandbox Code Playgroud)
有谁知道我如何循环遍历数组并删除整个子数组?
当我搜索时,这是一个常见问题,但我发现没有一个答案适合我的情况。我已经使用设计建立了一个用户模型,它有两个相关的模型,它有一个详细联系信息和许多地址。嵌套表单适用于地址,但未显示我的联系方式详细信息字段。
我的用户模型如下:
validates_presence_of :contact_detail, :addresses
# Include default devise modules. Others available are:
# :lockable, :timeoutable, :trackable and :omniauthable
devise :invitable, :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable, :confirmable
has_one :contact_detail, dependent: :destroy
has_many :addresses, dependent: :destroy
accepts_nested_attributes_for :addresses,
allow_destroy: true
accepts_nested_attributes_for :contact_detail,
allow_destroy: true
Run Code Online (Sandbox Code Playgroud)
联系方式型号只有belongs_to :user
我在我的应用程序控制器上进行了 devise gem 中提到的更改:
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [addresses_attributes: [:street_name, :street_number, :city, :country, :postal_code, :name],
contact_detail_attributes: [:first_name, :last_name, :description, :telephone, :mobile ]])
end
end
Run Code Online (Sandbox Code Playgroud)
我的 …
我有一个包含 180,000 个单词的大词典,需要将其加载到数据库中才能运行我的应用程序,并且对测试很有用。不幸的是,这需要大约 30 分钟来为数据库设置种子。无论如何,是否只为数据库播种一次,或者甚至只为数据库的一个表播种并允许每次运行刷新其他表?
编辑:我最终使用activerecord-import来大大加快播种过程。现在需要 16 秒而不是半小时。我还注意到,在我的/spec/rails_helper.rb文件中,我有以下内容:
config.before(:suite) do
Rails.application.load_seed # loading seeds
end
Run Code Online (Sandbox Code Playgroud)
很明显我很久以前就已经添加了它并且忘记了它,因为这是我使用的一种模板 rails_helper 文件。注释掉这意味着我不会每次都运行它,如果我确实需要重新播种,只需取消注释即可。
出于某种原因,我错误地认为 rspec 只是默认种子,事实并非如此。
我正在向方法发送可选参数,但未收到它们。使用 binding.pry 我已经检查过但未收到链接参数,但在 send_email 方法中收到了 id 参数。它总是作为零返回。请帮忙找出我出错的地方
class EmailsController < MyAccountController
def send_emails
@user = current_user
id = @user.id
HiringMailer.send_email(id, link: true).deliver_now
end
end
class HiringMailer < ApplicationMailer
def send_email(id, joining = false, options={})
link = options[:link]
binding.pry_remote
@user = User.find(id)
@joining_user = joining
to = (['abc@yah.com', 'adx@yah.com']).flatten
mail to: to, subject: "Joining Date"
end
end
Run Code Online (Sandbox Code Playgroud)
更新 1
HiringMailer.send_email(id, link: true).deliver_now
def send_email(id, joining = false, , **options)
binding.pry_remote
end
Run Code Online (Sandbox Code Playgroud) 我想要一个 Rails 应用程序支持的 URL 列表,但我不想提前提供任何参数。
例如,我想列出user_registration_pathas的路径/%{locale}/users/sign_up。
我可以获得一个命名路由列表,如下所示(在 Rails 控制台中测试):
Rails.application.routes.named_routes.helper_names
Run Code Online (Sandbox Code Playgroud)
输出示例:
["rails_info_properties_path", "rails_info_routes_path", "rails_info_path", "rails_mailers_path", "rails_service_blob_path", "rails_blob_path", "rails_blob_representation_path", "rails_representation_path", "rails_disk_service_path"...]
Run Code Online (Sandbox Code Playgroud)
Rails 有什么办法可以实现这一点吗?
我正在尝试检索特定部门内的所有员工姓名.
这是我做的:
Employee.find_by(department:'IT').name
Run Code Online (Sandbox Code Playgroud)
但它只返回第一个值.
这是我在控制台中得到的:
SELECT "employee".* FROM "employee" WHERE "employee"."department" = $1 LIMIT $2 [["department","IT"], ["LIMIT", 1]]
Run Code Online (Sandbox Code Playgroud)
为何选择LIMIT 1?
有没有办法实现类似于以下内容的逻辑:
Route::get('/', function () {
return "Hello, world!";
});
Run Code Online (Sandbox Code Playgroud)
在Rails应用程序中?因此,该请求GET 127.0.0.1/将仅返回纯文本“ Hello,world!”。。
是否有比下面更简洁的方法来检查数组是否仅包含String类对象?
a = ['one','twon','three'] # => ["one", "twon", "three"]
b = ['one','twon', 1 ] # => ["one", "twon", 1]
c = ['one','twon', {one: 1} ] # => ["one", "twon", {:one=>1}]
def contains_only_string_class(object)
uniq_a= object.map{ |i| i.class }.uniq # => [String], [String, Integer], [String, Hash]
uniq_a.include?(String) & (uniq_a.length == 1) # => true, false, false
end # => :contains_only_string_class
contains_only_string_class(a) # => true
contains_only_string_class(b) # => false
contains_only_string_class(c) # => false
Run Code Online (Sandbox Code Playgroud) ruby-on-rails-5 ×12
ruby-on-rails ×11
ruby ×6
devise ×2
activerecord ×1
apache ×1
database ×1
href ×1
http ×1
nested-forms ×1
passenger ×1
rest ×1
routes ×1
ubuntu-18.04 ×1
url ×1