这工作正常,但我想让它更漂亮 - 并容纳所有可被4整除的值:
if i==4 || i==8 || i==12 || i==16 || i==20 || i==24 || i==28 || i==32
# ...
end
Run Code Online (Sandbox Code Playgroud)
任何聪明,简短的方法吗?
我正在尝试为我的管理链接创建一个帮助方法.在很多视图中我都有代码
<% if current_user %>
<%= link_to "Edit", edit_model_path(model) %>
<%= link_to "New", new_model_path %>
<%= link_to "Delete", model, :confirm => "You're a Noob", :method
=> :delete %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
仅在登录时显示这些内容.
我想在他们的位置做这样的事情
<%= admin_links(model) %>
Run Code Online (Sandbox Code Playgroud)
并将当前项传递给应用程序帮助程序方法
def admin_links(m)
if current_user
a = "#{link_to "edit" edit_m_path(m)}"
a << "#{link_to "new" new_m_path}"
a << "#{link_to "Delete", m, :confirm => "Your a Noob", :method
=> :delete}"
end
end
Run Code Online (Sandbox Code Playgroud)
或类似的东西.
由于大量的提交,我有一个庞大的git repo,所以根据这里的建议我创建了一个浅层克隆.我已经对这个新的本地仓库进行了更改,现在我想在Github上推动我的起源(然后再到我在Heroku上的升级和生产遥控器).也许有一天我会学习阅读文档:
git clone --depth命令选项说
--depth创建一个浅层克隆,其历史记录被截断为指定的修订数.浅存储库有许多限制(您不能克隆或获取它,也不能从中推送或插入它)
那么......我如何从这种情况中解脱出来并将我的代码推送到Github?
我正在尝试了解Prawn pdf gem.
我能够生成一个pdf.gemfile中的每个gem都包括:
gem 'mysql', '~> 2.8.1'
gem 'prawn', '~> 0.12.0'
gem 'pdf-reader', '~> 0.10.0'
gem 'Ascii85', '~> 1.0.1'
Run Code Online (Sandbox Code Playgroud)
在config/application.rb中:
config.autoload_paths << "#{Rails.root}/app/reports"
Run Code Online (Sandbox Code Playgroud)
然后在控制器中:
require 'prawn'
def index
pdf = Prawn::Document.new
pdf.text "Hello World"
pdf.render_file "x.pdf"
end
Run Code Online (Sandbox Code Playgroud)
比我调用索引函数.在我的应用程序的根目录中创建了一个名为x.pdf的PDF.在gemfile中,rakefile和config.ru.
题:
我正在使用Brakeman识别安全问题.它标记了params.merge用作跨站点脚本漏洞的任何链接.我该如何清理以下内容?
- @archives.each do |archive|
= link_to "FTP", params.merge(:action => :ftp, :archive => archive, :recipient => "company")
Run Code Online (Sandbox Code Playgroud) 我在我的rails 3.1rc6应用程序上有一个使用子域的登台和生产环境.我为这些环境购买并配置了不同的域名,因为默认的something-something.herokuapp.com不能很好地与子域名一起使用.
当我为一个环境设置session_store.rb时,一切正常:
AppName::Application.config.session_store :cookie_store, :key => '_sample_app_session' , :domain => '.mystagingdomain.co.uk'
Run Code Online (Sandbox Code Playgroud)
但我似乎无法在条件中添加允许特定于环境的域名.
我试过了
AppName::Application.config.session_store :cookie_store, :key => '_sample_app_session' , :domain => '.mystagingdomain.co.uk' if Rails.env.staging?
AppName::Application.config.session_store :cookie_store, :key => '_sample_app_session' , :domain => '.myproductiondomain.com' if Rails.env.production?
Run Code Online (Sandbox Code Playgroud)
这不起作用.
我在开发和生产中有一个较大的Rails 3.1应用程序,我只是在Heroku上设置了一个临时环境.因为我的git repo非常庞大,所以每次尝试推送时都会出现33%的超时错误.
有没有替代做git push staging master这个最初的巨型推动?
错误消息是
EmBP-2:Appname Emma$ git push staging master
Counting objects: 17421, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (6363/6363), done.
Connection to 10.10.18.33 closed by remote host.46 KiB/s
error: pack-objects died of signal 13
error: failed to push some refs to 'git@heroku.com:appname-staging.git'
Run Code Online (Sandbox Code Playgroud)
///////////////////解决方案/编辑,几个月后......
如果你已经设置了一个你已推送代码的环境,那么现在使用Heroku的(实验性)Pipeline功能有一种偷偷摸摸的解决方法.来自Heroku 文档:
"例如,您可以将代码推送到暂存,将其内置到一个slug中,然后将staging slug推广到生产阶段."
Heroku将现有的slug从一个应用程序推送到另一个应用程序大约需要5秒钟!
这里有很多很好的问题和答案,关于获取nested_form,collection_select,accepts_nested_attributes_for和fields_for可以很好地一起玩的互联网,但我仍然难过.如果你能帮助我,请提前致谢.
目标:生成新的isbn记录.isbn可以有很多贡献者.我成功地使用ryanb nested_form gem根据需要在表单上动态生成新的贡献者字段.其中一个字段使用Contributor中的所有名称记录的collection_select下拉列表.创建新记录时,需要将许多贡献者ID写入连接表(contributors_isbns).
我有一些工作,但只是我可以将一个贡献者ID保存到isbns表中的新记录.我似乎无法将任何数据写入连接表.
我有三个型号.贡献者和Isbns有很多关系,我用has_many做过:通过.isbn可以拥有许多贡献者,贡献者可以拥有许多isbns.他们通过contributors_isbn加入.
isbn.rb
attr_accessible :contributor_id
has_many :contributors, :through => :contributors_isbns
has_many :contributors_isbns
accepts_nested_attributes_for :contributors
accepts_nested_attributes_for :contributors_isbns
Run Code Online (Sandbox Code Playgroud)
contributor.rb
attr_accessible :isbn_id
has_many :contributors_isbns
has_many :isbns, :through => :contributors_isbns
accepts_nested_attributes_for :isbns
Run Code Online (Sandbox Code Playgroud)
contributors_isbn.rb
class ContributorsIsbn
attr_accessible :isbn_id, :contributor_id
belongs_to :isbn
belongs_to :contributor
accepts_nested_attributes_for :contributors
Run Code Online (Sandbox Code Playgroud)
在isbns控制器中:
def new
@isbn = Isbn.new
@title = "Create new ISBN"
1.times {@isbn.contributors.build}
@isbn.contributors_isbns.build.build_contributor
end
Run Code Online (Sandbox Code Playgroud)
(显然我无法理解使用哪种构建方法.)
在isbns new.html.erb视图中:
<%= nested_form_for @isbn, :validate => false do |f| %>
<h1>Create new ISBN</h1>
<%= render …Run Code Online (Sandbox Code Playgroud) 这是我在rake任务中打开文件的代码:
File.open(Rails.root.join("public/system/xmls/**/original/*.csv"),"r") do |file|
#etc
Run Code Online (Sandbox Code Playgroud)
但它不匹配任何文件(有三种可能的匹配).第一个**是一个名为2位的文件夹.我哪里错了?
什么是明智的说法.
if @thing == "01" or "02" or "03" or "04" or "05"
Run Code Online (Sandbox Code Playgroud)
(这些数字包含在数据类型字符串列中.)