Tag: ruby on rails 4

访问请求规范中的请求对象

如何在请求规范上执行请求之前设置请求标头? 我正在移动控制器规范以使用Rails在我的API上请求规范。 我坚持的一件事是我无法访问request对象以允许请求。 在我的控制器规格上,我可以访问我创建的一个签署特定用户的方法: def sign_in(user) token = user.api_keys.first.token # note the request object being used in the next line request.env[“HTTP_AUTHORIZATION”] = ActionController::HttpAuthentication::Token.encode_credentials(token) end 这在控制器规格上工作正常,我可以安全地做到: before { sign_in(user) } it { post :endpoint, params } 但是在请求规范中, request对象不可用。 如果我尝试: before { sign_in(user) } it { post “/api/endpoint”, params } 我的帮助方法request为nil 。 我知道我能做到: it { post “/api/endpoint”, […]

rails 4中关于匹配关键字的路由问题在rails 3中工作

在rails 3匹配关键字正在工作但在rails 4匹配关键字不适用于路由 如何在rails 4中定义这些路由 此代码段正在rails 3中运行 match ‘admin’, :to => ‘access#menu’ match ‘show/:id’, :to => ‘public#show’ match ‘:controller(/:action(/:id(.:format)))’ 我需要轨道4的通用公式,如轨道3 match ‘:controller(/:action(/:id(.:format)))’

未初始化的常量ApplicationRecord

我正在线上的rails教程书,当我转到http:// localhost:3000 /时,我收到以下错误消息 “未初始化的常量ApplicationRecord” 它给了我以下代码突出显示第一行。 class User < ApplicationRecord attr_accessor :remember_token before_save { self.email = email.downcase } validates :name, presence: true, length: { maximum: 50 } VALID_EMAIL_REGEX = /\A[\w+\-.]+@[az\d\-.]+\.[az]+\z/i validates :email, presence: true, length: { maximum: 255 }, 这是我的application.html.erb文件: true %> true %> <div class="alert alert-“> 和我的user.rb文件: class User < ApplicationRecord attr_accessor :remember_token before_save { […]

Rails 4.1和Bootstrap 3 glyphicons无法正常工作

我试图摆脱使用Bootstrap 3的Rails 4项目中的glyphicon错误。我没有使用任何Bootstrap gems将其添加到资产管道。 我手动将bootstrap.css和bootstrap.js添加到各自的app/assets目录,并将它们添加到application.css和application.js我现在看到的是我的Web浏览器控制台中的以下内容: GET http://localhost:3000/fonts/glyphicons-halflings-regular.woff 404 (Not Found) localhost/:1 GET http://localhost:3000/fonts/glyphicons-halflings-regular.ttf 404 (Not Found) localhost/:1 GET http://localhost:3000/fonts/glyphicons-halflings-regular.svg 404 (Not Found) 在Ruby on Rails应用程序中可以做些什么来解决这个问题? 我尝试将所述文件复制到app/assets/fonts并将其弹出到application.rb : config.assets.paths << "#{Rails}/app/assets/fonts" 没运气。

Wicked-PDF没有显示图像,’wicked_pdf_image_tag’未定义

