Tag: ruby on rails 4

样式化Rails date_select的日期字段

在Rails的表单中,日期select下拉很容易: 然后我得到3个select下拉菜单的年,月和日。 有没有使用JavaScript在这些select元素之间添加HTML的简单方法? 我希望能够更好地设计和标记它们。

Rails 4.2 – dependent :: restrict_with_error – 访问错误

:restrict_with_error如果存在关联的对象rails关联基础,则会将错误添加到所有者 我在代码中添加了以下内容: class Owner < ActiveRecord::Base has_many :things, dependent: :restrict_with_error end 我的理解是,当我尝试删除具有依赖项的所有者时 ,应该引发错误。 在owners_controller show动作中,我尝试访问错误但找不到它们: def show @owner = Owner.find(params[:id]) @owner.errors end 更新 – 删除代码 def destroy @owner = Owner.find(params[:id]) @owner.destroy flash[:notice] = “Owner Deleted Successfully” respond_with(@owner) end

轨道中的外键4

我正在使用Rails 4和SQLite。 我正在尝试在indicators表中添加外键。 请参阅下面的代码 class Indicator < ActiveRecord::Base belongs_to :objetive belongs_to :responsible, class_name: "Person" end 迁移脚本: class AddFksToIndicator < ActiveRecord::Migration def change add_reference :indicators, :objective, index: true add_reference :indicators, :responsible, index: true end end 当运行迁移时一切正常,所以我尝试在控制台中: 2.0.0p247 :002 > i = Indicator.new => # 2.0.0p247 :002 > i.objective_id = 0 2.0.0p247 :003 > i.save 令我惊讶的是,指标已经保存,并且没有id = […]

用于嵌套索引操作的Rails多态链接

我一直试图找到这个链接几个小时。 我有一个多态关联,其中的集合和分类都有设计。 收集模型 has_many :designs, :as => :targetable 分类模型 has_many :designs, :as => :targetable 设计模型 belongs_to :targetable, :polymorphic => true 为了链接到设计的“show”动作,正确的多态路径将是: link_to polymorphic_path([@targetable, @design]) 但我无法弄清楚如何链接到设计的“索引”页面,以显示与其相应的可定位对象相关的所有设计。 有谁知道到达那里的适当链接?

update_attributes,然后检查其中一个属性是否已更改

我有一个课程模型,用户可以使用表单进行编辑和更新。 在控制器中,update方法调用update_attributes(course_params) ,即强参数。 这都是标准的,工作正常。 现在我试图找出更新期间特定属性是否正在发生变化。 特别是,如果用户正在更改课程对象的points属性,我还需要将对象的points_recently_changed属性标记为true。 快速而肮脏的实现将是这样的: def update @course = Course.find(params[:id]) @old_points = @course.points @course.update_attributes(course_params) @new_points = @course.points if @old_points != @new_points @course.points_recently_changed = true @course.save end end 一种稍微不那么糟糕的做法可能是: def update @course = Course.find(params[:id]) @course.points_recently_changed = true if @course.points != params[:course][:points] @course.update_attributes(course_params) end 然而,这些都不能满足我对干净,高效和易于阅读的实现的渴望。 理想情况下,update_attributes可以选择返回在更新期间实际更改的属性数组。 但事实并非如此。 我查看了ActiveModel :: Dirty ,但问题是它只能在保存之前运行。 因为我正在使用更新和保存的update_attributes,所以像has_changed?这样的方法has_changed? 在我的方案中不起作用。 任何建议,将不胜感激。 🙂 […]

Rails 4资产管道失去供应商资产

我似乎无法弄清楚如何让链轮在vendor/assets找到vendor/assets 。 我一直在通过将我的所有资产添加到app/assets来解决问题,但它变得太杂乱了。 我已阅读文档,并尝试将以下所有行添加到application.rb文件中。 config.assets.paths << "#{Rails.root}/vendor/assets/*" config.assets.paths << "#{Rails.root}/vendor/assets/fonts" config.assets.paths << "#{Rails.root}/vendor/assets/stylesheets" config.assets.precompile << Proc.new { |path| if path =~ /\.(eot|svg|ttf|woff)\z/ true end 它们在本地工作,但是当我将它们推送到服务器时,我的供应商资产都没有。 我正在使用capistrano进行部署,我知道升级存在一些问题。 这可能是问题的根源,但我按照文档来部署(几乎)一切正常。

如何使用act-as-votable设置多选项投票系统?

我想使用act-as-votable实现投票系统,我可以提供多种自定义选项 – 例如5个按钮(’蓝色’,’红色’,’绿色’,’灰色’,’白色’)。 我希望我的用户只能选择其中一种颜色,但我希望能够计算每个项目的所有投票(10 – 蓝色,4 – 红色等)。 我觉得我会使用投票范围,但我不太确定如何。 我如何通过行为投票来做到这一点?

关于从CSV导入的操作之前

我有一个简单的CSV导入,其中提供的文件已被破坏(UTF字符)(德语)。 例如:该清单有: G%C3%B6tterbote 正确的名称应该在哪里 Götterbote 我正在尝试在导入CSV时强制编码。 我的导入行动 def import Player.import(params[:file]) redirect_to players_path, notice: “Players Imported successfully” end 我的导入方法 def self.import(file) SmarterCSV.process(file.path) do |row| Player.create(row.first) end end 我发现这会成功转换String,但无法成功实现: u = “G%C3%B6tterbote” => “G%C3%B6tterbote” u1 = CGI::unescape(u).force_encoding(‘UTF-8’) => “Götterbote” 所以基本上我需要像before_action (我猜)…

gem安装ruby-audio-1.6.1错误(使用Mac OS 10.9 / homebrew)

我尝试了其他提示,但没有奏效。 (请参阅下面的链接) 恩。 gem安装ruby-audio失败 ruby-audio 1.6.1安装错误(已安装Libsndfile 1.0.25) 我想要一些反馈。 $ gem install ruby-audio Building native extensions. This could take a while… ERROR: Error installing ruby-audio: ERROR: Failed to build gem native extension. /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb **checking for sndfile.h in /opt/local/include,/usr/local/include,C:/Program Files (x86)/Mega-Nerd/libsndfile/include,C:/Program Files/Mega-Nerd/libsndfile/include… yes checking for sf_open() in -lsndfile-1… no checking for sf_open() in -lsndfile… no** *** […]

使用Rails 4(和Hstore)在PostgreSQL中存储嵌套的哈希

我有一个Rails应用程序,它汇集了来自Google API的大量数据。 我目前在MongoDB中存储JSON响应(因此我的Rails应用程序同时包含pg和mongo)。 但是,今天,我遇到了PostgreSQL Hstore扩展,我决定尝试一下。 不幸的是,我遇到了一个问题。 API提供的JSON是多层深度的,因此JSON.parse之后的Ruby Hash包含哈希,其中包含新的哈希值。 但是, Hstore是字符串键/值存储,它只有1级深度。 所以第一个哈希中的哈希就变成了字符串。 我发现真正讨厌的黑客是eval变成字符串的哈希: eval(“{ “foo” => “bar” }”) 我不喜欢这个。 关于该怎么做的任何提示? 我应该继续使用MongoDB还是有更好的方法在PG中存储多级深度哈希值?