Tag: ruby on rails 4

在下拉列表中显示树层次结构中的类别/子类别

我有一个包含字段id,name和parent_id的类别表。 根类别有parent_id 0.现在我想在下拉列表中显示类别列表,并显示如下结构: root_category first_sub_category sub_sub_category another_sub_sub_category second_sub_category another_root_category first_sub_category second_sub_category 这是我的控制器: def new @category = Category.new end 以下是观点: 请帮忙。

在Rails 4中使用单个表单保存多个记录

我有一个名为Family的模型,它属于User用户我想让用户在单个表单中添加多个家庭成员,这位于/views/families/new.html.erb 编辑:对不起,我应该在稍微修改代码后提供更详细的代码 在routes.rb资源中:系列编辑:在User.rb中 class User < ActiveRecord::Base has_many :families end 编辑:在Family.rb class Family < ActiveRecord::Base belongs_to :user end 在FamiliesController中 def new end 编辑:我已经这样修改了表单: Family Member # : 编辑:在FamiliesController中创建动作 def create @family = Family.new(family_params) a = params[:family][:”0″] @family.user_id = current_user.id if @family.save flash[:notice] = params[:family][:”0″] redirect_to dummy_path else flash[:notice] = “didnt happen” end end 因此,用户可以看到一个表单,用于为一个user_id创建3个系列成员 将数据输入3个相同的表单,然后点击提交 […]

Rails 4:schema.db显示“无法转储表”事件“因为跟随NoMethodError #nunfined方法`’为nil:NilClass”

我一直在研究一个带有sqlite(Rails开发环境的默认设置)的Rails 4.0应用程序,用于事件(黑客马拉松),它有一个父模型,Event,可以有很多Press_Blurbs。 首先,我运行了一些脚手架生成器,这些生成器创建了一些我看似没有问题的迁移: class CreateEvents < ActiveRecord::Migration def change create_table :events do |t| t.string :city t.string :theme t.datetime :hackathon_start t.datetime :hackathon_end t.datetime :show_start t.datetime :show_end t.text :about t.string :hack_rsvp_url t.string :show_rsvp_url t.timestamps end end end class CreatePressBlurbs < ActiveRecord::Migration def change create_table :press_blurbs do |t| t.string :headline t.string :source_name t.string :source_url t.string :logo_uri t.timestamps end […]

是否每次加载页面时都会执行每个相关计算?

我有一个模型“Wrapper”,其中包含另一个模型“Category”,其中又有另一个模型“Thing”。 “Thing”具有整数属性:count和:number 。 它还有一个在models/thing.rb定义的实例方法: def ratio (self.count + self.number).to_f / Thing.all.count.to_f end 那么,“类别”具有此实例方法,在models/category.rb定义: def thing_ratios self.things.sum(&:ratio.to_f) end 最后,我的wrapper.html.erb视图显示了按照thing_ratios顺序列出的thing_ratios : … 我的问题是这样的:每次有人重新加载页面wrapper.html.erb ,是否必须重新计算每一个相关计算,一直到每个与页面上每个类别相关的Thing的self.count ?

Rails迁移不会更改schema.rb

