Tag: ruby on rails 4

Rails 4.2无法识别我的模型问题

版本有多大区别! 在我的Rails 3.2应用程序中,我使用了关注点,它们的设置如下: – –app –models category.rb product.rb –concerns –category third_party_a.rb third_party_b.rb –product third_party_a.rb third_party_b.rb –warehouse third_party_a.rb third_party_b.rb 我有同名的问题,命名空间到不同的目录。 这就是我被教导如何做到这一点,它比Rails 4.2默认将一堆Ruby文件集中在一个关注目录中更有意义。 如果我有多个对象需要与多个第三方API交互,那么这是最好的方法。 但是,将此应用程序升级到Rails 4.2,它拒绝工作。 关于这些问题,我收到很多,很多,很多抱怨: Unable to autoload constant Category::ThirdPartyA 它“期望文件定义它”或其他东西。 该文件如下所示: module EbayInteraction extend ActiveSupport::Concern module ClassMethods … 不,那里没有其他命名空间。 但是Rails 3.2并不需要它。 它完美无缺。 所以我进入文件并将声明更改为: module Category::ThirdPartyA 我不知道这是否正确。 我对模块和命名空间感到很困惑,这两个概念对我来说都没有任何意义。 然后我进入模型本身并include的关注: include Category::ThirdPartyA 我越过错误。 然后抱怨ThirdPartyB,所以我也这样做。 等等。 当我完成后,我现在得到这个投诉: […]

播种数据库时“密码不能为空”错误

我正在尝试使用我在这里找到的seed_dump gem抓取的数据来播种我的数据库。 无论如何,生成的种子文件看起来合法,但它似乎与用户模型validation相悖: ActiveRecord :: RecordInvalid:validation失败:密码不能为空 为了给出一个想法,种子看起来像这样: User.create!([ {email: “1234@localhost”, encrypted_password: “$2a$10$5eoHh6M2q4GjGkHClO.NqebWWhS94D8rNj5Ot6CB2qrbn7IrTfkSa”, reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: “2014-12-31 22:27:09”, last_sign_in_at: “2014-12-31 22:27:09”, current_sign_in_ip: “127.0.0.1”, last_sign_in_ip: “127.0.0.1”, admin: false} ]) 此外,我正在使用devisegem进行用户管理。 在给定此filter的情况下是否有一种优雅的方式来播种用户数据

Rails 4 – 所有测试失败(甚至断言为真)

新的rails并且刚刚开始学习测试。 我们遇到了一个问题,可能有一个简单的答案,让我们难过。 我们已经编写了一些代码并使其在开发中工作(只是静态代码和具有Devise的登录系统)。 我们文件中的唯一测试用例是由rails生成的,例如: test “should get about” do get :about assert_response :success end 但是,当我们运行bundle exec rake test ,令我们惊讶的是,我们每次测试都失败了 。 10 runs, 0 assertions, 0 failures, 10 errors, 0 skips 每个测试都有相同的错误: ActiveRecord::StatementInvalid: PG::NotNullViolation: ERROR: null value in column “email” violates not-null constraint DETAIL: Failing row contains (980190962, null, 2015-10-27 09:40:30, 2015-10-27 09:40:30). : INSERT INTO […]

在rails关系数据库中创建和实施has_one关系

在查看的配置文件表上工作。 我有一个问题设置,这是正确的吗? 我对has_many和has_one关系感到困惑。 因为这是一个每个访问过的关系都有一行的表,所以我决定使用has_one。 这看起来是否正确,是否有办法在ActiveRecord中强制执行关系? 模型 class ViewedProfile < ActiveRecord::Base validates :viewed_profile_id, presence: true validates :profile_id, presence: true has_one :profile_id has_one :viewed_profile_id end 移民 class CreateViewedProfile < ActiveRecord::Migration def change create_table :viewed_profiles do |t| t.integer :profile_id t.integer :viewed_profile_id end end end 编辑 此外,当我进入我的控制台并输入ViewedProfile时,没有任何内容出现。 任何想法为什么? = c架构通常应该出现!

Rails设计不会将额外的输入字段保存到数据库

我在使用Devise gem将额外的用户字段值插入数据库时​​遇到问题。 我错过了什么? 我正在使用 ruby 2.1.5p273 Rails 4.1.8 这是我的user.rb模型 class User [:login] attr_accessor :login, :name, :last_name, :address, :post_number, :city, :mobile validates :username, presence: true, length: {maximum: 25}, :uniqueness => { :case_sensitive => false } validates :name, presence:true validates :last_name, presence:true validates :address, presence:true validates :post_number, presence:true validates :city, presence:true validates :country, presence:true validates :mobile, presence:true […]

如何在控制器方法中设置Rails 4中嵌套参数的值?

当我创建新的请求对象时,我有请求模型和嵌套模型filled_cartridges我还需要在create controller方法中设置嵌套params的值。 我有很多嵌套参数,所以我需要迭代它们,类似的东西: params[:request][:filled_cartridges_attributes].each do |_,value| # here i try to set :client_id parameter, which is nested # where @client.id is defined value[:client_id] = @client.id # i am pretty sure that the problem is here # is it a correct way of ding that? end 编辑: has_many :filled_cartridges, inverse_of: :request, dependent: :destroy accepts_nested_attributes_for :filled_cartridges, reject_if: […]

插入的命名路由。 代替 /