我想生成一个包含我们部门徽标的PDF。 当我尝试在我的控制器中使用WickedPdf类时(使用https://github.com/mileszs/wicked_pdf中描述的方法): def some_action image_tag_string = image_tag(‘logo.jpg’) pdf = WickedPdf.new.pdf_from_string(image_tag_string) save_path = Rails.root.join(‘testpdfs’,’logotest.pdf’) File.open(save_path, ‘wb’) do |file| file << pdf end end …应用程序将PDF保存到目标目录,但它有一个蓝白色的’?’ 标记图像应该在哪里。 如果我这样做: image_tag_string = wicked_pdf_image_tag(‘logo.jpg’) pdf = WickedPdf.new.pdf_from_string(image_tag_string) 我收到以下错误: NoMethodError: undefined method `wicked_pdf_image_tag’ for #<… 看来我的Rails应用程序也缺少/没有链接到属于wicked-pdf gem的帮助文件。 StackOverflow上类似问题的答案建议编写自定义“图像标记”帮助程序来定位图像或安装wkhtmltopdf。 对我来说,当放置在View(whatever.html.erb)中时,image-tag显示徽标就好了。 “logo.jpg”已经位于资产管道和#{RailsRoot} / public / images中。 最后,我在Ubuntu 14.04上使用wkhtmltopdf 0.9.9,wicked-pdf 0.11.0和rails 4。 简而言之 – 我做错了什么导致WickedPDF无法渲染图像?

如何为#解决未定义的方法`to_key’?

我遇到了未定义方法`to_key’的问题 这是我的books_controller.rb class BooksController < ApplicationController def index @books = Book.where(user_id: current_user.id) end end 和我的索引页面如下。 index.html.erb … … 现在,当我要访问索引页面时,我得到了如下错误。 undefined method `to_key’ for #

在Ruby中将Unicode Number转换为Integer

不幸的是,我有一些数字作为字符串使用非ASCII数字。 我需要将它们转换为常规的Ruby数字来对它们进行一些数学运算。 因此,例如,如果数字作为字符串“19”进来,这是19但是作为字符“扩展阿拉伯语数字一”然后是“扩展阿拉伯语数字九”,我需要一种方法将其转换为Ruby整数Fixnum 19。 问题是, 根据这个 ,有55组这些扩展数字的0-9,即我需要处理550个总代码点。 我已经知道,对于给定的组,连续数字的代码点是连续的,因此例如扩展的阿拉伯语数字0是U + 06F0,扩展的阿拉伯语数字9是U + 06F9,所以我可以测试每个数字以查看哪个范围它在然后从我正在查看的字符的代码点中减去零代码点作为整数,给我常规的Ruby整数。 例如,6F9 – 6F0 = 9(粗略地说,一旦它们被转换为整数代码点)。 但要做到这一点,我需要为这55个范围创建一个巨大的查找哈希,这就是很多打字。 我想我可以将上面链接中的HTML表格翻译成ruby地图,但这感觉很糟糕。 我已经知道了 “۱۹” =~ /[[:digit:]]+/ 将是匹配,但问题是“如何将这些Unicode数字转换回常规的Ruby整数?” 一定有更好的方法! 有任何想法吗? 谢谢!

使用多个产量来插入内容

我正在尝试使用yield在我的页面上插入内容,但每次操作都会从页面中删除整个内容。 我有一个主要yield ,工作正常: 但是在一个页面上显示的新内容中,我有另一个yield : 当用户单击呈现的菜单时,应在该菜单下方显示新内容。 管理员/ _menu.html.erb “admins”, :action => “test” %> 控制器: class AdminsController < ApplicationController def index end def test @users = User.paginate(page: params[:page]) end end test.html.erb All users … 当我从菜单中单击“用户”选项时,页面刷新,菜单消失,“body”内没有显示任何内容。 我希望内容显示在菜单下方。 如何使用第二次产量并完成此function? 我希望这个问题不会令人困惑。 如果问题令人困惑,请在评论中写下我,我会立即编辑。 谢谢 :)

Rails路由:嵌套,成员,集合,命名空间,范​​围和可自定义

我想了解更多有关Rails路由的信息。 会员和collections # Example resource route with options: resources :products do member do get ‘short’ post ‘toggle’ end collection do get ‘sold’ end end 命名空间和范围 # Example resource route within a namespace: namespace :admin do resources :products end scope :admin do resources :products end 约束,Redirect_to # Example resource route with options: get “/questions”, to: redirect […]

如何在Rails 4中设置自定义字符串外键?

如何使用string foreign_key设置关联以正确设置has_one? class Pharmaceutic < ActiveRecord::Base has_one :pharmaceutic_group, foreign_key: "code" end class PharmaceuticGroup > Pharmaceutic.last.pharmaceutic_group Pharmaceutic Load (0.3ms) SELECT `pharmaceutics`.* FROM `pharmaceutics` ORDER BY `pharmaceutics`.`id` DESC LIMIT 1 PharmaceuticGroup Load (0.3ms) SELECT `pharmaceutic_groups`.* FROM `pharmaceutic_groups` WHERE `pharmaceutic_groups`.`code` = 2 ORDER BY `pharmaceutic_groups`.`id` ASC LIMIT 1 => nil >> Pharmaceutic => Pharmaceutic(id: integer, package_type: integer, group_code: […]