我有一个rails应用程序没有应用到我的schema.rb。 迁移应该创建一个表: class CreateUserGraphs < ActiveRecord::Migration def change create_table :user_graphs do |t| t.string :name t.string :content t.integer :user_id t.string :type_id t.integer :upload_id t.timestamps end add_index :user_graphs, [:user_id, :created_at] end end 我做了db:reset。 然后我尝试了rake db:migrate:up VERSION = 123123123(这是迁移#)。 我在我的“开发”环境中。 为什么迁移不会影响schema.rb?

Rails 4 + bootstrap设置资产

我正在尝试使用bootstrap-sass在Rails4上设置bootstrap ,我收到了这个着名的错误: Sprockets::FileNotFound – couldn’t find file ‘bootstrap’ (in app/assets/javascripts/application.js:16): 我试过以下: application.js twitter/bootstrap gem ‘bootstrap-sass’, ‘~> 3.1.0’是组外资产 还尝试了一些互联网上的其他东西 我花了很多时间从其他post中提出不同的建议。 我如何系统地调试这个, 如何设置bootstrap-sass? ps:也试图让twitter-bootstrap-rails没有运气。 这是一些文件 的application.js //= require jquery //= require jquery_ujs //= require js-routes //= require bootstrap //= require_tree . //= require bootstrap-slider application.css.scss *= require jquery.ui.core *= require jquery.ui.theme *= require_self *= require bootstrap-slider *= […]

Redis pub / sub on rails

关注Redis Pub / Sub 这工作正常,我可以使用任何类发布消息 $redis.publish ‘channel’, { object: @object.id } 使用redis-cli > MONITOR ,我可以validation此请求是否已正确发布 [0 127.0.0.1:64192] “publish” “channel” “{:object=>\”5331d541f4eec77185000003\” }” 当我在其他类(监听器类)中向该通道添加订户块时,问题就开始了,如下所示 class OtherClass $redis.subscribe(‘channel’) do |payload| p payload end end 在redis-cli > MONITOR ,还显示侦听器已正确订阅 [0 127.0.0.1:52930] “subscribe” “channel” 问题是,当我将订阅者监听器类添加到相同的rails应用程序时……它停止工作,因为OtherClass监听redis服务器并停止执行任何其他代码……它只是坐在那里听。 那么有没有办法在相同的rails应用程序上使用redis创建消息传递总线…以便从某些类或服务对象发布事件,并且有特定通道的侦听器在后台接收事件时采取行动。 我知道我可能会使用sidekiq或任何其他背景工作者来完成这项工作……但过了一段时间后,后台工作人员变得混乱而且无法维护。

Foundation 5 Topbar在导轨中不能始终如一地工作4

我在我的rails 4应用程序中使用基础5。 当我发送请求时,顶部栏菜单工作正常。 我可以hover项目和嵌套项目没有问题。 然后,我单击其中一个项目,其中包含与之关联的link_to方法。 有时它会在我的菜单仍能正常工作的情况下给出正确的结果,但有时候整个事情会冻结。 这也使移动菜单的后退按钮消失。 一旦我通过刷新或url发送新请求,菜单就会再次运行。 Rails在日志文件中没有看到任何问题。 我的菜单在rails之外工作正常。 我想知道是否可能与我的link_to tag_helpers或我在我的应用程序中设置基础的方式有关? 以下是JS如何为基础路由的快速概述。 (他们在他们的文档上建议的配置)views / layouts / application.html.erb: 资产/ application.js中: //= require jquery //= require jquery_ujs //= require foundation //= require turbolinks //= require_tree . $(document).foundation(); 还有其他人遇到过这个问题吗? 除了不使用粉底5之外,还有什么好的解决方案吗? 谢谢Alex

Rails 4 嵌套资源和浅:真

以下post基于Rails 4。 我实际上正在寻找关于多个嵌套资源(超过1个)的良好最佳实践,并且选项浅:true。 首先在我的路线中,有这样的: resources :projects do resources :collections end 相关的路线是: project_collections GET /projects/:project_id/collections(.:format) collections#index POST /projects/:project_id/collections(.:format) collections#create new_project_collection GET /projects/:project_id/collections/new(.:format) collections#new edit_project_collection GET /projects/:project_id/collections/:id/edit(.:format) collections#edit project_collection GET /projects/:project_id/collections/:id(.:format) collections#show PATCH /projects/:project_id/collections/:id(.:format) collections#update PUT /projects/:project_id/collections/:id(.:format) collections#update DELETE /projects/:project_id/collections/:id(.:format) collections#destroy projects GET /projects(.:format) projects#index POST /projects(.:format) projects#create new_project GET /projects/new(.:format) projects#new edit_project GET /projects/:id/edit(.:format) projects#edit project […]

Rails 4重定向到Chrome中的“data:”

Google Chrome中存在一种奇怪的行为,此问题也有描述: rails重定向到’data:,’ 当创建新资源并且我的控制器重定向到show动作时, chrome ‘data:,’在地址栏中启动加载’data:,’的空白页面 。 提出上述问题的作者的答复如下: 这是一项安全function,新页面的HTML内容与Chrome阻止的提交表单的HTML内容相匹配。 但是没有解释如何解决它。 该行为仅出现在Chrome浏览器中。