在最近的一次railscast之后,我在我的网站上使用了kaminari gem,我想做ajax分页
kaminari文件说: the helper supports Rails 3 unobtrusive Ajax.
有没有人有关于如何做到这一点的任何提示或教程?我正在使用jquery,rails 3等.我想我的application.js文件需要一些内容来监听分页导航中的按钮
如果有人需要代码,我会发布它
jquery pagination ruby-on-rails jquery-pagination ruby-on-rails-3
总计Rspec noob在这里.今晚写下我的第一次考试.
我有一个名为Image的模型.使用回形针我附上一个名为photo的文件.标准的东西.我已经运行了回形针生成器,在生产和测试模式下一切正常.
现在我有一个名为image.rb的spec文件,它看起来像这样(它是由ryanb的nifty_scaffold生成器创建的):
require File.dirname(__FILE__) + '/../spec_helper'
describe Image do
it "should be valid" do
Image.new.should be_valid
end
end
Run Code Online (Sandbox Code Playgroud)
这个测试失败了,我意识到这是因为我的模型验证(即validates_attachment_presence)
我得到的错误是:
Errors: Photo file name must be set., Photo file size file size must be between 0 and 1048576 bytes., Photo content type is not included in the list
Run Code Online (Sandbox Code Playgroud)
那么如何在运行测试时告诉rspec上传照片?
我猜它有点与灯具有关......也许不是.我试过和他们玩,但没有运气.为了记录,我在我的fixtures文件夹中创建了一个名为images的文件夹,我想在我的测试中使用的两个文件名为rails.png和grid.png)
我尝试过以下方法:
it "should be valid" do
image = Image.new :photo => fixture_file_upload('images/rails.png', 'image/png').should be_valid
# I've also tried adding stuff like this
#image.stub!(:has_attached_file).with(:photo).and_return( true )
#image.stub!(:save_attached_files).and_return true
#image.save.should …Run Code Online (Sandbox Code Playgroud) 我在rails 3应用程序中使用font-awesome,在开发模式下一切正常,但是当我推送到Heroku时,Firefox无法呈现图标,相反,我看到了:

