Tag: ruby on rails 4

覆盖渲染:json在没有模型对象的Rails 4中

我需要覆盖“渲染json”,但我不需要为模型覆盖它,在模型中我知道我可以打开ActiveRecord :: Base并重写as_json方法。 但我的问题是当我有这样的事情 class TestController < ApplicationController def index render json: { field: 'is a test' } end end 我的问题是因为我使用了一个gem,其中一些方法我需要返回“渲染json”,而这个gem没有集中一个方法。 我认为在做py的fork并重构这个因为我可以覆盖集中式方法,但首先我想知道是否有可能覆盖渲染。

rails中模型属性的保护级别

假设有以下情况 class User < ActiveRecord::Base private def password= p self[:password] = p end def password self[:password] end end 如果有权访问Rails控制台的任何人都可以: Loading development environment (Rails 4.0.0) 2.0.0p247 :001 > User => User(id: integer, name:string, password:string) 2.0.0p247 :002 > u = User.find(1) => # 2.0.0p247 :003 > u.password = “123” NoMethodError: private method ‘password’ called for # 2.0.0p247 […]

Rails 4 x paper_trail:使用嵌套资源按item_id过滤版本

在我的Rails 4应用程序中,我有以下模型: class User < ActiveRecord::Base has_many :administrations, dependent: :destroy has_many :calendars, through: :administrations end class Administration < ActiveRecord::Base belongs_to :user belongs_to :calendar end class Calendar < ActiveRecord::Base has_many :administrations, dependent: :destroy has_many :users, through: :administrations end class Post < ActiveRecord::Base belongs_to :calendar end 我安装了paper_trail gem来跟踪我的post模型的变化,如文档中所述 ,它就像一个魅力。 版本显示在Calendars#Index视图中,该视图用作user的仪表板。 以下是我的CalendarsController的设置方法: def index @user = current_user @calendars […]

Rspec未定义的局部变量或方法root_path

我开始使用Rspec,但是当我运行bundle exec rspec我收到一个错误 /spec/requests/pages_spec.rb:20:in `block (2 levels) in ‘: undefined local variable or method `root_path’ for # (NameError) 我没有运行Spork或Guard,所以下面的问题不适用 未定义的局部变量或方法`root_path’(Rspec Spork Guard) 我在spec_helper.rb文件中添加了config.include Rails.application.routes.url_helpers ,但这没有帮助。 undefined局部变量或方法`root_path’Hartl的教程第5.3.2节 这是pages_spec.rb require ‘spec_helper’ describe “Pages” do describe “navigation” do def self.it_should_be_on(path_name, value=nil) before { visit path_name } it “should be on #{path_name}” do page.should have_link(‘Home’) page.should have_link(‘Inventory’) page.should have_link(‘FAQ’) […]

Validates_Overlap Gem多个范围覆盖彼此

我正在使用Validates_Overlap Gem,可以在这里找到: https : //github.com/robinbortlik/validates_overlap 实质是我有两个房间可以预订。 我希望validation在同一房间已经在SAME房间进行确认预订时介入。 如果预订了其他房间,或者预订了相同的房间但尚未确认,则不应该给我一个错误。 到目前为止,我的代码如下 validates :start_time, :end_time, :overlap => { :exclude_edges => [“starts_at”, “ends_at”], :scope => { “bookings.studio_id” => proc {|booking| booking.studio_id}} && { “bookings.is_confirmed” => proc {|booking| booking.is_confirmed == true}} }, on: :update 这将从我的服务器返回以下内容: Booking Exists (0.4ms) SELECT 1 AS one FROM “bookings” WHERE ((bookings.end_time IS NULL OR bookings.end_time […]

如何在Rails中为集成测试编写帮助程序?

如何编写在多个集成测试中使用的集成测试助手? 我尝试了以下错误。 我正在考虑创建一个基类并扩展它,但我不明白’test_helper’是如何工作的! 我不能将帮助器方法放在test_helper中,因为它们使用特殊的集成助手,如post_with_redirect 。 LS $ ls test/integration integration_helper_test.rb post_integration_test.rb user_flows_test.rb 代码,integration_helper_test.rb class IntegrationHelperTest < ActionDispatch::IntegrationTest def login(user) … 代码,post_integration_test.rb require ‘test_helper’ require ‘integration_helper_test’ # require ‘integration/integration_helper_test’ class PostIntegrationTest < ActionDispatch::IntegrationTest # include IntegrationHelperTest 错误 $ rake rake aborted! cannot load such file — integration_helper_test C:/Users/Chloe/workspace/SeenIt/test/integration/post_integration_test.rb:2:in `’ Tasks: TOP => test:run => test:integration 代码,post_integration_test.rb […]

