Tag: ruby on rails 3.1

Rails 3.1按特定顺序加载css

我看到在rails 3.1的开发环境中,css是按字母顺序加载的,而不是按照我想要的顺序加载。 我想要一个特定的css文件在最后,所以它覆盖了之前给予类的任何样式。 我们怎样才能做到这一点?

在rails 3.1上,Rspec中的Cookie不会存在

这是以下环境中控制器规范中的cookie集合的问题: rails 3.1.0.rc4 rspec 2.6.0 rspec-rails 2.6.1 我有一个简单的控制器规范,它创建一个Factory用户,调用一个设置cookie的登录方法,然后测试以查看登录用户是否可以访问页面。 问题是所有cookie似乎都在设置的身份validationcookie和我的控制器上调用的“show”操作之间消失。 在rails dev服务器上的浏览器中运行时,我的代码工作正常。 运行规范时更奇怪的行为是,通过cookie哈希设置的所有内容都会消失,但通过会话哈希设置的所有内容都会持续存在。 我只是错过了使用rspec时cookie的工作方式吗? 规格代码 it “should be successful” do @user = Factory(:user) test_sign_in @user get :show, :id => @user response.should be_success end 登录代码 def sign_in(user, opts = {}) if opts[:remember] == true cookies.permanent[:auth_token] = user.auth_token else cookies[:auth_token] = user.auth_token end session[:foo] = “bar” cookies[“blah”] = […]

跳过Model中的某些validation方法

我正在使用Rails v2.3 如果我有一个型号 : class car < ActiveRecord::Base validate :method_1, :method_2, :method_3 … # custom validation methods def method_1 … end def method_2 … end def method_3 … end end 如上所示,我有3种自定义validation方法 ,我将它们用于模型validation。 如果我在此模型类中有另一个方法,它保存模型的新实例,如下所示: # “flag” here is NOT a DB based attribute def save_special_car flag new_car=Car.new(…) new_car.save #how to skip validation method_2 if flag==true end […]

Rails 3.1:在客户端应用程序中公开引擎助手的更好方法

我找到了一些文章来解决引擎中的助手问题,这些问题不适用于消费(父)应用程序。 为了确保我们都在同一页上,让我们说我们有这个: module MyEngine module ImportantHelper def some_important_helper …do something important… end end end 如果您查看“隔离引擎的助手”(L293)中的rails引擎文档,它会说: # Sometimes you may want to isolate engine, but use helpers that are defined for it. # If you want to share just a few specific helpers you can add them to application’s # helpers in ApplicationController: # # class […]

在1控制器方法(rails)中保存db中的2项

我有一个问题,一次在db中保存2个项目(我正在使用rails 3.1)。 我有模型消息 class Message < ActiveRecord::Base belongs_to :user validates_presence_of :content end 消息模型有params:user_id(接收方),user_from(发送方)和内容(文本) 和型号用户是 class User < ActiveRecord::Base has_many :posts has_many :messages end 在MessagesController我有方法 def create @message =Message.new(params[:message]) User.find(@message.user_id).messages.build(params[:message]) User.find(@message.user_from).messages.build(params[:message]) end 问题是User.find(@ message.user_id)只有这条消息,user_from没有。 我甚至User.find(2).messages.build(params[:message])过这个User.find(2).messages.build(params[:message])而不是User.find(@message.user_from).messages.build(params[:message])和id为2的用户也有这个消息,只有user_id的用户有。 我做错了什么? 提前致谢

在EC2上部署Rails应用程序

在过去的一周里,我一直在玩橡胶gem,似乎无法让它发挥作用。 我已经决定手动设置我的EC2实例会更容易。 问题是我不知道怎么做。 谷歌也没有为新手提供太多帮助。 有什么建议? 真的很感激。

使用Regex和Before_Save删除电子邮件地址

尝试在我的Post模型中使用before_save然后使用正则表达式替换任何看起来像电子邮件的单词’forbidden’。 这是为了减少用户在讨论区中创建的评论/post中的垃圾邮件。 它目前给我一个语法错误; 但我相信它不止于此? 我如何解决它? Post.rb before_save :remove_emails # Prevents and replaces any emails or URLs posted by user as def remove_emails self.post = post.gsub^(((ht|f)tp(s?))\://)?(www.|[a-zA-Z].)[a-zA-Z0-9\-\.]+\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\;\?\’\\\+&%\$#\=~_\-]+))*$, “forbidden”) end

如何在页面加载时停止显示闪存错误消息,直到在轨道上的ruby中单击按钮后?

我正在试图找出一种方法来阻止此Flash消息在页面加载时显示: class SearchesController < ApplicationController def index @users = User.search params[:search] @default_image = "/assets/default_avatar.jpg" if @users.empty? || params[:search].blank? flash[:error] = "Sorry no user(s) found!" if @users.empty? flash[:error] = "Please give us something to search for!" if params[:search].blank? render 'index' end end end 我明白为什么它显示(显然当访问页面时,搜索参数已经是空白)。 在ruby轨道上必须有一些技巧,我可以在这个flash消息上使用它来阻止它点击,直到点击搜索按钮,例如 flash[:error] = “Please give us something to search for!” if params[:search].blank? […]

显示来自Ruby表的数据

我是Ruby的新手,我正在尝试从表中获取数据。 所以看完这个 I have this this result. [#] 所以当我调用Note.text(例如)时,我得到了nil结果。 那么我应该在这里写什么来从数组中获取数据? 谢谢

Michael Hartl的Ruby on Rails教程。 第9.1章中的失败测试

嗨,我正在完成Hartl的Ruby on Rails教程,我在第9.1章中遇到了一个失败的测试。 当我在终端中运行spec时,它返回: sis-macbook-pro:sample_app Lagaspi$ bundle exec rspec spec/ ………………………….F………………………….. Failures: 1) AuthenticationPages signin with valid information Failure/Error: it { should have_link(‘Users’, href: users_path) } expected link “Users” to return something # ./spec/requests/authentication_pages_spec.rb:37:in `block (4 levels) in ‘ Finished in 1.42 seconds 64 examples, 1 failure Failed examples: rspec ./spec/requests/authentication_pages_spec.rb:37 # AuthenticationPages signin with […]