这就是我所做的:
将以下内容添加到font-awesome.css.scss的顶部:**
// font-awesome.css.scss
@font-face {
font-family: 'FontAwesome';
src: font-url("fontawesome-webfont.eot");
src: font-url("fontawesome-webfont.eot?#iefix") format("eot"),
font-url("fontawesome-webfont.woff") format("woff"),
font-url("fontawesome-webfont.ttf") format("truetype"),
font-url("fontawesome-webfont.svg#FontAwesome") format("svg");
font-weight: normal;
font-style: normal;
}
Run Code Online (Sandbox Code Playgroud)
然后我把它放在application.rb中:
# application.rb
config.assets.paths << Rails.root.join("app", "assets", "fonts")
config.assets.precompile += %w( .svg .eot .woff .ttf )
Run Code Online (Sandbox Code Playgroud)
最后我将所有4个字体文件放入app/assets/fonts.
我真的很想知道我在这里做错了什么.
我在rails应用程序中使用paperclip,并在我的模型中进行以下三种验证
validates_attachment_presence :photo
validates_attachment_size :photo, :less_than=>1.megabyte
validates_attachment_content_type :photo, :content_type=>['image/jpeg', 'image/png', 'image/gif']
Run Code Online (Sandbox Code Playgroud)
如果用户忘记添加附件,则所有三个验证都会失败,因此会向用户显示以下三个错误:
# Photo file name must be set.
# Photo file size file size must be between 0 and 1048576 bytes.
# Photo content type is not included in the list
Run Code Online (Sandbox Code Playgroud)
我认为最好只显示此实例中的第一个错误,因为其他两个错误纯粹是后果...我希望用户只看到后两个错误,如果已添加附件但不符合验证标准.
我确定没有预先出现的验证可以做这种事情,并且通过阅读vendor/plugins/paperclip/lib/paperclip.rb中的代码,我看到validates_attachment_size方法支持:unless参数如下所示:
def validates_attachment_presence name, options = {}
message = options[:message] || "must be set."
validates_presence_of :"#{name}_file_name",
:message => message,
:if => options[:if],
:unless => options[:unless]
end
Run Code Online (Sandbox Code Playgroud)
所以,我在想我可以做以下事情:
validates_attachment_size :photo, :less_than=>1.megabyte, :unless=> :photo.blank
Run Code Online (Sandbox Code Playgroud)
但这打破了应用程序.有没有经验做过这种事情?对回形针源代码有很好的贡献.
编辑:
我试过用这个:
validates_attachment_size :photo, …Run Code Online (Sandbox Code Playgroud) 我有一个带有属性(布尔值)的Post模型和一个带有属性(string)的User模型.有三个角色::publishedroleROLES = %w[admin publisher author]
我不想让角色为作者应能设置的或编辑时,:published在Post模型领域.
我正在使用CanCan(和RailsAdmin gem),我的简化Ability.rb文件如下所示:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
if user.role? :admin
can :manage, :all
elsif user.role? :publisher
can :manage, Post
elsif user.role? :author
# I want to prevent these guys from setting the :published attribute
end
end
end
Run Code Online (Sandbox Code Playgroud)
做这种事的人有什么提示吗?
我已经习惯了在我的身体元素的底部伸出我的JS(按HTML5Boilerplate),但由于TurboLinks,这将是在默认情况下在轨道4,重新加载体(和标题)在每个请求时,它才有意义从现在开始把我的JS放在头上?对此有没有合适的指南或最佳实践?
我有以下javascript:
// create a new article tag
var elem = document.createElement('article');
// append the article to the comments list
document.querySelector('#comments-list').appendChild(elem);
Run Code Online (Sandbox Code Playgroud)
我想设置文章的内容,并添加一些类,所以我正在考虑两种方式:
// Option 1
// set the content using .innerHTML()
// and add the classes manually to the classList
elem.innerHTML = "This is the comment";
elem.classList.add('comment');
// Option 2
// set the content and classes in one go using .outerHTML()
elem.outerHTML = "<article class='comment'>This is the comment</article>";
Run Code Online (Sandbox Code Playgroud)
两者都很好用,但我注意到.innerHTML需要在元素附加到DOM之前调用,outerHTML需要在添加到DOM之后调用它.
我更喜欢第二个选项,因为我实际上是在这个javascript文件中渲染Rails部分,并且有一个细微的情况,它更可取.
我的问题是这些技术中的一种是否优于另一种?将元素添加到DOM然后更改它的HTML是一个问题吗?或者从性能角度来看,在写入DOM之前设置innerHTML是否更好?
我的应用程序托管在Heroku上,我也有一个公共github repo.
我的应用程序有一个包含我的amazon S3凭据的配置文件.确保文件被推送到heroku而不是github是很重要的.
所以我想我可以将我的主分支推送到heroku并创建一个单独的github分支,并确保它的.gitignore文件引用我的s3.yml文件.然后我可以做"git push origin github:master"将github分支推送到github.com
这适用于第一次提交.
但后来我切换到我的主分支,写了一些很棒的代码,然后将它全部推送到heroku.然后我切换回我的github分支并执行"git merge master",以便将新代码添加到其中.但这会导致master分支中的s3.yml和gitignore文件被复制到github分支中.提示从头到桌敲打会议.
是否有任何关于如何保持分支同步,同时确保未跟踪文件保持未跟踪的建议.我可以告诉git不要合并不需要的S3.yml文件和不同的.gitignore文件吗?
我在这里鞭打一匹死马吗?我无法证明支付私人github账户的合理性,但我想这个答案将涉及做到这一点....或者转换为projectlocker
我希望这个问题仅仅取决于我的垃圾技巧,并且有一种方法......提前感谢
编辑:::接受的答案是一个很好的解决方案,但我刚刚发现了一个我更喜欢的新解决方案.在这里阅读它:http://docs.heroku.com/config-vars- Heroku的那些聪明的人有一个答案的一切......真是太棒了
我试图在我的一个rails表单中关闭自动完成功能.这是我到目前为止所尝试的:
# add it at the form level
= form_tag admin_sessions_path, autocomplete: "off" do
Run Code Online (Sandbox Code Playgroud)
那没用.
# add it at the element level
.panel
= label_tag :email
= email_field_tag :email, params[:email], autocomplete: 'off'
Run Code Online (Sandbox Code Playgroud)
也没有那样做.
# add it to both
= form_tag admin_sessions_path, autocomplete: "off" do
# add it at the element level
.panel
= label_tag :email
= email_field_tag :email, params[:email], autocomplete: 'off'
Run Code Online (Sandbox Code Playgroud)
当我访问我的表单时,我的电子邮件地址和已保存的密码已经填写完毕.我在这做错了什么?我喜欢铁轨,但它有时让我很生气.
我在维基百科上看到这个:
数据URI不是单独缓存其包含的文档(例如CSS或HTML文件),因此每次重载时都会下载包含文档的数据.
这是否意味着每次刷新页面或用户点击导航链接时都会下载我的代码?我该怎么做才能缓存数据-uri?
ps - 我只是在谈论20个左右的小png文件(大多数是丝绸图标,但也有2*16KB文件)
paperclip ×2
autocomplete ×1
autofill ×1
cancan ×1
css ×1
data-uri ×1
font-awesome ×1
font-face ×1
fonts ×1
forms ×1
git ×1
github ×1
javascript ×1
jquery ×1
model ×1
pagination ×1
rspec ×1
turbolinks ×1
unit-testing ×1
validation ×1