小编Uri*_*ssi的帖子

rails helper中的访问请求对象

在我的application_helper.rb文件中,我有一个这样的函数:

def find_subdomain
  request.domain
end
Run Code Online (Sandbox Code Playgroud)
undefined local variable or method `request' 

我在另一个帮助器中调用此方法.如何在不通过控制器传递任何参数的情况下在helper中获取域.

ruby ruby-on-rails

9
推荐指数
2
解决办法
5592
查看次数

'handle_call'超时了

我从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)

elixir gen-server

8
推荐指数
1
解决办法
1867
查看次数

Ruby错误地解析了2位数年份

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 ruby-on-rails-3

8
推荐指数
2
解决办法
2412
查看次数

"未定义的方法`验证'"错误 - RoR Tut 5.11添加一些验证

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)

validation ruby-on-rails ruby-on-rails-4

7
推荐指数
2
解决办法
6399
查看次数

在rails上的ruby中创建带有键和值的动态哈希

我正在尝试使用动态密钥和相应的值创建哈希.比如这样

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)

ruby arrays hash ruby-on-rails multidimensional-array

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

Rails用错误的外键加载灯具

我正在使用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)

ruby sqlite ruby-on-rails fixtures ruby-on-rails-4

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

如何简化此枚举器代码?

我想优化以下代码以简洁.

x1.each { |x| 
  x2.each { |y|
    ....
    xN.each { |z|
      yield {}.merge(x).merge(y)...... merge(z)
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

假设x1, x2, ..., xNEnumerator对象.

  1. 以上并不简洁
  2. 它适用于x1,x2作为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)

ruby optimization enumerator simplification

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

文章#index中的Ruby on Rails教程NoMethodError

所以我在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)

怎么了?我从字面上复制并粘贴了网站上的代码,看看我做错了什么,但仍无法解决.

ruby ruby-on-rails

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

如何从hash - ruby​​ c扩展中获取密钥

我正在寻找一个函数,它可以从哈希中获取所有键,或者我可以循环遍历哈希以一次检索单个键.

目前我是硬编码的关键

VALUE option = rb_hash_aref(options, rb_str_new2("some_key"));
Run Code Online (Sandbox Code Playgroud)

ruby ruby-c-extension

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

没有此类文件要加载-ap(LoadError)

当我尝试任何 railsrake在临时服务器我得到了命令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版本:

  • 本地和暂存中的rails版本:3.0.11
  • 分期中的ruby版本:ruby 1.8.7(2012-02-08 …

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

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