Tag: ruby on rails 4

“key(slug)=()已经存在”在rails4 app上的friendly_id中

虽然我正在尝试将friendly_id设置为我的rails4项目,但同样地 ,我在“朋友”之后将“朋友”添加到friends表后出错。 我该如何解决它: PG::UniqueViolation – ERROR: duplicate key value violates unique constraint “index_friends_on_slug” DETAIL: Key (slug)=() already exists. 另外,以下是我的文件问题可能基于: # app/models/friend.rb: class Friend < ActiveRecord::Base has_many :entries, dependent: :destroy belongs_to :user extend FriendlyId friendly_id :candidates, use: [:slugged, :finders] # not :history here def candidates [ :first_name, [:first_name, :last_name] ] end end # db/schema.rb: create_table "friends", […]

如何让country_select gem和simple_form从头开始工作?

我只想要一个简单的国家/地区下拉列表,按照我的目的排列优先顺序(即包括所选国家/地区内的州)。 事实上,我只想要1个或2个国家。 我正在使用Simple_Form,他们的文档说,为了获得访问权限: f.input :shipping_country, priority: [ “Brazil” ], collection: [ “Australia”, “Brazil”, “New Zealand”] 我只需要在我的Gemfile中包含它: gem ‘country_select’ 但我很困惑……我不必运行迁移来将国家选择存储在我的Post模型上 – 这是我将修改_form.html.erb的模型? 当我这样做时: 我no country method on Post错误no country method on Post有一个no country method on Post 。 我不得不将它添加到我的Post.rb : attr_accessor :country 如果我的记忆正确地为我服务,那么由于强参数,我不再需要在Rails 4中做,对吧? 这有效,但现在collection:规范不起作用。 我仍然看到很多国家。 请记住,我没有必要运行任何迁移来修改Post.rb模型。 我不知道要添加哪些列。 我是否将country, state, city添加到我的Post模型中,还是创建一个名为Country的新模型? 关于gem中的国家(国家代码,城市,州等)的所有信息都是通过Gem中的YAML文件加载的吗? 我没有任何线索,文档非常稀疏。 所以我的问题只是这些: 如何使这个simple_form集合起作用。 如何让国家的其他属性出现在表单中(如州和/或城市)? 我是否必须在某个表格中提供其他数据? […]

Rails尝试在创建用户后创建配置文件

我正在试图弄清楚如何为刚刚创建的用户创建新的配置文件, 我在User模型上使用设计,User模型与UserProfile模型有一对一的关系。 这是我的用户模型的样子: class User ‘username’) end # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable end 这会产生以下错误: nil的未定义方法`new’:NilClass 我在rails c中尝试过User.find(1).user_profile ,这样可行,所以我很确定关系设置正确, 我可能是一个胖胖的大菜,并且试图错误地取自我。 你还可以告诉我如何访问模型中的参数…是否可能?

Rails Asset Pipeline – 错误的指纹

我的Rails 4应用程序有new.html.erb ,它引用了form.html.erb : # app/assets/templates/jobs/new.html.erb … <ng-include src="''”> … # app/assets/templates/jobs/form.html.erb my form is here ( ng-include是AngularJS指令) 问题是,当form.html.erb更改时,生产环境仍会加载旧的form.html.erb 。 发生这种情况可能是因为new.html.erb尚未更改,因此具有相同的旧指纹,指向带有旧指纹的form.html.erb 。 Rails处理这个问题的方法是什么?

Rails 4生成无效的列名称

我有一个相当简单的查询来返回多对多关系中的第一个记录,或者如果它不存在则创建一个。 UserCategorization.where(category_id: 3, user_id: 5).first_or_create 我的模型看起来像: class UserCategorization < ActiveRecord::Base belongs_to :user belongs_to :category self.primary_key = [:user_id, :category_id] end 但是它在SQL中生成无效的列名: SQLite3::SQLException: no such column: user_categorizations.[:user_id, :category_id]: SELECT “user_categorizations”.* FROM “user_categorizations” WHERE “user_categorizations”.”category_id” = 3 AND “user_categorizations”.”user_id” = 5 ORDER BY “user_categorizations”.”[:user_id, :category_id]” ASC LIMIT 1 如果我从模型中删除self.primary_key = [:user_id, :category_id] ,它可以正确检索记录但无法保存,因为它不知道在WHERE子句中使用什么: SQLite3::SQLException: no such column: user_categorizations.: […]

