Tag: ruby

在rails中分配/替换params散列

我在rails控制器动作中有下面的代码序列。 在IF之前,params包含请求参数,如预期的那样。 在它之后,params是零。 谁能请解释这里发生的事情? if false params = {:user => {:name => “user”, :comment => ‘comment’}} end 谢谢。

Rails:模型与迁移中的validation

可能重复: Ruby on Rails:在模型或数据库中validation是否更好? 我看到可以在Rails模型和迁移中添加相同的约束/validation。 但哪一个是最好的方法? 在模型和数据库级别(以及为什么)进行validation是一种好的做法吗? 或者它们在轨道上相同? 例如,我们可以在模型和迁移中对名称进行相同的validation class User true, :presence => true end class CreateUser true, :null => false end end end

ruby-debug19 on ruby​​-1.9.3-preview1

可能重复: Rails 3.1和Ruby 1.9.3p125:ruby-debug19仍然崩溃,“找不到符号:_ruby_threadptr_data_type” 我在使用ruby 1.9.3 preview1版本进行调试时遇到了一些问题。 我使用rvm使用以下命令安装了1.9.3: rvm install 1.9.3 –reconfigure –debug -C –enable-pthread 并安装了ruby-debug19: gem install ruby-debug19 — –with-ruby-include=$rvm_path/src/ruby-1.9.3-preview1 调试gem安装正常,但是当我打开一个调试控制台或尝试任何调试标志rails c –debug或rspec spec –debug 。 我收到以下错误: /Users/mario/.rvm/gems/ruby-1.9.3-preview1@project/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in `require’: dlopen(/Users/mario/.rvm/gems/ruby-1.9.3-preview1@project/gems/ruby-debug-base19-0.11.25/lib/ruby_debug.bundle, 9): Symbol not found: _ruby_current_thread (LoadError) Referenced from: /Users/mario/.rvm/gems/ruby-1.9.3-preview1@project/gems/ruby-debug-base19-0.11.25/lib/ruby_debug.bundle Expected in: flat namespace in /Users/mario/.rvm/gems/ruby-1.9.3-preview1@project/gems/ruby-debug-base19-0.11.25/lib/ruby_debug.bundle – /Users/mario/.rvm/gems/ruby-1.9.3-preview1@project/gems/ruby-debug-base19-0.11.25/lib/ruby_debug.bundle from /Users/mario/.rvm/gems/ruby-1.9.3-preview1@project/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in `block in require’ from /Users/mario/.rvm/gems/ruby-1.9.3-preview1@project/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in […]

如何将主键添加到rails中的表

嗨,我的rails应用程序中有一个表没有任何id列或主键。 多数民众赞成因为这张桌子是从以前的应用程序带入我的rails应用程序,并且该表格预先填充了数据。 我现在意识到没有整数主键,rails无法在浏览器中显示编辑和显示视图。 所以我需要在我的数据库中添加一个id列,它是一个主键并且是自动增量。 根据我的谷歌搜索,我发现了一些关于为表创建主键的post,但没有完成任何内容。 所以我想知道是否有人可以帮助我。 到目前为止,我所知道的是为了更改表,必须编写一个迁移然后运行它。 需要运行以添加id列的命令如下: rails generate migration add_id_to_businesses id:primary_key 但是,运行此命令后,没有新列添加到我的数据库中。 我不知道如何继续..如何在我的数据库中添加新列? 非常感谢一只手…..谢谢,

如何为(所有)Rails生成RDOC?

我可以 sudo gem rdoc activerecord –no-ri 和 sudo gem rdoc actionpack –no-ri 这两个都给了我很好的文档。 但 sudo gem rdoc rails –no-ri 因为Railsgem本身只是其他人的持有者,所以几乎没有给我任何东西。 如何生成相应的http://api.rubyonrails.org/ ?

无法使用Ruby Mechanize登录Amazon

我正在尝试使用Ruby gem Mechanize登录Amazon。 我总是被踢回登录页面而没有任何错误消息。 我想知道这是否是Mechanize的错误,或者亚马逊是否阻止了这种访问。 我有下面的代码,你可以测试。 @mechanizer = Mechanize.new @mechanizer.user_agent_alias = ‘Mac Safari’ @page = @mechanizer.get(“https://www.amazon.com/ap/signin?_encoding=UTF8&openid.assoc_handle=usflex&openid.return_to=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fyourstore%3Fie%3DUTF8%26ref_%3Dpd_irl_gw&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.pape.max_auth_age=0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select”) form = @page.form_with(:id => “ap_signin_form”) field = form.field_with(:name => “email”) field.value = “fake@email.com” radiobutton = form.radiobutton_with(:name => ‘create’, :value => ‘0’) radiobutton.check button = form.button_with(:id => “signInSubmit”) @page = form.submit button 谢谢你的帮助。

Form_for“表单中的第一个参数不能包含nil或为空”错误

我无法弄清楚为什么我会收到这个错误,这究竟是什么意思。 表单中的第一个参数不能包含nil或为空(第3行) 添加新post //Error here 控制器: class PostsController “Your post was saved” else render “new” end end def edit end def update end def destroy end end

Rails按最常用的标签排序(tag.posts.count)

我想显示所有最常用的邮件标签列表。 我的控制器目前有: @tag_list = Tag.all 我的观点是: () 编辑关系如下: Tag (has_many :posts, :through => :taggings) Tagging(belongs_to :tag and belongs_to :post) Post(has_many :tags, :through => :taggings) 这显示了所有带有计数的标签。 我曾尝试使用Tag.order(..)来玩控制器,但似乎不太好。 任何帮助将非常感激。 谢谢。

从rake任务调用控制器

我想从rake任务调用控制器动作。 我的问题是什么方法最好准备http请求? 感谢所有提示。 编辑:请别人提示吗? 我试过这个而不是工作: controller_obj = Controller.new controller.your_method 我有这个例外: rake aborted! uninitialized constant Controller Edit2:我试过了: sess = ActionController::Integration::Session.new sess.post(‘/route’, ‘codes=3′) 但我得到了(我在rake文件中要求’action_controller / integration’): rake aborted! cannot load such file — action_controller/integration

自定义错误页面 – Ruby on Rails

我正在尝试在我的网站中设置自定义错误页面。 我遵循PerfectLine Blog的指导原则。 它适用于控制器存在的情况,但id不存在。 例如,我有一个博客控制器,id 4不存在。 它显示自定义错误页面 但在控制器本身不存在的情况下它不存在。 例如,如果我键入一些带有数字ID的随机控制器,则不会被我在应用程序控制器中设置的方法捕获,以重新路由自定义错误页面。 在这种情况下,我得到一个 ActionController::RoutingError (No route matches “/randomcontrollername”): 在终端和rails附带的默认错误页面中。 application_controller.rb class ApplicationController :render_error rescue_from ActiveRecord::RecordNotFound, :with => :render_not_found rescue_from ActionController::RoutingError, :with => :render_not_found rescue_from ActionController::UnknownController, :with => :render_not_found rescue_from ActionController::UnknownAction, :with => :render_not_found end private def render_not_found(exception) render :template => “/error/404.html.erb”, :status => 404 end def render_error(exception) render […]