Tag: ruby on rails 4

从代码内生成Rails模型(从控制器调用生成器)

我特别需要能够从代码中调用Rails命令(即发生某些操作时)。 我需要使用特定字段创建模型(由表单确定)并最终运行创建的迁移。 所以这个表单我会创建所有字段,然后创建一个带有某些字段(表和列)的模型 那么,有没有办法调用rails generate model NAME [field[:type][:index] field[:type]并bundle exec rake db:migrate从控制器/ ruby​​代码中bundle exec rake db:migrate ?

Rails与模型和迁移相关联

无法理解Rails如何与模型联系起作用。 这是一个简单的问题: 有两张桌子 制品 id| name | status 1 | Tube | 0 2 | Pillar | 1 3 | Book | 0 4 | Gum | 1 5 | Tumbler | 2 状态 status | name 0 | Unavailable 1 | In stock 2 | Discounted 与模型和控制器相同的名称。 我不知道在每个新产品的状态表中创建新行的内容。 我想在erb中显示状态名称。 我应该在模型和迁移文件中写什么(例如,belongs_to,has_one或has_many ……)?

Ruby委托类/关系级方法

编辑 – 我改变了面向对象的建模,以更好地反映我的应用程序中非特别直观的关系,(谢谢,安东尼),这样这个问题更有意义。 对于那个很抱歉。 在我的Rails应用程序中,我希望某些模型能够不仅在实例级别而且在类/关系级别上委托给其他模型。 也就是说,假设一个House模型,它具有多个Users并定义一个类级别的方法“ addresses ”,这个方法被称为houses关系,我可以这样做: users.addresses 在幕后,这实际上会做两件事:1)运行users.houses (它们会抓住所有带有ID的房子,从用户关系的house_id列中获取的那些),以及2)关于房屋关系的呼叫addresses 。 我的尝试目前看起来像这样: class House has_many :users def self.addresses map(&:address) end def address “#{street_address}, {state}, #{country}” end end class User belongs_to :house delegate :address, to: :house class << self delegate :addresses, to: :houses end def self.houses Houses.where(id: pluck(:house_id)) end … end 这基本上似乎有效 – 差不多。 如果我有一组用户,我可以做users.houses并获取相关的houses关系。 […]

没有路由匹配“/ contact1”

我是rails的新手,并且在Rails 4.2.3应用程序中遇到了上述错误。 我在更新资源时收到此错误: No route matches [PATCH] “/contact.1” 这是我的应用程序代码片段: 的routes.rb resources :contacts 车型/ contact.rb class Contact < ActiveRecord::Base belongs_to :user validates :name, presence: true, length: { maximum: 50 } validates :number, presence:true end 意见/联系人/ edit.html.erb Editing Contact 控制器/ contacts_controller.rb def edit @contact = Contact.find(params[:id]) end def update @contact = Contact.find(params[:id]) if @contact.update(contact_params) flash[:success] = “Contact […]

nomethoderror undefined方法’configure’应用程序

