Tag: ruby on rails

无法找到已弃用的RAILS_ROOT的位置

当我加载rails( rails s )时,终端中显示的第一行是: DEPRECATION WARNING: RAILS_ROOT is deprecated. Please use ::Rails.root.to_s. (called from /Users/x/Sites/x/config/application.rb:7) 我一直无法隔离使用它的地方。 上面提到的第7行是: Bundler.require(:default, Rails.env) if defined?(Bundler) 有没有人有任何建议?

控制器如何手动设置某个字段的validation错误

我有一个包含3个ActiveRecord字段的表单。 其中一个领域有一些愚蠢的,以及STATE-DEPENDENTvalidation要求。 (例如,如果在安装向导表单上创建对象,我只validation该字段。) 在我的POST处理程序中创建对象,我想我可以调用errors.add来插入一个特殊的错误条件 @foo = Foo.new(params[:foo]) if goofy_conditions(params[:foo][:goofy_field]) @foo.errors.add(:goofy_field, “doesn’t meet the goofy conditions” ) end respond_to do |format| if @foo.save … else … redirect back to form (with error fields hilited) 但是,在控制器中执行@ foo.errors.add()似乎没有做任何事情……如果其他字段通过validation,它不会阻止save()。 另一种方法是将自定义validation处理程序放入模型中……我知道使用errors.add(:field,’msg’)可以正常工作……但在这种情况下,我的控制器如何将’传递’信息传递给validation器告诉它该字段是否需要validation。

从数组中删除空白元素

