Tag: ruby on rails 4

使用rails_admin为日期字段添加默认值

使用rails (4.2.10)和rails_admin (1.2.0)我试图将默认日期值添加到User模型的confirmed_at字段。 它适用于编辑视图,但不适用于新视图。 这是我的代码: config.model ‘User’ do list do […] end show do […] end edit do field :confirmed_at do default_value DateTime.now end exclude_fields […] end end

Send_file在浏览中打开文件而不是下载它

请告诉我这是如何工作的,因为我不明白……这是我的简单代码: 路线: get ‘download’ =>’pages#download’ 我的行动: def download send_file “#{Rails.root}/public/downloads/robots.zip”, :type=>”application/zip” 并下载链接: 所以,当我点击链接时,我得到了这个: 图片http://sofzh.miximages.com/ruby-on-rails/GrqvQ3xHlbaYmz.jpg 看起来浏览器正在尝试显示zip文件内容……但是当我从浏览器(或刷新页面)打开操作时,它工作正常。 当我点击link_to生成的链接时,为什么它不起作用?

Rails:NameError:未初始化的常量OrderItem

车型/ order_item.rb class OrderItem < ActiveRecord::Base belongs_to :item belongs_to :order belongs_to :user end 车型/ order.rb class Order < ActiveRecord::Base has_many :order_item end 现在,我使用rails console测试它们。 当我键入OrderItem ,它抛出了 NameError: uninitialized constant OrderItem 更新 我在rails console 2.1.3 :021 > reload! Reloading… => true 2.1.3 :022 > Order => Order(id: integer, user_id: integer, created_at: datetime, updated_at: datetime, status: integer) […]

Rails 4 Unicorn服务应用程序没有ActiveRecord

我正在构建一个不使用任何数据库的rails 4应用程序。 我已经在我的开发机器上成功禁用了ActiveRecord,方法是在线删除database.yml并替换几个指南 require ‘rails/all’ 同 require “action_controller/railtie” require “action_mailer/railtie” require “rails/test_unit/railtie” require “sprockets/railtie” 它在本地工作,但是当我尝试在运行独角兽的服务器上部署它时,我在错误日志上得到了这个 ERROR — : ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished) /home/rtb/shared/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:546:in `retrieve_connection’ 当我启用了database.yml和activerecord时,应用程序在生产的unicorn服务器上工作正常。 有什么我想念的吗?

为什么我在使用protected_attributes gem时得到ForbiddenAttributesError

我们刚刚从4.0.1升级到rails 4.1.13。 我们一直在努力实施强大的参数。 因此,我们仍然使用protected_attributes gem,因此我们可以使用旧的行为。 现在,我们得到一个ActiveModel::ForbiddenAttributesError 。 我不明白为什么,因为我们使用的是protected_attributes gem。

为什么Rails 4.0 RC1中没有资产/图像目录

我刚刚发现当我生成Rails 4应用程序(目前是Rails 4 rc1)时,没有默认的目录assets/images 。 而在Rails 3.1 / 3.2中则生成了它。 请告诉我为什么会这样? 我的意思是我可以在一秒钟内创建一个目录,但这不是重点。 如果我没有在任何公告中得到这个原因,我很抱歉,否则对我来说没有意义。 谢谢

对于嵌套的父/子模型,Rails会以什么顺序进行模型回调?

我知道保存模型的一般顺序是最深的孩子,然后逐渐到达父母。 但我对其他回调感到疑惑,是否会发生以下情况: ChildA – before validation ChildB – before validation Parent – before validation ChildA – after validation ChildB – after validation Parent – after validation ChildA – before save ChildB – before save Parent – before save … 或者沿着以下方向: ChildA – before validation ChildA – after validation ChildA – before save … ChildB – […]

ror4表单中的第一个参数不能包含nil或为空

当我点击编辑时我得到:表格中的第一个参数在bets / app / views / users / _form.html.erb中不能包含nil或为空 用户/ _form prohibited this user from being saved: 在users_controller中 class UsersController < ApplicationController def new @user = User.new end def create @user = User.new(user_params) if @user.save flash[:notice] = "You have signed up successfully." redirect_to posts_path else render :new end end def show @user=User.find(params[:id]) end def update […]

Rails – 如何将.new / .save更改为.create

我有两个模型:Schedule和Project。 项目has_one计划和计划belongs_to项目。 当我创建一个时间表时,我会这样做: def create @schedule = Schedule.new(schedule_params) @schedule.project = Project.find(params[:project_id]) if @schedule.save flash[:notice] = “Successfully created schedule.” redirect_to profile_path(current_user) end end 这很有效。 但是,我添加了一个after_create回调和一个after_update回调来发出通知。 创建计划时创建“新计划”通知,更新计划时“计划已更新”通知。 问题是在控制器中我使用@ schedule.new和@ schedule.save,而不是@ schedule.create。 我需要更改我的控制器代码以使用.create,以便after_create回调可以工作。 我已经尝试过使用after_save回调,但是每当更新计划时都会调用它,这样就无法工作了。 由于我定义@schedule和@ schedule,project的方式,我无法弄清楚如何更改上面的代码以使用@ schedule.create。 有没有人有任何想法? 谢谢。

Rails 4最佳选择集合

我似乎无法在任何地方找到我的问题的答案,所以请帮助我,如果有人可以我试图更好地理解这一点。 我有一个方法将变量传递到另一个模型的方法,以从表中获取packlist_id。 比如在我的发票模型中我有: def packlistid Shipperline.packlist(@customerid, @shipqty) end customerid来自我的Invoice类中的另一个方法(效果很惊人),shipqty也来自我所做的另一个方法查询。 然后该方法进入Shipperline模型,该模型有一个方法self.packlist(customerid,shipqty): def self.packlist(customerid, shipqty) Shipperline.joins(:customerorderline, :shipper).select(“cust_order_id”).where(cust_order_id: customerid).pluck(:packlist_id).uniq end 然后在我看来,我有: best_in_place invoice, :packlsitid, :as => :select, :class=>”best_in_place”, :collection => invoice.packlistid 这是很好的原因,我得到一个下降做选择框填充数字,如 76433 53422 43156 34231 但是,当我选择一个如76433时,进入数据库的值是1.我希望它是完全相同的值76433。 select时53422将数值2放在数据库中,依此类推。 这是具有最佳版本3.0.1的rails 4 如果有人可以帮助我,我会非常感激。