Tag: 覆盖

覆盖默认访问者时更新属性的麻烦

我正在使用Ruby on Rails 4,我用这种方式覆盖了一些默认的访问器方法: class Article < ActiveRecord::Base def title self.get_title end def content self.get_content end end self.get_title和self.get_content方法返回一些计算值,如下所示(注意: has_one_association是a :has_one ActiveRecord::Association ) def get_title self.has_one_association.title.presence || read_attribute(:title) end def get_content self.has_one_association.content.presence || read_attribute(:content) end 当我从数据库中找到并读取@article实例时,所有实例都按预期工作: title和content值分别使用self.has_one_association.title和self.has_one_association.content输出。 但是,我发现当属性分配给@article ,@ @article对象不会按预期更新。 也就是说,在我的控制器中,我有: def update # params # => {:article => {:title => “New title”, :content => “New […]

用户登录失败后设计重定向

我知道有一个关于它的维基页面 ,但由于我对Rails很新,我在理解该页面时遇到了很多困难。 我不得不为我的用户覆盖注册控制器。 当用户无法登录时,我希望将他重定向到我的自定义登录页面。 但应用程序将他发送到gem内的登录页面。 我怎么能做到这一点? 我应该把这个课程放在哪里,如何更改? 我有多个模型,每个模型都有不同的登录页面。 如何设置每个型号的范围? class CustomFailure ‘secure’) end # You need to override respond to eliminate recall def respond if http_auth? http_auth else redirect end end 结束 Devise是一个强大的gem,但是一些wiki页面并不认为可以有很多新的程序员

如何重新打开gem中的课程

我在主应用程序中有相同的类和gem(可安装的引擎)。 我想重新打开gem中的类(在主应用程序中定义)。 主要应用有 应用程序/模型/ test.rb 课堂考试 def original_method 结束 结束 在Gemfile中 gem’gem名’ 在gem中 应用程序/模型/ test.rb 课堂考试 def add_method 结束 结束 但是主应用程序引发了一个错误,即没有定义Test#original_method。 看起来Test类被覆盖在gem中。 为什么不重新开放? 我怎么解决这个问题? 编辑 NoMethodError (undefined method `original_method’ for #): vendor/bundle/ruby/1.9.1/gems/activemodel-3.2.13/lib/active_model/attribute_methods.rb:407:in `method_missing’ vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/attribute_methods.rb:149:in `method_missing’ vendor/bundle/ruby/1.9.1/bundler/gems/test_gem/app/controllers/tests_controller.rb:213:in `new’ vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_controller/metal/implicit_render.rb:4:in `send_action’ vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/abstract_controller/base.rb:167:in `process_action’ vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_controller/metal/rendering.rb:10:in `process_action’ vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/abstract_controller/callbacks.rb:18:in `block in process_action’ vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:535:in `_run__422315203817736318__process_action__439125674649202736__callbacks’ vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:405:in `__run_callback’ vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks’ vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:81:in `run_callbacks’ […]

覆盖as_json没有效果?

我试图在我的一个模型中覆盖as_json,部分是为了包含来自另一个模型的数据,部分是为了去除一些不必要的字段。 从我所看到的,这是Rails 3中的首选方法。为了简单起见,让我说我有类似的东西: class Country [:id,:name] ) end end 而在我的控制器中 def show respond_to do |format| format.json { render :json => @country } end end 然而,无论我尝试什么,输出始终包含完整数据,字段不会被“:only”子句过滤。 基本上,我的覆盖似乎没有开始,但如果我改变它,说… class Country < ActiveRecord::Base def as_json(options={}) {foo: "bar"} end end …我确实得到了预期的JSON输出。 我只是弄错了语法吗?

如何在ruby-on-rails中覆盖通用的activerecord错误消息?

在我的en.yml翻译文件中,我有: activerecord: errors: template: header: one: “1 error prohibited this {{model}} from being saved” other: “{{count}} errors prohibited this {{model}} from being saved” 在登录我的应用程序期间发生activerecord / validation错误时,错误消息: “1错误禁止保存此用户会话” 显示(其中user_session是正在使用的模型)。 我宁愿让它说出像 “发生了一次错误,导致您无法登录自己的帐户”。 如何使用我的特定错误消息覆盖常规错误消息?