我正在努力推动我的应用程序做Heroku。 我创建了运行“heroku create”并顺利运行,它在我们的网站上的应用程序中显示。 当我运行“git push heroku master”时,我收到此错误。 我所看到的与ProjectOne :: Application相同的“NoMethodError:undefined方法`configure”的所有内容都只是进入development.rb文件夹并进行“ProjectOne :: Application.configure do”。 我被困在这里,找不到任何可以帮助我的东西。 据我所知,no方法错误是推送失败的地方,据我所知,到目前为止,我已经修复了问题。 如果错误在代码中的其他地方或我误解了什么,请告诉我! 谢谢 :) C:\Users\Devin Miller\project_one>git push heroku master Warning: Permanently added the RSA host key for IP address ‘50.19.85.132’ to the list of known hosts. Initializing repository, done. Counting objects: 63, done. Delta compression using up to 8 threads. Compressing […]

具有虚拟属性的活动模型序列化程序 – Rails 4

我目前正在使用RoR制作API,我需要创建一个具有虚拟属性和关联对象的对象。 问题是当我返回具有虚拟属性的对象时,序列化程序不会启动。 这是foo_controller返回的对象 { :id=>280, :virtual=>”y8st07ef7u” :user_id=>280 } :virtual是虚拟属性,user_id是关联表的ID – User。 我的目标是做到这一点 { :id=>280, :virtual=>”y8st07ef7u”, :user=>{ :id=>280, :name=>’foo’ } } Foo_controller设置 class Api::V1::FoosController :virtual), status: 200 end Foo_model设置 class Foo < ActiveRecord::Base belongs_to :user attr_accessor:virtual def set_attribute(path) self.virtual = path end end Foo_serializer设置 class FooSerializer < ActiveModel::Serializer attributes :id, :virtual has_one :user end Foo迁移设置 class […]

BUG Segmentation fault ruby​​ 2.0.0p481(2014-05-08修订版45883)x86_64-darwin13.2.0

尝试运行rails s或rails c时收到错误。 我最近通过自制软件和rbenv安装了ruby和rails。 当我运行rails new project ,应用程序已成功创建。 然后我在相应的目录中进行bundle install ,当我运行rails s或rails c时,一切都顺利更新。 出现以下错误: |14:48:23| ~ rails c /Users/jona/.rvm/gems/ruby-2.0.0-p481/gems/json-1.8.1/lib/json/ext/parser.bundle: [BUG] Segmentation fault ruby 2.0.0p481 (2014-05-08 revision 45883) [x86_64-darwin13.2.0] — Crash Report log information ——————————————– See Crash Report log file under the one of following: * ~/Library/Logs/CrashReporter * /Library/Logs/CrashReporter * ~/Library/Logs/DiagnosticReports * /Library/Logs/DiagnosticReports the more detail […]

puma初始化程序不适用于rails 4.2

我之前已经多次安装过puma并且从未遇到过这个问题。 我按照heroku的说明逐字记录。 我在里面创建了一个Procfile : web: bundle exec puma -C config/puma.rb 这是配置文件puma.rb: workers Integer(ENV[‘WEB_CONCURRENCY’] || 2) threads_count = Integer(ENV[‘MAX_THREADS’] || 5) threads threads_count, threads_count preload_app! rackup DefaultRackup port ENV[‘PORT’] || 3000 environment ENV[‘RACK_ENV’] || ‘development’ on_worker_boot do # Worker specific setup for Rails 4.1+ # See: https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server#on-worker-boot ActiveRecord::Base.establish_connection end 当我通过运行rails s或rails s puma启动服务器时,我得到了 => Booting Puma […]

Rails_admin:如何删除rails 4中的嵌套对象?

我有: class Entity < AR::Base has_many :representatives, inverse_of: :entity, dependent: :destroy accepts_nested_attributes_for :representatives, allow_destroy: true end 并且Entity的rails管理表单允许我内联创建嵌套代表。 但是,在我们已经保存后,我无法通过实体的嵌套表单删除代表。 我怀疑这与rails 4有关,因为还有其他类似的问题,答案不起作用: Rails管理员 – 删除相关对象

建模相同模型之间的两种不同关系

两个模型组织和用户具有1:多关系,其中组织具有多个用户(成员;用户也可以不与任何组织关联): class Organization :all_blank, :allow_destroy => true validates_associated :users end class User < ActiveRecord::Base belongs_to :organization, inverse_of: :users end 一切正常,各种测试都通过了。 现在我为主持人function添加了一个额外的关系,其中用户可以拥有(多个)组织的主持人权限。 因此,通过第三个模型有很多:很多关系,我将其称为主持人: class Organization :all_blank, :allow_destroy => true has_many :moderators, class_name: “Moderator”, foreign_key: “reviewee_id”, dependent: :destroy has_many :users, through: :moderators, source: :reviewer validates_associated :users end class User < ActiveRecord::Base belongs_to :organization, inverse_of: :users has_many :moderators, […]