English 中文(简体)
将参数传递回模型以细化随机操作
原标题:Passing parameter back to Model to refine Random action

我正在创建一个应用程序,它将根据单词中定义的字母显示随机图片。

使用Paperclip将图像附加到图片模型(包含另一个“字母”字段),并在每个块中迭代。

我该如何将字母从每个块传递回模型进行随机选择呢。

这就是我到目前为止所想到的,但它抛出了以下错误。

 undefined method `%  for {:letter=>"e"}:Hash

型号:

def self.random(letter)
  if (c = count) != 0
    find(:first, :conditions => [:letter => letter], :offset =>rand(c))
  end
end

视图:

<% @letters.each do |a| %>
    <%= Picture.random(a).image(:thumb) %>
<% end %>

谢谢

最佳回答

一个问题是您的条件存在语法错误。哈希表示法错误:

:conditions => [:letter => letter]

应该是

:conditions => {:letter => letter}

此外,在我看来,如果不允许偏移量为0,您的随机范围将始终排除第一张图片。除此之外,如果随机数为0,你真的想返回nil吗?

每次c=0时,Picture.rrandom(a).image(:thumb)都会抛出“nil:NilClass的未定义方法映像”异常。可能只是使用:

def self.random(letter)
  find(:first, :conditions => {:letter => letter}, :offset =>rand(count))
end

编辑:您需要保证数据库中有所有字母的图像,或者告诉用户给定字母不存在图像。

<% @letters.each do |a| %>
  <% if pic = Picture.random(a).image(:thumb) %>
    <%= pic.image(:thumb) %>
  <% else %>
    No image available for <%= a %>
  <% end %>
<% end %>

或者类似的。。。

编辑:事实上,我认为你的抵消策略不会奏效。另一种方法是返回给定字母的可用图像集,并从该集合中随机选择,类似于:

def self.random(letter)
  pics = find(:all, :conditions => {:letter => letter})
  pics[rand(pics.size)] if !pics.blank?
end
问题回答

暂无回答




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签