覆盖Devise的注册控制器,以便在sign_up成功完成后允许重定向

我到处都看了,发现了很多信息……但没有什么对我有用而且我没有得到它:( 我知道您要覆盖注册控制器,如下所示: class Users::RegistrationsController < Devise::RegistrationsController def after_sign_up_path_for(resource) authors_waiting_path end end 然后按照Tony Amoyal http://www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-andcancan-customizing-devise-controllers/显示的示例,我应该改变我的路线更新新控制器的访问权限: devise_for :users, :controllers => { :registrations => “users/registrations” } do #get ‘/author/sign_up’, :to => ‘devise/registrations#new’ #get ‘/client/sign_up’, :to => ‘devise/registrations#new’ get ‘/author/sign_up’, :to => ‘users/registrations#new’ get ‘/client/sign_up’, :to => ‘users/registrations#new’ end 是的,我在这里有点奇怪,因为我正在捕捉一些特定的路径将它们发送到注册页面,这使我能够有效地创建2个注册场景。 在我覆盖注册控制器之前,我评论了我的内容。 即使所有这些和我的authors_waiting_path是一个有效的路径,它只是在注册后继续进入登录页面:( 这真令人沮丧。 亚历克斯 编辑:我也在设计维基上找到了这个: https : //github.com/plataformatec/devise/wiki/How-To : […]

覆盖路由助手方法

问题有很多评论。 URL“questions / 123”显示了一个问题。 一个url: “问题/ 123#答案-345” 显示一个问题并突出显示答案。 345-是Answer模型的id,“answer-345”是HTML元素的id属性。 我需要覆盖“answer_path(a)”方法来获取 “问题/ 123#答案-345” 代替 “答案/ 345” 怎么做 ?

覆盖设计注册创建方法

我想在创建用户时专门设置一个字段。 我有 class RegistrationsController < Devise::RegistrationsController def create super @user.tag_list = params[:tags] end end 我有通过tags参数的复选框,我已经在服务器日志中validation了tags参数正在传递。 但是,当我在控制台中调用@ user.tag_list时,我只得到一个空白的响应[] 。 我觉得问题在于我操纵设计的创造方法。 我没有在任何地方明确设置@user,但我不确定如何使用Devise设置它。 有人在使用设计时知道如何设置特定字段吗?

如何覆盖在Ruby on Rails狂欢商务应用程序中导致问题的文件?

从Rails 3更新到Rails 4并为Spree安装Bootstrap gem后,我在app/views/spree/products/_image.html.erb收到错误。 单击主页上产品的图像后,页面如下所示: NameError in Spree::Products#show Showing []/ruby/1.9.1/bundler /gems/spree_bootstrap-a529d6bb6db0/app/views/spree/products/_image.html.erb where line #1 raised: undefined local variable or method `image’ for #<#:0x000000068d7da8> Extracted source (around line #1): 1 2 3 4 “image” %> “image”) %> Trace of template inclusion: []app/views/spree/products/show.html.erb Rails.root: []/rails/releases/20140118194836 我知道这可以通过更改破坏它的文件来解决,只包含 “image”) %> 。 我了解到如果我想让它们改变,我需要覆盖文件。 所以我想在那里更改代码,我在本地添加了一个具有相同路径和名称的文件( app/views/spree/products/_image.html.erb )和所需的新代码。 然后我部署了它,当我进入服务器时,我看到了文件。 但在网站上我仍然得到同样的错误。 由于模板包含的痕迹说它来自app/views/spree/products/show.html.erb我也将该文件更改为它,但我仍然在服务器上看到相同的代码。 我该如何解决?

通过混合模块来覆盖类的实例方法

给定A类和模块B,混合B的实例方法,使其覆盖A的相应实例方法。 module B def method1 “B\#method1” end def method2 “B\#method2” end end class A def method1 “A\#method1” end def method2 “A\#method2” end # include B does not override instance methods! # (module gets mixed into the superclass) end puts A.new.method1 # want it to print out “B#method1” puts A.new.method2 # want it to print out […]