为什么控制器方法中定义的实例变量不在相应的部分中可用?

例如,给定一个基本控制器,例如: class PostsController < ApplicationController def index @post = Post.all end 实例变量@post在视图posts/index.html.erb可用,但不在部分posts/_index.html.erb 我的主要问题是:这是为什么? 如何在控制器中为部分定义方法,例如,传递实例变量? 更具体的说明:我问这个是因为我在单独的主视图welcome/index.html.erb呈现数据的方式是通过这个post部分。 我已经通过posts/_index.html.erb partial中的Post.all直接调用模型来解决这个限制,但据我所知,这忽略了MVC架构并严重限制我开发更复杂的方法,除非我真的打破约定并将它们写入视图(我认为它可能有用,但设计很差)。 目前,@ post将nil传递给部分符号:welcome / index.html.erb中的post 很明显我错过了什么,所以任何见解都会非常感激! 谢谢 相关文件: views/ posts/ _index.html.erb _new.html.erb show.html.erb welcome/ index.html.erb controllers/ posts_controller.rb welcome_controller.rb 文章/ _index.html.erb 文章/ _new.html.erb 文章/ show.html.erb welcome / index.html.erb相关部分(部分) 控制器/ posts_controller.rb class PostsController < ApplicationController def new end def create @post […]

ruby on rails console无法正常工作,我正在Windows上“切换到检查模式”

当我运行我的rails控制台时,它说“切换到检查模式”。我在Windows上工作。 请帮忙!

有些问题在生产模式下访问资产文件

我正在使用Ruby on Rails 4.1.1,在我的本地机器上,我有以下图像: # Directory: MyApp/app/assets/images/ logo.png 我使用Capistranogem上传MyApp,所有内容似乎都按预期工作。 但是当我尝试通过以下url的浏览器访问我的网站时 1) http://www.myapp.org/logo.png 2) http://www.myapp.org/assets/logo.png 3) http://www.myapp.org/images/logo.png 4) http://www.myapp.org/assets/images/logo.png 然后我得到一个错误页面: The page you were looking for doesn’t exist. You may have mistyped the address or the page may have moved. 在日志文件中,我得到: 1) ActionController::RoutingError (No route matches [GET] “/logo.png”): 2) ActionController::RoutingError (No route matches [GET] “/assets/logo.png”): 3) […]

在Ruby on Rails中使用Plucked Values

给定一个名为Album的模型,我想使用我采用的值。 对于初学者,我想尝试打印值。 我怎样才能做到这一点? 这是我目前的代码: my_arr = Album.where(title: “Greatest Hits”, artist: “The Beatles”).pluck(:publisher, :year_published) puts my_arr[0][0].to_a 为了使事情变得更简单,我将如何检索任何值,以便将其置于正常变量中? 我正在使用Rails 4和SQLite3。

Rails 4 Minitest测试虚拟属性

所以我有一个类TimeBank,它正在更新,并且正在获得两个新的虚拟属性,它们将操纵另外两个现有属性(DB列)。 class TimeBank < ActiveRecord::Base # validations, consts, few other methods omitted for brevity def date_worked @date_worked ||= self.start.to_date rescue Date.current end def date_worked=(date_worked_val) self.attributes['date_worked'] = date_worked_val end def hours_worked @hours_worked ||= hours rescue NoMethodError nil end def hours_worked=(hours_worked_val) self.attributes['hours_worked'] = hours_worked_val end def hours if attributes["hours"] read_attribute(:hours).to_f else ((finish – start)/1.hour).to_f end rescue NoMethodError […]