Tag: 的Ruby on 轨道

使用包含来validation数组元素包含:{in:array}

我使用acts-as-taggable-on gem来填充用户对这样的用户兴趣 # User.rb acts_as_taggable acts_as_taggable_on :interests 当我填充interest_list数组时,我需要检查给定的值是否与常量数组匹配,以确保它们是可接受的值,类似这样的 VALID_INTERESTS = [“music”,”biking”,”hike”] validates :interest_list, :inclusion => { :in => VALID_INTERESTS, :message => “%{value} is not a valid interest” } 上面的代码返回以下错误 @user = User.new @user.interest_list = [“music”,”biking”] @user.save => false …. @messages={:interest_list=>[“music, biking is not a valid interest”]} 我可以看到包含没有意识到它应该迭代数组元素而不是s考虑作为一个普通的字符串,但我不知道如何实现这一点。 任何的想法?

form_for()的多个参数

我正在阅读Beginning Rails 3.它创建了一个博客,用户可以发布文章,也可以发表评论到这些文章。 它们看起来像这样: class User ‘published_at DESC, title ASC’, :dependent => :nullify has_many :replies, :through => :articles, :source => :comments class Article < ActiveRecord::Base attr_accessible :body, :excerpt, :location, :published_at, :title, :category_ids belongs_to :user has_many :comments class Comment < ActiveRecord::Base attr_accessible :article_id, :body, :email, :name belongs_to :article 在app / views / comments / new.html.erb中,有一个表单从这样开始: 我的困惑在于为什么form_for()有两个参数。 […]

如何正常处理无法访问的数据库服务器?

我有一个主要在mysql中的应用程序,但是有几页需要连接到另一个mssql服务器。 我成功完成了这项工作,但是当我在Mac笔记本电脑上开发,并且sql server在另一台主机上时,它有时不可用。 当服务器不可用时,是否有一种优雅的方式来处理连接? 即,我仍然希望开发和测试运行不依赖于该服务器的部件。 如果生产rails应用程序失去与ms sql服务器的连接,这也可以很方便 – 那么能够捕获错误并显示一个很好的“服务器不可用”消息可能会很好…但仍然允许其余的不依赖于该数据库运行的应用程序。

Rails模型使用连接写def

我正试图在“工作人员”的模型中为“可计费”建立一个def。 关系: Workorders – belongs_to :billmethod Billmethod – has_many :workorders 我试过这些: def self.billable joins(:billmethods).where(billmethods: {“method_name != ?”, ‘Not Billable’}) end def self.billable where(“billmethod.method_name != ?”, ‘Not Billable’) end

嵌套表单未知属性错误

我正在尝试构建一个小费用跟踪应用程序。 使用nested_form gem添加订单项。 有一个Expense模型接受嵌套属性。 物品属于费用。 class Expense < ActiveRecord::Base belongs_to :organization belongs_to :department has_many :expense_types has_many :items accepts_nested_attributes_for :items end 物品型号: class Item < ActiveRecord::Base belongs_to :expense end 控制器创建动作操作如下: class ExpensesController < ApplicationController def new @expense = Expense.new end def create @expense = Expense.new(expense_params) if @expense.save flash[:notice] = "Expense Report Submitted" redirect_to @expense else render […]

是否可以从Rails 2.3中的响应中省略set-cookie标头?

假设某些操作只需要重新编辑xml或json,它们就不应该发送用于浏览器的cookie。 AFAIK它是Rails 3中金属的一个用例,但2.3可能吗? 除了机架级别……

rails方式编写复杂的查询

我有这个问题: SELECT f.id, Concat(f.name, ‘ ‘, REPLACE(f.parent_names, ‘,’, ‘ ?’)) AS FullName, u.name AS Unit, u.id AS UnitId, u.position AS UnitPosition, city.name AS City, hus.mobile1 AS HusMobile, wife.mobile1 AS WifeMobile, hus.first_name AS Spouse1, wife.first_name AS Spouse2, f.phone AS HomePhone, f.email AS Email, Date_format(f.contact_initiation_date, ‘%d/%m/%Y’) AS InitDate, f.contact_initiation_date AS sql_date, Date_format(f.status_change_date, ‘%d/%m/%Y’) AS StatususChangeDate, stts.name AS […]

找不到uri 和方法的处理程序

嗨,我正在使用ruby-2.4.0和rails 5进行RoR项目。我正在使用elasticsearch在我的应用程序中进行搜索。 我有一个模型venue_details如下: – class VenueDetail :not_analyzed indexes :venue_name, :type => ‘string’, :index => :analyzed end end def self.search(params) if params[:query].present? tire.search(load: true) do query { string “*#{params[:query]}*”, default_operator: “AND” } size 100 end else tire.search(load: true) do query { string “*#{params[:term]}*”, default_operator: “AND”} size 100 end end end end 我正在使用elasticsearch-5.1.2。 我试图用命令rake environment tire:import CLASS=VenueDetail […]

在没有弹出对话框的情况下分享到Facebook墙

我正在Rails中构建一个Facebook应用程序,用户在其中创建一个条目并将其提交到其他用户的条目集合中 – 我想要做的是在提交时自动共享该条目的链接到用户的墙上,但是如果没有弹出对话框窗口,我就无法理解Graph API文档。 有人可以帮忙吗? 提前致谢!

Rails 4:如何禁用Edit,Destroy等,

我可以在Rails中禁用“编辑”和“命令”吗?例如,如果我想为每个人禁用“编辑”,我在test_controller.rb中执行的操作是什么? 或其他什么? 我是Rails的新手,提前谢谢! class BooksController < ApplicationController before_action :set_book, only: [:show, :edit, :update,:destroy ] # GET /books # GET /books.json def index @books = Book.all end # GET /books/1 # GET /books/1.json def show end # GET /books/new def new @book = Book.new end # GET /books/1/edit def edit end # POST /books # POST […]