我正在使用主动模型序列化器.我有一个有很多活动的模特活动.
我想用前n个活动返回事件.我想我应该将params n传递给事件序列化器.
我有一个列表页面,然后是表单,然后是感谢页面.我需要在感谢页面上添加一个链接,该链接将用户带回到他们所在的页面之前,该表单始终有所不同.我试过用这个:
= link_to "Back", :back
Run Code Online (Sandbox Code Playgroud)
但这只会让他们回到上一页,所以表格.
该脚本必须验证大量IP中是否存在一个预定义的IP.目前我的代码功能就像这样(说"ips"是我的IP数组,"ip"是预定义的ip)
ips.each do |existsip|
if ip == existsip
puts "ip exists"
return 1
end
end
puts "ip doesn't exist"
return nil
Run Code Online (Sandbox Code Playgroud)
有没有更快的方法来做同样的事情?
编辑:我可能错误地表达了自己.我可以做array.include吗?但我想知道的是:array.include?能给我最快结果的方法吗?
我正在使用Refile with Rails 4.我正在按照他们的教程进行多个图像上传.每个帖子可以有多个图像.我的模型看起来像这样:
Post.rb:
has_many :images, dependent: :destroy
accepts_attachments_for :images, attachment: :file
Run Code Online (Sandbox Code Playgroud)
Image.rb:
belongs_to :post
attachment :file
Run Code Online (Sandbox Code Playgroud)
我可以上传文件,使用:
<%= f.attachment_field :images_files, multiple: true, direct: true, presigned: true %>
Run Code Online (Sandbox Code Playgroud)
但是当我尝试检索如下图像时:
<%= attachment_image_tag(@post.images, :file, :small) %>
Run Code Online (Sandbox Code Playgroud)
我收到错误:
undefined method file for #<Image::ActiveRecord_Associations_CollectionProxy:0x007fbaf51e8ea0>
Run Code Online (Sandbox Code Playgroud)
如何使用多个图像上传来重新检索图像?
如果我在Rails 4 + Ruby 2.0项目的文本编辑器中编辑.scss或.erb文件,我想在浏览器中看到实时更改.
我尝试了Guard和Guard-live-reload,但它引发了以下错误.
22:58:04 - ERROR - Could not load 'guard/rspec' or find class Guard::Rspec
22:58:04 - ERROR - /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/plugin_util.rb:100:in `require'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/plugin_util.rb:100:in `plugin_class'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/plugin_util.rb:57:in `initialize_plugin'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard.rb:167:in `add_plugin'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/dsl.rb:174:in `block in guard'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/dsl.rb:173:in `each'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/dsl.rb:173:in `guard'
> [#] /home/jitendra/milaap-webapp/Guardfile:4:in `_instance_eval_guardfile'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/guardfile/evaluator.rb:97:in `instance_eval'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/guardfile/evaluator.rb:97:in `_instance_eval_guardfile'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/guardfile/evaluator.rb:37:in `evaluate_guardfile'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/setuper.rb:148:in `evaluate_guardfile'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/setuper.rb:64:in `setup'
> [#] /home/jitendra/.rvm/gems/ruby-2.1.1@global/gems/guard-2.6.1/lib/guard/commander.rb:24:in `start'
> [#] …Run Code Online (Sandbox Code Playgroud) 我正在使用码来从rdoc文件生成Rails应用程序的文档.有AngularJS文档生成器,但它们如何连接以生成AngularJS + Rails应用程序的一个连贯文档?
在此示例中使用guard子句的正确方法是什么?
def require_admin
unless current_user && current_user.role == 'admin'
flash[:error] = "You are not an admin"
redirect_to root_path
end
end
Run Code Online (Sandbox Code Playgroud)
尝试使用这些https://github.com/bbatsov/ruby-style-guide#no-nested-conditionals约定重写时,我不知道在哪里放置flash消息
看看下面的工作,我会假设工作在low_priority队列中运行.
class GuestsCleanupJob < ActiveJob::Base
queue_as :low_priority
#....
end
Run Code Online (Sandbox Code Playgroud)
我同意这一点,但这只是队列的名称正确吗?它实际上与优先级无关.例如,如果我创建了一个名为queue的作业,:my_queue那么它将被视为具有与:low_priority队列相同的优先级.
从文档中我无法找到任何表明我可以优先排队的工作.我知道delayed_jobs有这个功能,但我还没有在active_job中找到它.
我有一个包含电影列表的数据库.典型条目如下所示:
id: 1,
title: "Manhatten and the Murderer",
year: 1928,
synopsis: 'some text...'
rating: 67,
genre_id, etc. etc.
Run Code Online (Sandbox Code Playgroud)
现在我试图让一系列搜索测试通过,到目前为止我已经做了一个测试用例传递,如果你在文本字段中输入标题"Manhatten and the Murderer",它将找到你想要的电影.问题在于部分匹配.
现在我想要一种搜索"Manhat"并匹配记录"Manhatten and the Murderer"的方法.我也希望它与任何有"Manhat"的电影相匹配.例如,它可能会返回2或3个其他标题:"我在曼哈顿的生活",标题:"曼哈顿的大苹果"等.
下面是我在Movie模型中到目前为止的代码:
def self.search(query)
# Replace this with the appropriate ActiveRecord calls...
if query =~ where(title:)
#where(title: query)
binding.pry
end
end
Run Code Online (Sandbox Code Playgroud)
我的问题是,我该如何设置?我的问题是"where(title :)行.一个想法是使用Regexp来匹配title属性.任何帮助将不胜感激!谢谢.
我正在开发一个小型的rails网站,允许一些用户上传图像,其他用户可以看到它们.我开始使用CarrierWave和S3作为存储介质,一切都运行良好,但后来我想尝试使用CanFront.我首先distribution在我的S3存储桶中添加了一个,然后将我正在使用的CarrierWave配置更改为:
CarrierWave.configure do |config|
config.storage = :fog
config.fog_credentials = {
:provider => 'AWS', # required
:aws_access_key_id => ENV['S3_ACCESS_KEY_ID'], # required
:aws_secret_access_key => ENV['S3_SECRET_ACCESS_KEY'], # required
:region => 'eu-west-1',
}
config.asset_host = 'http://static.my-domain.com/some-folder'
config.fog_public = true # optional, defaults to true
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'} # optional, defaults to {}
end
Run Code Online (Sandbox Code Playgroud)
我应该提一下,它http://static.my-domain.com是一个指向CloudFront端点(some-id.cloudfront.net)的CNAME条目.结果是图片显示正确,URL看起来像这样:http://static.my-domain.com/some-folder/uploads/gallery_image/attachment/161/large_image.jpg但每当我尝试上传照片或者就此而言,获得上传附件的大小时,我会得到以下异常:
Excon::Errors::MovedPermanently: Expected(200) <=> Actual(301 Moved Permanently)
response => #<Excon::Response:0x007f61fc3d1548 @data={:body=>"",
:headers=>{"x-amz-request-id"=>"some-id", "x-amz-id-2"=>"some-id",
"Content-Type"=>"application/xml", "Transfer-Encoding"=>"chunked",
"Date"=>"Mon, 31 Mar 2014 21:16:45 GMT", "Connection"=>"close", "Server"=>"AmazonS3"}, …Run Code Online (Sandbox Code Playgroud)