我在rails 4中的命名路由遇到了问题( 非资源嵌套的命名路由 )。 我已经转移到其他东西,但仍然在努力解决非资源url的命名路由问题。 这是我从rake routes : GET /messages/:id/report/:reply_token(.:format) messages#report messages POST /messages(.:format) messages#create 和我的routes.rb resources :messages, only: [:create] do member do get ‘report/:reply_token’, :action => ‘report’#, :as => :message end end 由于我在顶部链接的post中遇到的问题,我正在尝试通过执行以下操作获取/ messages /:id / report /:reply_token路由的URL: “#{messages_url(@message, :host => “localhost:3000″)}/report/#{@message.reply_token}” 但它给了我这个: http://localhost:3000/messages.110/report/6bBw22TdaRYcQ3iVzW1ZwA 为什么会有。 在’messages’和’110’(message_id)之间? 我还在messages_url()尝试了@ message.id,而不是@message。 我也尝试了这个: report_message_path(message_id: @message.id, reply_token: @message.reply_token)但是得到了与上面链接的问题相同的错误。 我也尝试过message_url()但它给出了undefined […]

会话值在重定向rails 4后重置

我正在使用设计3.5.1,rails 4.0.0和ruby 2.0.0-p0。 如果用户未登录并尝试打开需要身份validation的私有页面,请使用lvh.me:3000/users/1/edit ,然后设计将针对关键user_return_to在会话中添加users/1/edit 。 问题是当用户在身份validation失败后被重定向到登录页面时,会话中的user_return_to值会重置。 我的应用程序遍布此问题。 如果我在会话中存储值并从同一操作重定向到另一个路径,则会话值将消失。 注意:我已将rails应用程序从(rails 3.0.4,ruby 1.8.7)升级到(rails 4.0.0,ruby 2.0.0)。 对不起,问题中有任何含糊之处。 如果您有任何疑问,请发表评论。 更新: 在我的应用程序控制器中,我添加了一个beforefilter来validation用户身份。 应用程序控制器中仅显示相关代码。 application_controller.rb class ApplicationController :devise_controller? # rest of the code end 我正在使用inheritanceDevise::FailureApp的自定义失败类。 代码看起来像这样。 custom_failure.rb class CustomFailure < Devise::FailureApp def redirect_url eval "new_user_session_path" end def redirect store_location! flash[:alert] = i18n_message # session[:user_return_to] returns '/foobar' here. redirect_to redirect_url end […]

在对custom.css.scss进行微小更改后,Bootstrap无法正常工作

与此问题类似: rails:对custom.css.scss的任何更改都会导致应用程序崩溃 我已经对custom.css.scss文件进行了更改,并且所有对颜色的引导引用都停止了。 我现在已手动输入这些颜色的hex值并加载应用程序,但我的导航栏的类现在无法正常工作。 由于某种原因,我现在有一个引用任何引导类的问题。 我也不明白为什么对样式表进行微小的更改会导致我的引导样式陷入崩溃。 我想我必须在其他地方遇到设置问题。 custom.css.scss: @import “bootstrap”; /* mixins, variables, etc. */ $grayMediumLight: #eaeaea; $gray-darker: #222; $gray-dark: #333; $gray: #555; $gray-light: #999; $gray-lighter: #eee; 相关的gem gem ‘rails’, ‘4.0.2’ gem ‘bootstrap-sass’, ‘2.3.2.0’ gem ‘sprockets’, ‘2.11.0’ gem ‘sass-rails’, ‘4.0.1’ gem ‘uglifier’, ‘2.1.1’ gem ‘coffee-rails’, ‘4.0.1’ gem ‘jquery-rails’, ‘3.0.4’ gem ‘turbolinks’, ‘1.1.1’ 除此之外,我最近将bootstrap.css和bootstrap.min.css文件添加到vendor / assets […]

Rails webrick force-exits:未初始化的常量WebConsole :: Railtie :: Middleware(NameError)

所以我是ruby的新手,试图第一次启动webrick。 我刚刚用mysql2修复了分段错误错误,并遇到了这个新的退出问题。 这是命令行读数: G:\ Ruby Development \ simple_cms> G:\ Ruby Development \ simple_cms> rails服务器 =>启动WEBrick => Rails 4.2.1应用程序从http:// localhost:3000开始开发 =>运行rails server -h以获取更多启动选项 => Ctrl-C关闭服务器 退出 C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/web-console-2.1.2/lib/web_console/railtie.rb:21:in`block in’:未初始化的常量WebConsole :: Railtie: :中间件(NameError) 来自C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/initializable.rb:30:在`instance_exec’中 来自C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/initializable.rb:55:在`block in run_initializers’中 来自C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/initializable.rb:30:在’run’中 来自C:/Ruby22-x64/lib/ruby/2.2.0/tsort.rb:226:inclock in tsort_each’ 来自C:/Ruby22-x64/lib/ruby/2.2.0/tsort.rb:348:在每个_trtrongly_connected_component中的’block(2个级别)’ 来自C:/Ruby22-x64/lib/ruby/2.2.0/tsort.rb:429:在`each_strongly_connected_component_from’中 来自C:/Ruby22-x64/lib/ruby/2.2.0/tsort.rb:347:在`block in each_strongly_connected_component’中 来自C:/Ruby22-x64/lib/ruby/2.2.0/tsort.rb:345:在’each’中 来自C:/Ruby22-x64/lib/ruby/2.2.0/tsort.rb:345:在`call’中 来自C:/Ruby22-x64/lib/ruby/2.2.0/tsort.rb:345:在’each_strongly_connected_component’中 来自C:/Ruby22-x64/lib/ruby/2.2.0/tsort.rb:224:in`tsort_each’ 来自C:/Ruby22-x64/lib/ruby/2.2.0/tsort.rb:203:在`tsort_each’中 来自C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/initializable.rb:54:in”run_initializers’ 来自C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/application.rb:352:在`initialize!’ 来自G:/ Ruby Development / simple_cms […]