Tag: ruby

Rails Cancan:在注册时定义默认角色

我最近使用CanCanCan(通过枚举)为我的rails应用程序添加了角色 – 但现在我想在注册时添加默认角色。 我该怎么做呢? 它是在控制器还是模型中? 我的用户型号: class User < ActiveRecord::Base #Defining different roles enum role: [:Admin, :User, :Guest] #Users can only create one scholarship application has_one :applications # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable end 我的能力模型 – 只有三个角色,一个管理员,我将通过为一个角色为1的用户播种数据库来创建,然后其他人应该在注册时为2: class Ability include […]

如何从omniauth获取facebook的时区?

user.rb中的所有内容在注册时正确传递,时区除外。 我无法弄清楚为什么我在尝试登录时遇到此错误 : 2016-03-30T20:13:38.083469+00:00 app[web.1]: NoMethodError (undefined method `timezone=’ for #): 2016-03-30T20:13:38.083470+00:00 app[web.1]: app/models/user.rb:72:in `block in from_omniauth’ 2016-03-30T20:13:38.083471+00:00 app[web.1]: app/models/user.rb:66:in `tap’ 2016-03-30T20:13:38.083471+00:00 app[web.1]: app/models/user.rb:66:in `from_omniauth’ 2016-03-30T20:13:38.083472+00:00 app[web.1]: app/controllers/sessions_controller.rb:7:in `facebook’ user.rb def self.from_omniauth(auth) # Sets 60 day auth token oauth = Koala::Facebook::OAuth.new(“1540371223342976229929”, “ee917abf2e8f1c98274cd323fa1234ebb1346f4”) # Fake Numbers new_access_info = oauth.exchange_access_token_info auth.credentials.token new_access_token = new_access_info[“access_token”] new_access_expires_at = DateTime.now […]

Rails:将axlsx视图生成的文件发送到模型

我使用axlsx gem生成excel电子表格。 我正在尝试将生成的电子表格发送到模型以进行压缩。 此方法使用其他一些文件来压缩excel文件。 我的模型中的方法如下所示: def zipper tempfile = Tempfile.new children = self.children_with_forms Zip::OutputStream.open(tempfile) do |stream| children.each do |child| directory = “#{child.wide_reference[0,3]}/” if child.model_name == “Position” stream.put_next_entry(“#{child.volume} #{child.title} TOC.xlsx”) stream.print IO.read(Rails.application.routes.url_helpers.toc_path(format: :xlsx, position_id: child.id)) end stream.put_next_entry(“#{directory}#{child.wide_reference}-#{child.short_name}-#{child.title.truncate(15, omission:”)}.docx”) stream.print IO.read(child.download_form.path) end end tempfile end 我遇到问题的部分是: if child.model_name == “Position” stream.put_next_entry(“#{child.volume} #{child.title} TOC.xlsx”) stream.print IO.read(Rails.application.routes.url_helpers.toc_path(format: :xlsx, position_id: […]

如何计算方法中的方法

如果我们创建一个名为days_left_in_current_level的新方法,那么我们需要在那里放一些,以便我们可以计算current_level中剩余的天数? habit.rb def current_level return 0 unless date_started def committed_wdays committed.map do |day| Date::ABBR_DAYNAMES.index(day.titleize) end end def n_days ((date_started.to_date)..Date.today).count do |date| committed_wdays.include? date.wday end – self.real_missed_days end case n_days # 1 – 6 represent the different levels when 0..9 1 when 10..24 2 when 25..44 3 when 45..69 4 when 70..99 5 else 6 end […]

如何从Ruby代码连接到mongodb?

如何从Ruby代码连接到mongodb?

Rails访问符号的当前值

在edit.html.erb文件中,我有代码 这显示了一个数字字段,其当前值为question_id,允许我更改它。 现在我只想显示该值而不是让任何人改变它。 我怎样才能获得这个价值? 如果我说 问题只是等于一个字符串:“question_id”

从ruby方法中获取内容

我使用jruby运行一堆ruby脚本,虽然我使用的是纯ruby部分。 有时难以从输出中确切地发现究竟发生了什么或者确实出现了什么问题。 我想在每个方法的std中得到类似的东西:在方法A的方法A中输入 现在我肯定可以把这些评论放在每一种方法中……感觉非常错误。 有没有办法在更详细的情况下运行ruby以在我的日志中获取此信息。 希望我会避免使用很多gem等..因为这些是在一些托管服务器上,我将不得不花一些时间来获得更多的s / f。 希望有些东西可以作为jruby本身的一部分 谢谢!

RoR:CanCanCan仅授权用户创建的项目

这段代码有什么问题? 普通用户仍然可以看到所有的关系,当他应该只看到他自己的。 我的观看代码: 我的Ability课程: can :manage, :all if user.role == “admin” if user.role == “normal” can :read, Relato , :user_id => user.id can :manage, Relato, :user_id => user.id end

机架:管理客户端和服务应用程序响应

我正在使用Ruby on Rails 3,我在服务应用程序上正确设置Rack中间件行为时遇到了一些问题,以便使用来自客户端应用程序的传入请求。 我改变了我的代码 第一个代码 # application.rb config.middleware.use “Rack::Api::User” # lib/rack/api/user.rb module Rack module Api class User def initialize(app) # Changed part @app = app end def call(env) # Changed line if env[“PATH_HOST”] =~ /^\/api\/user\//i # Changed line @app.call(env) # Changed line else [200, {}, [“Nope, missed it. Here’s env:\n” + env.inspect]] end end […]

在实施“副作用”元编程时加载类的麻烦

我正在使用Ruby on Rails 3.2.9和Ruby 1.9.3-p125。 方案如下: 我实现了一个acts_as_customizable插件,我有一个Article模型类“充当可自定义”。 该插件通过元编程, 将一个名为customize 的方法添加到Comment模型类中( 注意 :由于这个原因,该插件具有副作用,因为操作了Article对象范围之外的Comment对象)。 鉴于这种情况,当我(重新)启动服务器并运行@comment.customize我得到错误NoMethodError – undefined method ‘customize’ for # 。 但是,当我(重新)启动服务器并运行以下代码时,所有工作都按预期工作: Article @comment.customize 如果我理解“邪恶”部分,上面的代码是有效的,因为在运行customize方法之前对Article类的“简单”调用使得它触发acts_as_customizable方法,该方法又将(通过元编程) customize方法添加到Comment类。 我该如何避免“邪​​恶”?