我正在尝试生成一些Ruby代码,它将获取一个字符串并返回一个新的字符串,从其末尾删除了一些x个字符 - 这些可以是实际的字母,数字,空格等.
例如:给出以下字符串
a_string = "a1wer4zx"
我需要一种简单的方法来获得相同的字符串,减去 - 比如说 - 最后3个字符.在上面的例子中,那将是"a1wer".我现在这样做的方式似乎很复杂:
an_array = a_string.split(//,(a_string.length-2))
an_array.pop
new_string = an_array.join
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
在ruby中(在服务器端)验证电子邮件地址的最佳/简单方法是什么?
我想在视图中提供csv链接,我将csv生成代码放入ApplicationHelper.但是我收到了这个错误:
undefined method `send_data' for #<#<Class:0x0000010151c708>:0x0000010151a070>
Run Code Online (Sandbox Code Playgroud)
引用这个:
send_data content, :type => "text/plain",
:filename => filename,
:disposition => 'attachment'
Run Code Online (Sandbox Code Playgroud)
如果我将csv代码放在控制器中它工作正常.我希望使用帮助器来避免为每个我想为csv选项提供csv选项的控制器定义路由(我有一堆).如何send_data为助手提供(和其他必要的方法)?
这是我的哈希:
tempData = {"a" => 100, "here" => 200, "c" => "hello"}
Run Code Online (Sandbox Code Playgroud)
我需要访问哈希键作为一种方法,如:
tempData.a #100
tempData.here # 200
Run Code Online (Sandbox Code Playgroud) 我正在为库存管理编写Rails前端.我希望用户能够注册产品,所以我有:
class User < ActiveRecord::Base
has_many :products
# <snip>
end
Run Code Online (Sandbox Code Playgroud)
和
class Product < ActiveRecord::Base
belongs_to :user
# <snip>
end
Run Code Online (Sandbox Code Playgroud)
问题是产品是在用户注册之前创建的.也就是说,这是完全可以接受的呼吁Product.create,只是把它设置user_id到nil.但是,正如您可以想象的那样,Rails不支持开箱即用:
> Product.create!
(0.3ms) SELECT COUNT(*) FROM "products" WHERE "products"."type" IN ('Product')
(0.1ms) begin transaction
(0.1ms) rollback transaction
ActiveRecord::RecordInvalid: Validation failed: User can't be blank
from ~/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-3.2.13/lib/active_record/validations.rb:56:in `save!'
Run Code Online (Sandbox Code Playgroud)
我已经考虑过一堆kludgey解决方法,其中最吸引人的是有一个NullUser子类User并使用它来创建产品.但这似乎仍然是一个黑客.这是什么Rails方式?
谢谢.
相关迁移:
class AddUseridToProducts < ActiveRecord::Migration
def change
add_column :products, :user_id, :integer
end
end
Run Code Online (Sandbox Code Playgroud)
然后:
class Changeuseridtobeoptionalforproducts < ActiveRecord::Migration
def change …Run Code Online (Sandbox Code Playgroud) 我正在尝试安装Jekyll.运行后gem install jekyll我收到此错误:
ERROR: While executing gem ... (Errno::EACCES)
Permission denied - /usr/local/lib/ruby/gems/2.0.0/gems/jekyll-1.0.3/CONTRIBUTING.md
Run Code Online (Sandbox Code Playgroud)
我可以看到Jekyll在我跑的时候安装了gem list所以我很困惑:
*** LOCAL GEMS ***
bigdecimal (1.2.0)
classifier (1.3.3)
colorator (0.1)
commander (4.1.3)
directory_watcher (1.4.1)
fast-stemmer (1.0.2)
highline (1.6.19)
io-console (0.4.2)
jekyll (1.0.3)
json (1.7.7)
kramdown (1.0.2)
liquid (2.5.0)
maruku (0.6.1)
minitest (4.3.2)
posix-spawn (0.3.6)
psych (2.0.0)
pygments.rb (0.5.1)
rake (0.9.6)
rdoc (4.0.0)
rubygems-update (2.0.3)
safe_yaml (0.7.1)
syntax (1.0.0)
test-unit (2.0.0.0)
yajl-ruby (1.1.0)
Run Code Online (Sandbox Code Playgroud)
我过去在用户路径上遇到了很多问题,所以我想知道这个错误是否与此有关?
这是输出gem env:
RubyGems Environment:
- RUBYGEMS …Run Code Online (Sandbox Code Playgroud) 问题如下:
结果是:
我想以某种方式告诉rake忽略挂起的迁移,但到目前为止无法做到这一点.
更新(由于额外的经验)
有时迁移和模型代码不同步,因此迁移不会运行.为避免此问题,最近在迁移中使用了重新定义模型:
# reset all callbacks, hooks, etc for this model
class MyAwesomeModel < ActiveRecord::Base
end
class DoSomethingCool < ActiveRecord::Migration
def change
...
end
end
Run Code Online (Sandbox Code Playgroud) 我可以轻松地将一个元素添加到现有数组中:
arr = [1]
arr << 2
# => [1, 2]
Run Code Online (Sandbox Code Playgroud)
如何将多个元素添加到我的数组?
我想做类似的事情arr << [2, 3],但这会为我的数组添加一个数组#=> [1, [2, 3]]
我正在尝试为iPhone应用程序构建rails API.Devise适用于通过Web界面登录,但我需要能够使用REST API创建和销毁会话,我想使用JSON而不必在会话控制器上执行POST并且必须解析HTML并处理重定向.
我以为我可以这样做:
class Api::V1::SessionsController < Devise::SessionsController
def create
super
end
def destroy
super
end
end
Run Code Online (Sandbox Code Playgroud)
在config/routes.rb中我添加了:
namespace :api do
namespace :v1 do
resources :sessions, :only => [:create, :destroy]
end
end
Run Code Online (Sandbox Code Playgroud)
rake路由显示路由设置正确:
api_v1_sessions POST /api/v1/sessions(.:format) {:action=>"create", :controller=>"api/v1/sessions"}
api_v1_session DELETE /api/v1/sessions/:id(.:format) {:action=>"destroy", :controller=>"api/v1/sessions"}
Run Code Online (Sandbox Code Playgroud)
当我发布到/ user/sessions时,一切正常.我得到一些HTML和302.
现在如果我发布到/ api/v1/sessions我得到:
未知的操作AbstractController :: ActionNotFound
curl -v -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST http://localhost:3000/api/v1/sessions -d "{'user' : { 'login' : 'test', 'password' : 'foobar'}}"
Run Code Online (Sandbox Code Playgroud) [gkaykck@main myApplication]$ rails console
/usr/local/lib/ruby/1.9.1/irb/completion.rb:9:in `require': no such file to load -- readline (LoadError)
from /usr/local/lib/ruby/1.9.1/irb/completion.rb:9:in `<top (required)>'
from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails/commands/console.rb:3:in `require'
from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails/commands/console.rb:3:in `<top (required)>'
from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails/commands.rb:20:in `require'
from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails/commands.rb:20:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Run Code Online (Sandbox Code Playgroud)
我在ruby 1.9.2p136上安装了rails 3,我猜是好的.但我无法启动rails控制台,它给了我复制的错误.应用程序与ruby 1.8.7配合得很好,我从来没有看到过像这样的错误.
任何想法可能是什么?
ruby ×10
activerecord ×2
arrays ×1
controller ×1
devise ×1
jekyll ×1
linux ×1
macos ×1
rubygems ×1
view-helpers ×1