小编Eri*_*win的帖子

Rack:如何将URL存储为变量?

我正在写一个简单的静态Rack应用程序.查看下面的config.ru代码:

use Rack::Static, 
  :urls => ["/elements", "/img", "/pages", "/users", "/css", "/js"],
  :root => "archive"


map '/' do
  run Proc.new { |env|
    [
      200, 
      {
        'Content-Type'  => 'text/html', 
        'Cache-Control' => 'public, max-age=6400' 
      },
      File.open('archive/splash.html', File::RDONLY)
    ]
  }
end

map '/pages/search.html' do
  run Proc.new { |env|
    [
      200, 
      {
        'Content-Type'  => 'text/html', 
        'Cache-Control' => 'public, max-age=6400' 
      },
      File.open('archive/pages/search.html', File::RDONLY)
    ]
  }
end

map '/pages/user.html' do
  run Proc.new { |env|
    [
      200, 
      {
        'Content-Type'  => 'text/html', 
        'Cache-Control' => 'public, max-age=6400' 
      },
      File.open('archive/pages/user.html', File::RDONLY) …
Run Code Online (Sandbox Code Playgroud)

ruby rack

5
推荐指数
2
解决办法
714
查看次数

登录为Postgres的,但得到的错误CREATEUSER:新角色创建失败:错误:必须是超级用户创建超级用户

我需要创建一个超级用户,这样我就可以创建一个db,但是我遇到了麻烦.我以用户postgres身份登录:

sudo su - postgres
Run Code Online (Sandbox Code Playgroud)

但是当我尝试创建超级用户时,我遇到以下问题:

$createuser glassboard;
Shall the new role be a superuser? (y/n) y
Run Code Online (Sandbox Code Playgroud)

createuser:创建新角色失败:错误:必须是超级用户才能创建超级用户

如果我尝试在psql中创建一个新用户,然后让他成为超级用户,也会发生这种情况:

$ psql -U postgres
psql (9.1.4)
Type "help" for help.

postgres=> create user glassboard
postgres-> ;
ERROR:  permission denied to create role
Run Code Online (Sandbox Code Playgroud)

如何创建超级用户?

\dupostgres的输出:

postgres=> \du

                             List of roles
 Role name |                   Attributes                   | Member of 
-----------+------------------------------------------------+-----------
 main      | Superuser, Create role, Create DB, Replication | {}
 postgres  |                                                | {}
Run Code Online (Sandbox Code Playgroud)

postgresql

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

Rspec错误NameError:Factory上未初始化的常量

我正在用两个工厂创建一组测试,见下文:

规格/工厂/ page.rb

FactoryGirl.define do 
    factory :page do
      title "Example Title"
      content "Here is some sample content"
      published_on "2013-06-02 02:28:12"
    end 
    factory :page_invalid do
      title ""
      content ""
      published_on "2013-06-02 02:28:12"
    end 
end
Run Code Online (Sandbox Code Playgroud)

但是,在spec/controllers/page_controller_spec.rb中,以下测试会引发错误:

describe "with invalid params" do 
  it "does not save the new page in the database" do
    expect {
      post :create, {page: attributes_for(:page_invalid)}, valid_session
      }.to_not change(Page, :count).by(1)
  end
Run Code Online (Sandbox Code Playgroud)

错误:

  1) Api::PagesController POST create with invalid params does not save the new page in the database
     Failure/Error: post …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails

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

Rails的Heroku错误:语法错误,意外的keyword_end,期望输入结束

我正在将一个slug上传到Heroku,但我在主题标题中收到了错误.首先,我的config/environment/production.rb文件

config/environment/production.rb
  <App Name>::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb


  #Devise Note:
  #config/environments/development.rb:

  #config.action_mailer.default_url_options = { :host => 'localhost:3000' }

  #In production, :host should be set to the actual host of your application.


  # Code is not reloaded between requests
  config.cache_classes = true

  # Full error reports are disabled and caching is turned on
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  # Disable Rails's static asset server (Apache or nginx will already do this)
  config.serve_static_assets …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails heroku ruby-on-rails-3

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

jsx中的格式编号React组件

我正在编写一个jsx文件,并希望格式化表格中的数字显示.这是表的代码:

<tr>
  <td>
    {stringVar}
  </td>
  <td>
    {numberVar}
  </td>
</tr>
Run Code Online (Sandbox Code Playgroud)

numberVar正在直接打印; 如何使用C风格的字符串格式显示该数字(我需要设置精度值,添加逗号和$字符)?

javascript jquery reactjs react-jsx

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

如何在Rails中获取mb中ruby对象的大小?

我想查询ActiveRecord模型,修改它,并以mb计算新对象的大小.我该怎么做呢?

ruby memory activerecord ruby-on-rails

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

jQuery在Rails中执行if-else语句的两个分支

我在Rails页面上有以下jQuery:

$(document).on('click','.reportsArrow', function() {
    if ( $(this).parent().hasClass('reportCollapsed') ) {
      console.log("A");
      $(this).parent().removeClass('reportCollapsed');
    }else{
      $(this).parent().addClass('reportCollapsed');
        console.log("B");
    }
});
Run Code Online (Sandbox Code Playgroud)

当我点击有reportsArrow和没有的元素时reportCollapsed,日志显示

B

A

这意味着它正在执行else零件然后执行if零件.我希望每次单击只执行一次该功能,并且只跟随一个代码路径.为什么要执行两次以及如何阻止它?我应该指出,这在Web设计器创建的模型中正确切换(仅限HTML/CSS/JS).看起来问题是Rails相关.

编辑:

我们找到了一个有效的解决方案

$('.reportsArrow').click(function() {
    $(this).parent().toggleClass('reportCollapsed');
}); 
Run Code Online (Sandbox Code Playgroud)

javascript jquery ruby-on-rails

4
推荐指数
1
解决办法
1247
查看次数

将字段添加到 Rails 模型而不将它们保存在 Rails 的数据库中

我正在渲染基于表示 Rails 模型的 JSON 对象的视图。这个 JSON 对象需要不属于 Rails 模型的字段,所以我将它们添加到控制器中,如下例 1 所示:

#Controller
events = Event.where(<query>).each do |event|
    event[:duration] = (event.end - event.start)/3600
    event[:datatime] = event.start.hour + event.start.min/60.0
    ...
end

render json: events
Run Code Online (Sandbox Code Playgroud)

这可以正确呈现我的数据。但是,我收到以下弃用警告:

DEPRECATION WARNING: You're trying to create an attribute `duration'. Writing arbitrary attributes on a model is deprecated. Please just use `attr_writer` etc.
Run Code Online (Sandbox Code Playgroud)

我想重写我的代码以避免此警告。如果我尝试将这些额外字段视为标准对象属性,则无法正确呈现这些值。下面是我尝试更改为使用标准对象属性:

#Controller
events = Event.where(<query>).each do |event|
    event.duration = (event.end - event.start)/3600
    event.datatime = event.start.hour + event.start.min/60.0
    ...
end

render json: events



#Model
class Event …
Run Code Online (Sandbox Code Playgroud)

ruby json ruby-on-rails ruby-on-rails-3

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

从R中的列表中删除""元素

我有一个列表,我想删除空字符:"".

我似乎错误地对元素进行了子集化:

> sample2[which(sample2 == "")]
list()
> sample2[which(sample2 != "")]
[[1]]
 [1] ""          "03JAN1990" ""          ""          ""         
 [6] ""          "23.4"      "0.4"       ""          ""         
[11] ""          ""          "25.1"      "0.3"       ""         
[16] ""          ""          ""          "26.6"      "0.0"      
[21] ""          ""          ""          ""          "28.6"     
[26] "0.3"   
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能分组并删除空字符?

r

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