小编max*_*ner的帖子

将RegExp转换为String然后返回RegExp

所以我有一个RegExp regex = /asd/

我将它存储为我的key-val商店系统中的密钥.

所以我说str = String(regex)回归"/asd/".

现在我需要将该字符串转换回RegExp.

所以我试试:RegExp(str)我明白了/\/asd\//

这不是我想要的.它不一样/asd/

我应该在将其转换为正则表达式之前从字符串中删除第一个和最后一个字符吗?在这种情况下,这会让我获得理想的结果,但如果RegExp具有类似/i或的修饰符,则不一定有效/g

有一个更好的方法吗?

javascript regex

29
推荐指数
2
解决办法
1万
查看次数

什么.:格式在rake路线中意味着什么

我输入了rake路线,我得到了一堆这样的网址 - /articles/:id(.:format)

我的问题是 - .:format意思是什么?从Rails Guides Routing文章中不清楚,.:formatStackOverflow或google上没有其他有用的匹配.有一种类似的格式,/:controller(/:action(/:id(.:format)))我也不明白.

谢谢

编辑跟进问题 -

如果我只想路由HTML页面.最佳做法是在路由中指定类似.:html的内容或使用.:format并为format.html写一个respond_to块?在后一种情况下,是否会忽略所有其他格式?

rake ruby-on-rails

28
推荐指数
2
解决办法
8309
查看次数

为什么在Rails中需要attr_accessor?

我偶尔会在模型代码中看到属性访问器/读者/编写器.如果我希望能够从视图/控制器代码更新属性,这些是否必要?

我是初学者,所以我真的在谈论基本的应用程序.

ruby ruby-on-rails

23
推荐指数
2
解决办法
2万
查看次数

ActiveRecord :: Migration弃用警告 - 请求Rails版本,但我没有使用Rails

这是一个Ruby非Web项目,它使用ActiveRecord与数据库进行通信.

有一个文件包含数据库连接代码,迁移和模型.看到这里(但没有必要阅读这个来回答这个问题)

require 'sqlite3'
require 'active_record'
require 'yaml'
require 'active_support/all'
require 'securerandom'

BasePath = "#{File.dirname(__FILE__)}/.."
DATABASE_FILENAME = "database.sqlite"
DATABASE_PATH = "#{BasePath}/#{DATABASE_FILENAME}"
SQLite3::Database.new(DATABASE_PATH)
ActiveRecord::Base.establish_connection(
  adapter: 'sqlite3',
  database: DATABASE_PATH
)

class Migrations < ActiveRecord::Migration
  def up
    create_table :todos do |t|
      t.string :content
      t.boolean :completed
      t.timestamps null: false
    end
  end
  def down
    puts "backing up database".red_on_black if File.file?(DATABASE_PATH)
    loop { (`cp #{DATABASE_PATH} #{DATABASE_PATH}-#{SecureRandom.urlsafe_base64}.backup`; break) rescue next }
    sleep 0.5
    drop_table :todos
    puts "dropped todos table"
  end
end # Migrations

class Todo < ActiveRecord::Base …
Run Code Online (Sandbox Code Playgroud)

activerecord ruby-on-rails ruby-on-rails-5

11
推荐指数
1
解决办法
3764
查看次数

如何将octopress 3部署到现有gh-pages网站的子目录中?

我正在使用Octopress 3,当我运行时jekyll build,它会生成正确的文件集(包括我的静态文件,请参阅下面的文件列表):

      $ cd _site
:_site$ ls -a
.               CNAME               assets              google2d8.html  index-alternative.html
..              about               blog                increase-revenue.html       index.html
Run Code Online (Sandbox Code Playgroud)

请注意,我的整个jekyll生成blog现在安全地存储在内\blog\,这就是我想要的.

但是一旦我这样做octopress deploy,它会覆盖整个文件夹(也会覆盖我现有的静态文件),请参阅下面的列表:

      $ cd _site
:_site$ ls -a
.       ..      Gemfile     Gemfile.lock    about       css     feed.xml    index.html  jekyll
Run Code Online (Sandbox Code Playgroud)

这是我的_config.yml:

title: My Title
email: some@email.com
description: > # this means to ignore newlines until "baseurl:"
     some description
baseurl: "/blog" # the subpath of your site, e.g. /blog
url: "http://example.com" # the base …
Run Code Online (Sandbox Code Playgroud)

ruby jekyll octopress

11
推荐指数
1
解决办法
207
查看次数

Rspec - 引发错误时参数数量错误

所以在我的代码中,我有这个方法,我正在尝试测试:

  # checks if a file already exists on S3
  def file_exists?(storage_key)
    begin
      s3_resource.bucket(@bucket).object(storage_key).exists?
    rescue Aws::S3::Errors::Forbidden => e
      false
    end
  end
Run Code Online (Sandbox Code Playgroud)

现在我正在尝试制作两个测试用例 - 一个用于文件存在时,一个用于何时不存在.

专注于失败案例.我想删除exists?引发Aws::S3::Errors::Forbidden错误,以便该file_exists?方法返回false.

