Tag: 的Ruby on 轨道

独特的响应rails gem

我想确定访问者是否已提交表单。 我在想做几件事情: 曲奇饼 IP地址 需要登录(因为注册障碍可能会阻止访问者,因此更不可取) 这有一个RORgem吗? 如果是这样,请发布链接。 谢谢

如何在Rails 4中迭代单行中的关系?

我有一个User模型has_many :tasks并希望做这样的事情: User.find([100,97]).tasks.each { |task| puts task.id } 但这似乎不起作用。 我试过了 User.find([100,97]).map(&:tasks).each { |task| puts task.id } User.find([100,97]).each.tasks.each { |task| puts task.id } 这可能吗? 我该怎么办?

rake db:migrate aborted

我正在阅读rails教程书,在第6章第199页我必须做一个rake db:migrate,但是我收到此错误: 耙子流产了! nil的未定义方法“先决条件”:NilClass (通过使用–trace运行任务来查看完整跟踪) 当我尝试使用rake db:migrate –trace时,我收到以下错误: rake aborted! undefined method `prerequisites’ for nil:NilClass /Users/jeanosorio/.rvm/gems/ruby-1.9.2-p290/gems/rspec-rails-2.0.1/lib/rspec/rails/tasks/rspec.rake:3:in `’ /Users/jeanosorio/.rvm/gems/ruby-1.9.2-p290/gems/rspec-rails-2.0.1/lib/rspec-rails.rb:10:in `load’ /Users/jeanosorio/.rvm/gems/ruby-1.9.2-p290/gems/rspec-rails-2.0.1/lib/rspec-rails.rb:10:in `block in ‘ /Users/jeanosorio/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/railtie.rb:183:in `call’ /Users/jeanosorio/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/railtie.rb:183:in `block in load_tasks’ /Users/jeanosorio/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/railtie.rb:183:in `each’ /Users/jeanosorio/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/railtie.rb:183:in `load_tasks’ /Users/jeanosorio/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/engine.rb:396:in `block in load_tasks’ /Users/jeanosorio/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/application/railties.rb:8:in `each’ /Users/jeanosorio/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/application/railties.rb:8:in `all’ /Users/jeanosorio/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/engine.rb:396:in `load_tasks’ /Users/jeanosorio/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/application.rb:103:in `load_tasks’ /Users/jeanosorio/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/railtie/configurable.rb:30:in `method_missing’ /Users/jeanosorio/ror/sample_app/Rakefile:7:in `’ /Users/jeanosorio/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/rake_module.rb:25:in `load’ /Users/jeanosorio/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/rake_module.rb:25:in `load_rakefile’ /Users/jeanosorio/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/application.rb:495:in `raw_load_rakefile’ […]

在我的rails应用程序中实现searchkick和typeahead不起作用

我已经尝试按照本教程https://www.driftingruby.com/episodes/searchkick-and-elasticsearch#show_notes在我的应用程序中实现searchkick和typeahead.js,但不知何故它无法正常工作。 如果我输入整个名称“lito”,则输入类型根本不起作用并且搜索有效,但如果我只输入“li”则不返回用户。 图像1 图像2与本教程的一个明显区别是我没有在工具/资产中保存typeahead捆绑包,因为该文件夹不会在我的rails应用程序中退出,而是通过命令yarn add typeahead然后在javascript / application.js中退出我做了:“ortort typeahead”。如果您需要代码的任何其他部分,请告诉我。 提前致谢 :) class ProfilesController < ApplicationController def index search = params[:term].present? ? params[:term] : nil @users = if search User.search(search) else User.all end end def autocomplete render json: User.search(params[:query], { fields: ["first_name", "last_name", "username"], match: :word_start, limit: 10, load: false, misspellings: {below: 5} }).map(&:username) end def […]

回形针与Heroku失败

使用Paperclip在本地计算机上运行图像上传有什么可能的影响,但在部署到Heroku时却没有? 当它部署到Heroku时,图像将无法保存。

尝试加载gem’sass-rails’时出错。 (捆扎机:: GemRequireError)

