标签: ruby

将ruby数组转换为连续对的数组

将ruby数组转换为连续元素对数组的最简单方法是什么?

我的意思是:

x = [:a, :b, :c, :d]
Run Code Online (Sandbox Code Playgroud)

预期结果:

y #=> [[:a, :b], [:c, :d]]
Run Code Online (Sandbox Code Playgroud)

ruby arrays

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

对于Sinatra/Rack来说,什么是非常简单的认证方案

我正忙着将一个非常小的Web应用程序从ASP.NET MVC 2移植到Ruby/Sinatra.

在MVC应用程序中,当用户登录对数据库进行了正确验证时,FormsAuthentication.SetAuthCookie用于设置持久性cookie.

我想知道在Sinatra中相应的Forms身份验证是什么?所有的身份验证框架看起来都非常笨重,并不是我真正想要的.

ruby forms-authentication sinatra

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

设计自定义路由和登录页面

我正在尝试在我的Rails应用程序中使用自定义路由(Ruby 1.9.2 with Rails 3).

这是我的config/routes.rb文件

match '/dashboard' => 'home#dashboard', :as => 'user_root'
devise_for :user do
   get "/login", :to => "devise/sessions#new" # Add a custom sign in route for user sign in
   get "/logout", :to => "devise/sessions#destroy" # Add a custom sing out route for user sign out
   get "/register", :to => "devise/registrations#new" # Add a Custom Route for Registrations
end
Run Code Online (Sandbox Code Playgroud)

但是在/ login或/ register上提交表单会转到users/sign_in和users/sign_up.我该如何防止这种情况发生.或者甚至更好地确保默认情况下对users/sign_in等的所有请求都转到相关路由而不是Devise生成的默认路由.