这是我的测试代码的样子:

  it "returns false if the file doesn't already exist" do
    allow_any_instance_of(Aws::S3::Object).to receive(:exists?).and_raise(
      Aws::S3::Errors::Forbidden
    )
    expect(instance.file_exists?('foo')).to be false
  end
Run Code Online (Sandbox Code Playgroud)

运行此测试我看到:

   wrong number of arguments (given 0, expected 2)
   # ./lib/s3_client_builder.rb:48:in `file_exists?'
Run Code Online (Sandbox Code Playgroud)

真的不清楚这里发生了什么,因为这个file_exists?方法肯定没有2的arity,也不是exists?我正在捣乱的方法.

为了诊断这个,我在begin块中放了一个断点.我尝试运行该<object>.exists?行并得到相同的错误.

ruby rspec ruby-on-rails amazon-web-services

10
推荐指数
1
解决办法
1367
查看次数

Github Markdown 中的列表项之间出现额外的行

我有一个包含六个列表的自述文件。

一切正常,直到倒数第二个列表。问题是在这些列表项之间出现了一个额外的换行符。截图会更清楚:

在此处输入图片说明

我在 sublime text 中使用 Markdown Preview 包,如果我使用 Github Markdown 预处理器进行预览,我会看到相同的格式问题。

然而,最奇怪的是,如果我将文本输入到https://jbt.github.io/markdown-editor 中,我不会看到同样的问题

如果您想自己尝试,请使用以下字符串:

_git_

- **gl**: `git log <optional args>` _shows the revision history_
- **gpoh**: `git push origin head` _pushes the current branch_
- **gpom**: `git push origin master` _pushes the master branch_
- **grv**: `git remote -v` _lists the remotes_
- **gs**: `git status` _shows unstaged & staged changes_
- **gacm**: `git add -A; git commit -m "<message>"` _commits all changes with a message_ …
Run Code Online (Sandbox Code Playgroud)

markdown github-flavored-markdown

9
推荐指数
1
解决办法
4236
查看次数

在Rails中使用Postgres UUID时,在Join记录上创建,保存,更新和销毁

我正在创建uids

create_table :users, { id: false } do |t|
    t.uuid :uid, default: 'uuid_generate_v4()'
    ... other columns
Run Code Online (Sandbox Code Playgroud)

self.primary_key = :uid在模型中设置.

一般情况下,这适用于ActiveRecord,我写has_manybelongs_to关联很好.但是,当跨越连接表时(即has_many ... through:,我需要编写自定义SQL来获取记录.

我已经发现我一般可以通过编写自定义SQL来做到这一点,即 SELECT * FROM main_table JOIN join_table ON main_table.uid = cast(join_table.uid AS uuid) WHERE condition=true)

我刚刚意识到的ActiveRecord的create,destroy,save并且update不要在连接模型的工作.

我已修补了四种方法以便它们起作用,但这对我来说太复杂了,可能并不理想.这是我的补丁:

def save(*args)
    # save sometimes works if it is called twice,
    # and sometimes works the first time but says theres an error
    super(*args) unless …
Run Code Online (Sandbox Code Playgroud)

ruby postgresql activerecord ruby-on-rails

7
推荐指数
1
解决办法
393
查看次数

仅限我自己限制Firebase读/写访问权限

我有一个静态网站运行:

config =
  apiKey: "HIDDEN"
  authDomain: "HIDDEN"
  databaseURL: "HIDDEN"
  storageBucket: ""

firebase.initializeApp(config)
Run Code Online (Sandbox Code Playgroud)

在浏览器中(它编译为javacript)与Firebase的服务器进行身份验证.

我在这迷茫吗?这是从浏览器使用Firebase进行身份验证的有效方法吗?我从他们的"网络"教程中得到了代码,所以我觉得它是.

现在,我需要配置我的Firebase数据库规则,以便我和我只能读取和写入它.

我怎么能做到这一点?这个例子是否足够?

这是允许仅对经过身份验证的用户进行读/写的给定示例

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}
Run Code Online (Sandbox Code Playgroud)

javascript firebase-security firebase-authentication firebase-realtime-database

6
推荐指数
1
解决办法
2411
查看次数

form_for中text_field和text_area之间的区别

所以我看到text_field和text_area的例子在这样的表格中使用:

<%= form_for :account do |a| %>
    Name: <%= a.text_field :name %><br />
    Password: <%= a.text_area :password %><br />
    Password Confirmation: <%= a.text_field :password_confirmation %><br />
<%= a.submit %>
<% end %> 
Run Code Online (Sandbox Code Playgroud)

但我不明白其中的区别.初学者Rails开发人员是否有必要了解其中的差异?

我在API中找到了一些我不明白的解释 - 也许有人可以看看,让我知道发生了什么.

对于"text_area":

text_area(object_name, method, options = {})

Returns a textarea opening and closing tag set tailored for accessing a 
specified attribute (identified by method) on an object assigned to the template 
(identified by object). 
 Additional options on the input tag can be passed as a …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails

5
推荐指数
1
解决办法
4462
查看次数