在我的application_helper.rb文件中,我有一个这样的函数:
def find_subdomain
request.domain
end
Run Code Online (Sandbox Code Playgroud)
undefined local variable or method `request'
我在另一个帮助器中调用此方法.如何在不通过控制器传递任何参数的情况下在helper中获取域.
我从GenServer中的句柄信息功能调用elixir genserver来添加电话号码获取表单客户端.但是一旦调用handle_call,所有者进程就会崩溃[timeout].请帮忙.
全局创建一个ETS以在调用任何下面描述的函数之前插入值.
def handle_info(message, state) do
{a,b} = message
phonenumber = b[:body]
add phonenumber
{:noreply, state}
end
def add(phonenumber) do
GenServer.call(__MODULE__, {:add, phonenumber})
end
def handle_call({:add, phonenumber}, from, state) do
:ets.insert(:access_table, {:details, phonenumber})
reply = {:ok, "Added #{phonenumber} to profile"}
new_state = [{username} | state]
{:reply, reply , new_state}
end
Run Code Online (Sandbox Code Playgroud)
错误:
** When Server state == []
** Reason for termination ==
** {timeout,{gen_server,call,['Elixir.Bankrecord',{add,"346534543534"},5000]}}
** (EXIT from #PID<0.150.0>) exited in: :gen_server.call(Bankrecord, {:add, '346534543534'}, 5000)
** (EXIT) time out
Run Code Online (Sandbox Code Playgroud) Ruby正确解析第一个日期,但第二个日期不正确.用ruby 1.9.3和2.1.2测试.
知道如何让它始终如一地运作吗?(我们的出生日期为2位数)
Date.strptime("10/11/89","%d/%m/%y")
=> Fri, 10 Nov 1989
Date.strptime("15/10/63","%d/%m/%y")
=> Mon, 15 Oct 2063
Run Code Online (Sandbox Code Playgroud) Ruby on Rails noob在这里参加了RoR.org上的入门教程.我在"5.11添加一些验证"部分,并在尝试刷新/ posts/new页面时抛出错误:
错误消息:
NoMethodError in PostsController#new
undefined method ` validates' for #<Class:0x0000000414fab0>
Extracted source (around line #2):
1 class Post < ActiveRecord::Base
2 validates :title, presence: true,
3 length: { minimum: 5 }
4 end
Run Code Online (Sandbox Code Playgroud)
我的帖子.rb:
class Post < ActiveRecord::Base
validates :title, presence: true,
length: { minimum: 5 }
end
Run Code Online (Sandbox Code Playgroud)
我的posts_controller.rb:
class PostsController < ApplicationController
def index
@posts = Post.all
end
def new
@post = Post.new
end
def create
@post = Post.new(params[:post].permit(:title, :text))
if @post.save
redirect_to @post …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用动态密钥和相应的值创建哈希.比如这样
hash = {1 => 23.67, 1 => 78.44, 3 => 66.33, 12 => 44.2}
Run Code Online (Sandbox Code Playgroud)
像这样的1,2,12是数组索引.我希望这是可以理解的.我正在尝试使用ROR教程中的语法.
像这样
test = Hash.new
for i in 0..23
if (s.duration.start.hour == array[i].hour)
s.sgs.each do |s1|
case s1.type.to_s
when 'M'
test ={i => s1.power} # here I am trying to create hash like give example in which i is for loop value
when 'L'
puts "to be done done"
else
puts "Not Found"
end
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
更新的代码
test = Hash.new
for i in …Run Code Online (Sandbox Code Playgroud) 我正在使用rails 4.1.0编写一个简单的应用程序.我创建了两个名为Serie和Event的模型,它们之间有一对多的关系(因为在一个Serie中有许多事件).
class Serie < ActiveRecord::Base
has_many :events
end
class Event < ActiveRecord::Base
belongs_to :serie
end
Run Code Online (Sandbox Code Playgroud)
我为这些模型创建了以下fixture文件:
#series.yml
---
Serie1:
name: Whatever
#events.yml
---
EventSerie11:
date: 2013-01-01
value: '124.4'
serie: Serie1
EventSerie12:
date: 2013-02-01
value: '124.2'
serie: Serie1
Run Code Online (Sandbox Code Playgroud)
问题是,当我通过使用加载这些灯具时rake db:fixtures:load,我得到了错误的事件对象的外键:
$ rails c
Loading development environment (Rails 4.1.0)
2.1.1 :001 > Serie.first.id
Serie Load (0.2ms) SELECT "series".* FROM "series" ORDER BY "series"."id" ASC LIMIT 1
=> 10
2.1.1 :002 > Serie.first.events.count
Serie Load (0.3ms) SELECT "series".* FROM "series" …Run Code Online (Sandbox Code Playgroud) 我想优化以下代码以简洁.
x1.each { |x|
x2.each { |y|
....
xN.each { |z|
yield {}.merge(x).merge(y)...... merge(z)
}
}
}
Run Code Online (Sandbox Code Playgroud)
假设x1, x2, ..., xN是Enumerator对象.
Arrays,但不能作为Enumerators
我试过这个但没有成功:
[x1, x2, ..., xN].reduce(:product).map { |x| x.reduce :merge }
Run Code Online (Sandbox Code Playgroud)
有什么建议?
UPDATE
目前解决了:
[x1, x2, ..., xN].map(:to_a).reduce(:product).map { |x|
yield x.flatten.reduce(:merge)
}
Run Code Online (Sandbox Code Playgroud) 所以我在http://guides.rubyonrails.org/getting_started.html上关注官方的ROR教程, 我被困在第5.8节,它教我如何列出所有文章
以下是我的控制器和index.html.erb
调节器
class ArticlesController < ApplicationController
def new
end
def create
@article = Article.new(article_params)
@article.save
redirect_to @article
end
def show
@article = Article.find(params[:id])
end
def index
@article = Article.all
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
Run Code Online (Sandbox Code Playgroud)
index.html.erb
<h1>Listing articles</h1>
<table>
<tr>
<th>Title</th>
<th>Text</th>
</tr>
<% @articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
</tr>
<% end %>
</table>
Run Code Online (Sandbox Code Playgroud)
我收到NoMethodError in Articles#index了错误消息
undefined method `each' for nil:NilClass"
Run Code Online (Sandbox Code Playgroud)
怎么了?我从字面上复制并粘贴了网站上的代码,看看我做错了什么,但仍无法解决.
我正在寻找一个函数,它可以从哈希中获取所有键,或者我可以循环遍历哈希以一次检索单个键.
目前我是硬编码的关键
VALUE option = rb_hash_aref(options, rb_str_new2("some_key"));
Run Code Online (Sandbox Code Playgroud) 当我尝试任何 rails,rake在临时服务器我得到了命令no such file to load -- ap (LoadError),
对于 rails s:
/usr/local/lib/ruby/gems/1.8/gems/bundler-1.0.22/lib/bundler/runtime.rb:68:in`require': no such file to load -- ap (LoadError)
xxxx
/config/application.rb:7
Run Code Online (Sandbox Code Playgroud)
对于 rake -T:
rake aborted!
no such file to load -- ap
Run Code Online (Sandbox Code Playgroud)
我的 application.rb档案:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env) if defined?(Bundler)
Run Code Online (Sandbox Code Playgroud)
我的rails和ruby版本:
ruby ×8
arrays ×1
elixir ×1
enumerator ×1
fixtures ×1
gen-server ×1
hash ×1
optimization ×1
sqlite ×1
validation ×1