Tag: ruby on rails 5

Hartl Ruby on Rails教程4 ch 10,用户编辑测试失败

我正在尝试使用Hartl的教程创建一个自定义网站,从而练习Ruby on Rails。 到目前为止,情况一直很顺利,但我在用户编辑测试“使用友好转发成功编辑”中收到错误,其中用户在登录后未被重定向回编辑页面。这是我收到的错误消息: FAIL[“test_successful_edit_with_friendly_forwarding”, UsersEditTest, 3.1409137547016144] test_successful_edit_with_friendly_forwarding#UsersEditTest (3.14s) Expected response to be a redirect to but was a redirect to . Expected “http://www.example.com/users/787258272/edit” to be === “http://www.example.com/users/787258272”. test/integration/users_edit_test.rb:23:in `block in ‘ 我觉得这个解决方案非常简单,我错过了,但我似乎无法弄清楚它是什么。 这是我的users_controller: class UsersController < ApplicationController before_action :logged_in_user, only: [:edit, :update] before_action :correct_user, only: [:edit, :update] def show @user = User.find(params[:id]) end def […]

如何使用elasticsearch / searchkick在我的导航栏上运行搜索栏并在特定页面中显示结果?

我刚刚完成了一些处理搜索的教程。 我想将我的搜索栏放在我的导航栏上,这样每当有人搜索结果时,我都会被带到我的书库文件夹中的search.html.erb(类似于SO的工作方式)。 我遇到了一个问题,每次我进行搜索时都被带到我的书页[ http:// localhost:3000 / books?utf8 =%E2%9C%93&q = travel] ,没有结果只显示我的标准索引显示我的所有书籍,但从未进入我的搜索结果页面。 我在下面列出了所有相关代码。 如果有人能帮助我的话会非常感激。 非常感谢你们! Book.rb class Book true has_many :users, through: :likes searchkick end books_controller.rb class BooksController < ApplicationController before_action :authenticate_user!, only: [:new, :create] before_action :set_book, only: [:show, :edit, :update, :destroy, :share] def index @books = Book.all end def search @booksearches = Book.search(params.fetch(:q, "*")) end […]

无法通过Post在Rspec中创建模型

我正在尝试在Rspec中创建一个用户: require “rails_helper” RSpec.describe UsersController, type: :controller do describe “POST #index” do it ‘should create a user’ do post :create, {name: “name1”, age: 25}.to_json, format: :json # …………. 但我每次都会收到validation错误: name can’t be blank, age can’t be blank UserController接受json: def create @user = User.new(params1) if @user.save render json: @user else render json: @user.errors, status: :unprocessable_entity end […]

在rails中查看管理员

我有一个父表,子表,资金表。 家长登录,添加他的孩子,然后为每个孩子申请资助。 parent.rb class Parent < ApplicationRecord has_many :children, dependent: :destroy has_many :secondaryparents, dependent: :destroy end funding.rb class Funding < ApplicationRecord has_many :organisations, dependent: :destroy belongs_to :child end child.rb class Child < ApplicationRecord belongs_to :parent has_many :fundings, dependent: :destroy end Parent有一个字段作为管理员。 如果设置为true,则parent充当admin。 application.html.erb是admin的视图。 我能够将所有主要父母和他们的孩子展示在彼此面前。 我需要帮助,在他们面前展示孩子们的资助申请。 application.html.erb Application Status Primary Parent Name <!– Financial Status –> […]

Rails 5请求运行时间超过5000毫秒

我很难过。 我在docker中运行了一个可以安装Rails 5的vanilla但我经常遇到这个错误: Request ran for longer than 5000ms 到目前为止我所做的就是生成一个带有docker-compose run website rails g controller pages的新控制器。 到目前为止,如果我从layouts/application.html.erb删除(非常重要)行,我可以加载页面: 但是我放弃了所有我的默认样式(bootstrap)以及我希望在以后生活中加载的任何潜在的javascript文件,我不认为这是一个修复。 其他人遇到这个问题并知道如何解决它?

用于ActionDispatch路由的Rspec undefined方法’helpers’

我正在将旧的rails 3应用程序升级到rails 5,并在此过程中升级Rspec。 运行测试时,出现错误: NoMethodError Exception: undefined method `helpers’ for # 每当我在响应上调用be_success时。 见例子: it “renders ‘OK’ text when request is html” do get :ping expect(response).to be_success response.body.should == ‘OK’ end 我在运行此测试时遇到错误。 插入断点后,我看到每当调用be_success时都会抛出错误… (byebug) be_success *** NoMethodError Exception: undefined method `helpers’ for # Did you mean? helper_names 我在我的spec_helper文件中包含了config.include Rails.application.routes.url_helpers(使用rails路径路径时遇到了类似的问题),但它对此错误没有帮助。

找不到jsonapi解析器0.1.1

当我运行rails服务器时,出现’找不到jsonapi-parser-0.1.1.beta2’错误。 ~/ruby_on_rails/test/sampleapp$ rails s Could not find jsonapi-parser-0.1.1.beta2 in any of the sources Run `bundle install` to install missing gems. ~/ruby_on_rails/test/sampleapp$ bundle install Fetching gem metadata from https://rubygems.org/……… Fetching version metadata from https://rubygems.org/.. Fetching dependency metadata from https://rubygems.org/. Could not find jsonapi-0.1.1.beta4 in any of the sources

rails 5 carrierwave没有路由匹配图像

我需要一些帮助。 我使用rails 5和carrierwave上传图像。 我遇到的问题是图像被保存但不会显示,我得到一个无路由匹配GET …..我将图像存储在Rails.root的uploads文件夹中,因为我不希望它们保存在公共场所DIR。 image_uploader.rb def store_dir “uploads/#{model.user_id}/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}” end carrier_wave.rb require ‘carrierwave/orm/activerecord’ require ‘carrierwave’ CarrierWave.configure do |config| config.root = Rails.root end 这是我在日志中收到的错误: Started GET “/uploads/2/uitem/image/26/A_penrose-small.jpg” for ::1 at 2016-10-29 16:06:27 +0200 ActionController::RoutingError (No route matches [GET] “/uploads/2/uitem/image/26/A_penrose-small.jpg”): 非常感谢任何帮助,我一直在搜索,无法找到解决方案。 谢谢

Rails new给出“无法在任何来源中找到正确版本的铁路(4.2.5.1)”错误

我试图使用Rails 5.0.0.rc1与Ruby 2.3.1。 但是,如果我尝试在“编程”目录中创建一个新项目,我会得到:  ➜ ~/Documents/Programming rails new foo Could not find proper version of railties (4.2.5.1) in any of the sources Run `bundle install` to install missing gems. 如果我做一个gem列表,我在5.0.0.rc1中安装了唯一的rails版本。 有趣的是,当我执行rails -v时,我得到了同样的错误,但是当我从我的主目录中执行时,请不要这样做:  ➜ ~ rails -v Rails 5.0.0.rc1  ➜ ~ cd Documents/Programming  ➜ ~/Documents/Programming rails -v Could not find proper version […]

按月计算,按年计算

我有一个Albums表,其中有一个名为release_date的date列。 我想获得所有月份+年份组合的列表以及该月/年中发布的专辑数量。 所以,输出可能是这样的: 2016年11月 – 11 2016年10月 – 4 2016年7月 – 19日 2015年12月 – 2 Postgres 9.6上的Ruby 2.3.1 w / Rails 5,FWIW。