另外,我如何使登录表单部分包含在任何控制器中?这样我可以在主页上设置登录页面(主页#index)而不是用户/ sign_in?

我在Mac OSX Snow Leopard上使用Ruby 1.9.2上的Devise 1.1.3和Rails 3.

谢谢!

ruby routing ruby-on-rails partials devise

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

符号到字符串问题

以下代码失败

world = :world
result = 'hello' + world
puts result #=> can't convert Symbol into String
Run Code Online (Sandbox Code Playgroud)

以下代码有效

world = :world
result = "hello #{world}"
puts result #=> hello world
Run Code Online (Sandbox Code Playgroud)

为什么?

使用ruby 1.8.7

ruby string symbols

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

Ruby:如何将一种方法接收的所有参数和块传递给另一种方法?

我正在编写一个帮助程序,它将一个HTML属性添加到rails中的link_to标记.所以,我的想法是我的帮助器方法应该接受传递给它的任何参数或块,使用相同的参数调用link_to,将它的属性添加到返回的内容,并将结果返回给调用者.

像这样:

def link_to(*args, &block)
  ... rails code in link_to ...
end

def myhelper(*args, &block) # Notice that at this point, 'args' has already
  link_to()                 # become an array of arguments and 'block' has
  ... my code ...           # already been turned into a Proc.
end

myhelper() # Any arguments or blocks I pass with this call should make
           # it all the way through to link_to.
Run Code Online (Sandbox Code Playgroud)

所以,正如你所看到的,似乎没有办法(不涉及大量的代码和条件分支)将myhelper收到的内容传递给link_to,而没有将所有参数恢复到它们到达之前的状态我的方法.

这个问题是否有更"类似Ruby"的解决方案?

ruby ruby-on-rails

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

捆绑包可以告诉我Gemfile中哪些宝石有更新版本(例如,干捆绑更新)

有没有办法在假装模式下运行bundle update,类似于Rails生成器的-p(假装)标志或cap -n(干运行)标志?

我想象的是:

$> bundle update -p
Fetching source index for http://rubygems.org/
The following gems have updated versions:
...
list of gems
...
Run Code Online (Sandbox Code Playgroud)

ruby bundler

38
推荐指数
1
解决办法
4441
查看次数

安装RVM:收到错误"出现错误(23)."

我试图安装Ruby Version Manager(RVM)但没有成功.如何解决我得到的这个错误?

ubuntu@ip-172-31-0-20:/tmp$ rvm install ruby-1.9.3-p484
Searching for binary rubies, this might take some time.
No binary rubies available for: ubuntu/14.04/x86_64/ruby-1.9.3-p484.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for ubuntu.
Requirements installation successful.
Installing Ruby from source to: /usr/local/rvm/rubies/ruby-1.9.3-p484, this may take a while depending on your cpu(s)...
ruby-1.9.3-p484 - #downloading ruby-1.9.3-p484, this may take a while depending on your connection...
Warning: Failed to create the file ruby-1.9.3-p484.tar.bz2.part: Permission …
Run Code Online (Sandbox Code Playgroud)

ruby ubuntu rvm

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

指南针在Symfony 2资产过滤器异常中

我已经在我的symfony 2项目中安装了指南针.现在我想将资产过滤器与指南针结合使用.我正在使用Windows 7.

我认为它几乎正常,但我仍然收到此错误:

[Assetic\Exception\FilterException]
An error occurred while running:
"C:\Ruby21-x64\bin\ruby.EXE" "C:\Ruby21-x64\bin\compass.BAT" "compile" "C:\
Users\tommie\AppData\Local\Temp" "--boring" "--config" "C:\Users\tommie\App
Data\Local\Temp\ass4325.tmp" "--sass-dir" "" "--css-dir" "" "C:/Users/tommi
e/AppData/Local/Temp/ass4326.tmp.scss"
Error Output:
C:/Ruby21-x64/bin/compass.BAT:1: syntax error, unexpected tCONSTANT, expect
ing end-of-input
Run Code Online (Sandbox Code Playgroud)

我在html(twig)中的代码:stylesheets.html.twig

{% stylesheets filter="compass" output='css/compiled/*.css'
   "@AcmeSassDemoBundle/Resources/assets/css/base.scss"
%}
<link rel="stylesheet" href="{{ asset_url }}" />

{% endstylesheets %}
Run Code Online (Sandbox Code Playgroud)

base.html.twig:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>{% block title %}Sass Demo!{% endblock %}</title>
    {#{% block stylesheets %}{% endblock %} #}

    {% include "AcmeSassDemoBundle:Demo:stylesheets.html.twig" %}
    <link …
Run Code Online (Sandbox Code Playgroud)

php ruby symfony compass

38
推荐指数
1
解决办法
1271
查看次数

如何编写启动tmux会话的shell脚本,然后运行ruby脚本

我想编写一个执行此操作的shell脚本:

  • 首先,创建一个tmux会话
  • 其次,在tmux会话中运行一个名为"run.rb"的ruby脚本

在伪代码中,我想做什么:

tmux new -s my_session
ruby run.rb     # NOTE: I want this to run inside the my_session tmux session.
tmux detach
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?(我读的帖子越多,就会越混乱.)

ruby bash shell tmux

38
推荐指数
4
解决办法
3万
查看次数

如何解决弃用警告"方法to_hash已弃用,将在Rails 5.1中删除"

我正在尝试更新到Rails 5,我收到以下弃用警告:

弃用警告:方法to_hash已弃用,将在Rails 5.1中删除,因为ActionController::Parameters不再继承哈希.使用此弃用行为会暴露潜在的安全问题.如果您继续使用此方法,则可能会在您的应用中创建可被利用的安全漏洞.相反,请考虑使用其中一种未被弃用的文档化方法:http://api.rubyonrails.org/v5.0.0/classes/ActionController/Parameters.html(从/ Data/Projects/portal/trunk/app中的column_header调用) /helpers/application_helper.rb:114)

警告所在的行如下所示:

    link_to(name,
            {
              action: action_name,
              params: params.merge({ order: key, page: nil })
            },
            {
              title: "Sort by this field",
            }) +
Run Code Online (Sandbox Code Playgroud)

如你所见,我不是在打电话to_hash.也许Rails是.也许其他一些宝石.我无从谈论,因为他们认为不值得提供堆栈跟踪.(临提示-它通常值得提供一个堆栈跟踪!)

所以无论如何,我跟着链接,打算找一个替代品,并且merge方法不会出现被弃用,但也许他们只是忘了记录弃用状态,所以我真的不能确定.

那么我应该做些什么来清除这个呢?

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

38
推荐指数
1
解决办法
8176
查看次数