当我从轨道上保存多个ruby时,它似乎在前面添加了一个空白元素。 我该如何删除它? 该字段是selected_player。 {“utf8″=>”✓”, “authenticity_token”=>”H8W7qPBezubyeU0adnTGZ4oJqYErin1QNz5oK0QV6WY=”, “schedule”=>{“event”=>”1”, “result_id”=>””, “time”=>”26/10/2012”, “duration”=>”15”, “arrival_time”=>”14”, “location_id”=>”25”, “selected_players”=>[“”, “38”, “41”], “team_id”=>”1”, “opponent_id”=>”7”, “home_or_away”=>”Home”}, “commit”=>”Save Event”} 调节器 def update @schedule = Schedule.find(params[:id]) @user = User.find(current_user) @players = User.where(:team_id => current_user[:team_id]).all respond_to do |format| if @schedule.update_attributes(params[:schedule]) Notifier.event_added(@user,@schedule).deliver format.html { redirect_to(@schedule, :notice => “#{event_display_c(@schedule.event)} vs #{@schedule.opponent.name} was successfully updated.”) } format.json { head :no_content […]

Rails 3.1中的授权:CanCan,CanTango,declarative_authorization?

我查看了declarative_authorization,CanCan和CanTango。 他们都善于为应用程序添加授权,但我想知道如何将授权添加到模型的特定实例,即一个人可以在一个项目中拥有管理访问权限并且仅限于(读取少于管理:有限更新等)在另一个。 你能以更好的方式取悦吗? 如果我的问题听起来太微不足道,请道歉。 这可能是因为我是RoR的新手。 谢谢,约翰

对于#<ActiveRecord :: ConnectionAdapters :: MysqlAdapter,未定义的方法`explain'

我是Ruby on Rails的新手,但我已经学过一些教程,并且知道我的方法。 我已经生成了一些脚手架并将数据插入到MySql数据库中。 导航到index.html.erb时,我收到标题中的错误 控制器正在执行索引 def index @beers = Beer.all respond_to do |format| format.html # index.html.erb format.json { render :json => @beers } end end 并将此作为一种结构 Beer: id, brewer_id, name, price, score, color, brew_type, create_at, updated_at RoR正在为我创建的其他脚手架工作,并列出数据。 我更新了实体Beer的mysql结构,它可能没有反映rails的变化(dunno)。 我是否需要一个不同的gem来将rails连接到mysql db? 任何关于检查什么的建议都将受到赞赏(:

会话是否可以在Heroku上使用多个web dynos?

如果您在Heroku上运行带有多个Web dynos的Rails 3应用程序, 每次你点击应用程序,你通常会连接不同的网络动态? 会话可以在不同的网络动态中工作吗? 它适用于不同的Rails会话存储(ActionDispatch :: Session :: CookieStore,ActiveRecord :: SessionStore和ActionDispatch :: Session :: CacheStore)

Button_to使用POST Link_to使用GET,为什么? ROR

我使用link_to遇到了一个问题。 在link_to参数中指定我的“method”=>“post”后,为什么我的链接使用GET方法而我的button_to使用POST方法? 视图: ‘getquote’ %> ‘inventories’, :action => ‘getquote’, :method => :post } %> 控制器方法: def getquote @cart = find_cart respond_to do |format| format.pdf end end 终端输出(分别为按钮/链接): Processing InventoriesController#getquote (for 127.0.0.1 at 2010-01-30 01:38:02) [POST] Parameters: {“action”=>”getquote”, “authenticity_token”=>”D2cwnHyTHgomdUM3wXBBXlOe4NQLmv1Srn0paLbExpQ=”, “controller”=>”inventories”} Processing InventoriesController#show (for 127.0.0.1 at 2010-01-30 01:39:07) [GET] Parameters: {“method”=>”post”, “action”=>”show”, “id”=>”getquote”, “controller”=>”inventories”}

嵌套资源的表单

我已经完成了大量的form_for嵌套资源问题,无法让任何解决方案适合我。 我想是时候问一个个性化的问题了。 我有两个模型,工作和问题,工作有很多问题和问题属于工作。 我使用scaffolding创建控制器和模型,然后嵌套routes.rb中的资源。 root :to => “pages#home” resources :jobs do resources :questions end get “pages/home” get “pages/about” get “pages/contact” class Job < ActiveRecord::Base has_many :questions end class Question < ActiveRecord::Base belongs_to :job end 现在我正在尝试访问’/ jobs / 1 / questions / new’并继续获取 问题#new中的NoMethodError 当代码出现时,我开始使用错误No route matches {:controller =>“questions”} 我知道这是错的,所以我开始尝试其他组合,但没有一个工作。 我试过了 那 那 在一堆其他组合中,并没有工作。 这是我的rake路线的链接:git clone […]

Rails关联中的未知密钥

我有以下协会代码: has_many :rates_without_dimension, :as => :rateable, :class_name => “Rate”, :dependent => :destroy, :conditions => {:dimension => nil} has_many :raters_without_dimension, :through => :rates_without_dimension, :source => :rater has_one :rate_average_without_dimension, :as => :cacheable, :class_name => “RatingCache”, :dependent => :destroy, :conditions => {:dimension => nil} dimensions.each do |dimension| has_many “#{dimension}_rates”, :dependent => :destroy, :conditions => {:dimension => dimension.to_s}, :class_name […]

从Windows将rails应用程序连接到SQL Server 2005

我(遗憾地)必须在Windows XP上部署一个必须连接到Microsoft SQL Server 2005的rails应用程序。 在网上冲浪有很多从Linux到SQL Server连接的点击,但无法从Windows中找到如何做到这一点。 基本上我遵循以下步骤: 安装dbi gem 安装activerecord-sql-server-adapter gem 我的database.yml现在看起来像这样: development: adapter: sqlserver mode: odbc dsn: test_dj host: HOSTNAME\SQLEXPRESS database: test_dj username: guest password: guest 但我无法连接它。 当我运行rake db:migrate时,我得到了 IM002 (0) [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified 我不是Windows用户,因此无法理解dsn元素的含义。 有人知道如何解决这个问题吗? 提前致谢 现在我已经将Alexander.yml修改为: development: adapter: sqlserver mode: odbc […]