我在启动rails服务器时遇到错误。 C:\Ruby22-x64\WebAppRails>rails s C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.11.2/lib/bundler/runtime.rb:80:in `rescue in block (2 levels) in require’: There was an error while trying to load the gem ‘sass-rails’. (Bundler::GemRequireError) from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.11.2/lib/bundler/runtime.rb:76:in `block (2 levels) in require’ from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.11.2/lib/bundler/runtime.rb:72:in `each’ from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.11.2/lib/bundler/runtime.rb:72:in `block in require’ from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.11.2/lib/bundler/runtime.rb:61:in `each’ from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.11.2/lib/bundler/runtime.rb:61:in `require’ from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.11.2/lib/bundler.rb:99:in `require’ from C:/Ruby22-x64/WebAppRails/config/application.rb:7:in `’ from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:78:in `require’ from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:78:in […]

两个日期轨道之间的validation

我正在尝试创建预约/预约预约,用户选择开始日期和结束日期两个日期,如何从开始日期和结束日期validation日期,并validation这两个日期之间的日期, 防止用户再次选择开始日期,结束日期和日期之间的日期。 数据库 date_start | date_end 14/11/2017 14/18/2017 视图: 日期的日期字段 模型 class Reservation < ApplicationRecord belongs_to: user validates :date_start, uniqueness: true validates :date_end, uniqueness: true end 我使用了validates :date_start, uniqueness: true和validates :date_end, uniqueness: true但它只检查日期开始和date_end但它不检查它们之间的日期。

Rails link_to路径

我想在Rails 4中创建一个应用程序。 我有3个模型:Project,Project_Question,Project_Answer 协会是: 项目: has_many :project_questions 并接受项目问题的嵌套属性。 项目问题: belongs_to :project has_one :project_answer 并接受Project Answers的嵌套属性。 我的路线嵌套如下: resources :projects do resources :project_questions do resources :project_answers end end 在我的项目问题部分,我想要一个链接来回答问题。 我试过了: project_question.id) %> 我的路由文件中有一个具有该名称的路由,但是我收到此错误消息: undefined local variable or method `project_question’ for #<#:0x0000010f810b68> 括号内应该怎么做? 视图: project_question.id) %> 项目问题控制器: class ProjectQuestionsController < ApplicationController before_action :set_project_question, only: [:show, :edit, :update, :destroy] […]

如何在ActiveSupport :: Concern包含块中获取我的模型名称?

我正在使用ActiveSupport::Concern来扩展ActiveRecord ,以便ActiveSupport::Concern我的模块。 但为了做到这一点,我需要included块内部的ActiveRecord的模块名称。 例如,我有一个表,我保留了可访问的属性,我希望每个模块都能获得自己的可访问属性 我的module ActiveRecordExtension看起来像: module ActiveRecordExtension extend ActiveSupport::Concern included do AccessibleAttributes.where(name: “#{self.name.pluralize}”).each do |e| attr_accessible e.attribute.to_sym end end end # include the extension ActiveRecord::Base.send(:include, ActiveRecordExtension) A.如何在不获取nil:NilClass情况下(动态)获取我的模型名称nil:NilClass ? B.我如何检查included do哪些内容?

Rails Devise Rolify员工角色的集合

我正在使用Devise和Rolify。 员工belongs_to User和用户has_one :employee 。 用户模型已rolify 。 由于Rolify,有一个名为user_roles的表。 以下代码工作正常: 如果我想与用户而不是员工关联,这将有效: :collection => User.with_role(:agent) 现在,我希望在一个表单中有一个关联,其中包含一个具有“代理”角色的员工的选择。 以下尝试不起作用: Employee.user.with_role(:agent), :label_method => :employee_full_name, :label => ‘Agent’ %> 上面的一个给出了这个错误: undefined method `user’ for # 这也不起作用: User.with_role(:agent).employee.id, :label_method => :employee_full_name, :label => ‘Agent’ %> 什么会起作用? 谢谢您的帮助! UPDATE1 以下内容用于显示具有关联角色的员工列表: UPDATE2 因为employee.user.roles.first.name将为我提供employee.user.roles.first.name的角色名称,所以我尝试了这个: Employee.where(’employee.user.roles.first.name = ?’, ‘agent’) 但是,我得到一个PG错误= PG::Error: ERROR: improper qualified name (too […]