接受用于填充`url_for`方法的URL参数是否安全?

我正在使用Ruby on Rails 4.1.1,我想接受直接传递给url_for方法的参数(通过URL查询字符串),这样: # URL in the browser http://www.myapp.com?redirect_to[controller]=users&redirect_to[action]=show&redirect_to[id]=1 # Controller … redirect_to url_for(params[:redirect_to].merge(:only_path => true)) 采用上述方法,可以在执行操作后重定向用户。 但是,我认为人们可以输入可能导致安全问题的任意params … 接受用于填充url_for方法的URL参数是否安全? 什么是陷阱? 在最坏的情况下会发生什么? 通过在对我的应用程序的请求期间记录params ,我注意到Rails 总是添加:controller和action参数。 也许确认url_for可以使用上面的方式,因为它在内部受到保护并且像Rails一样工作。

Rails 4 ActiveRecord。其中一个数组中的任何元素都在另一个数组中

我有一个看起来像这样的ID列表: feed_ids = [1,2,3,4,5] # will be filled with random ids 我有一个Post模型,其属性parent_ids可能如下所示: parent_ids = [20,14,1] 我想检索parent_ids中的元素与feed_ids中的元素匹配的所有记录 我试过这个,但它不起作用: nodes = Post.where(parent_ids: feed_ids) 它没有给我一个错误,但它也没有返回任何记录。

故障排除friendly_id w / Rails 4:在rails控制台中按预期工作,从控制器获取RecordNotFound错误

我已经成功地在我的Rails 4应用程序中使用了带有多个模型的friendly_id。 我之前有两个我决定合并的模型(终端和位置)。 终端显示行动以前使用的是friendly_id,在那里我可以访问/终端/奥尔巴尼并使用slug’albany’获得终端。 我已经通过大部分重构压缩到只有Locations,并且在最后一块(修复视图)。 一路上我一直在Rails控制台中测试并使用rspec,事情似乎按预期工作。 还有其他模型仍然可以按预期工作。 现在我正在为Locations的新显示页面工作,我在尝试访问/ locations / albany时遇到以下错误: ActiveRecord::RecordNotFound in LocationsController#show Couldn’t find Location with id=albany 但是,当我访问/ locations / 5时,它成功加载了记录。 它也适用于Rails控制台: Location.find(5) # returns the correct location object Location.find(5).slug # returns ‘albany’ Location.friendly.find(‘albany’) # returns the correct location object 当我查看Rails dbconsole中生成的slugs时,它们看起来都是正确生成的并且没有重复项。 这是我的模特: Class Location < ActiveRecord::Base extend FriendlyId friendly_id :slug_candidates, use: [:slugged, […]

为什么我的Heroku应用程序没有使用Sendgrid在生产中发送电子邮件?

我的设计电子邮件在开发中运行良好。 但是现在我已经推送到Heroku而且正在使用sendgrid附加组件,它们不会被发送。 我没有收到错误。 它似乎发送得很好,只是它从未实际到达我的收件箱。 这是我的config/environment/production.rb文件: config.action_mailer.delivery_method = :smtp config.action_mailer.perform_deliveries = true config.action_mailer.raise_delivery_errors = false config.action_mailer.default :charset => “utf-8” config.action_mailer.default_url_options = { :host => ‘http://myapp.herokuapp.com’ } config.action_mailer.smtp_settings = { :user_name => ENV[“SENDGRID_USERNAME”], :password => ENV[“SENDGRID_PASSWORD”], :address => ‘smtp.sendgrid.net’, :domain => ‘myapp.herokuapp.com’, :port => 587, :authentication => :plain, :enable_starttls_auto => true } 我在Heroku上检查了那些配置变量,它们返回了有效的结果。 有任何想法吗? PS我还没有指向Heroku App的域名,我只想先用Heroku设置测试它。