Tag: ruby on rails 4

在Rails 4中使用覆盖错误消息中的值作为自定义validation器

我在Rails 4.2上我编写了一个自定义Validator,它将检查输入的值是否存在于另一个表中。 我一直在审查其他一些post,似乎有一个特定于上下文或rails版本的首选方法来重用被validation的value 。 在rails文档中,我看到例如: validates :subdomain, exclusion: { in: %w(www us ca jp), message: “%{value} is reserved.” } 但是,如果我尝试在我的自定义消息覆盖中使用%{value} ,则它不会进行插值,而只是打印“%{value}”。 我见过各种称之为“价值”的方法。 我也无法让%{value} to work in my Validator definition, but could get #{value} to work (New to ruby, if #{value}从validate_each获取它?)。 我一直在努力处理各种格式的validation语句并输入自定义消息。 从文档看起来可重复的一些东西不是。 如果我声明自定义消息的方式导致错误,请告诉我如何更正? class ExistingGroupValidator value).any? record.errors[attribute] << (options[:message] || "#{value} is not a valid […]

如何编辑或覆盖ActiveAdmin的页脚?

如何编辑或覆盖Active_Admin的页脚?

作为可投票的Ajax投票大小不更新Rails

我在我的rails 4 app上使用这些行为作为可投票的gem。 我终于使用ajax工作了,所以当用户投票时页面不会刷新,但是,在刷新页面之前,vote.size不会更新。 这是我的控制器动作: def upvote @article = Article.find(params[:article_id]) @subarticle = @article.subarticles.find(params[:id]) session[:voting_id] = request.remote_ip voter = Session.find_or_create_by(ip: session[:voting_id]) voter.likes @subarticle respond_to do |format| format.html {redirect_to :back } format.json { render json: { count: @subarticle.get_upvotes.size } } end end 并查看: Helpful $(‘.vote’) .on(‘ajax:send’, function () { $(this).addClass(‘loading’); }) .on(‘ajax:complete’, function () { $(this).removeClass(‘loading’); }) […]

在父母的孩子上搜索时,ActiveRecord不会返回

很想知道我正在做这个简单的错误… current_user = User.find(60) Plan.joins(:user).where(“users.id” => current_user.id).where(“plans.status” => nil) # Plan Load (8.6ms) SELECT “plans”.* FROM “plans” INNER JOIN “users” ON “users”.”id” = “plans”.”user_id” WHERE “users”.”id” = 60 AND “plans”.”status” IS NULL # => #<ActiveRecord::Relation [# # 不明白为什么第二个陈述没有找到计划..

Rails 4 – Sprockets :: FileNotFound在Welcome#index中

有什么想法吗? 我在Windows中安装了Highcharts并且工作但现在在Ubuntu中无法正常工作。 这是我的application.html.erb文件。 { Highchartstest1 true %> true %> } {application.js} {// This is a manifest file that’ll be compiled into application.js, which will include all the files // listed below. // // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, // or any plugin’s vendor/assets/javascripts directory can be referenced here using a relative path. […]

在Rails 4.2.4中使用控制器中的帮助器

我对这里读到的rails文档感到困惑。 特别是这句话: 默认情况下,每个控制器都包含所有帮助程序。 这些助手只能通过.helpers在控制器上.helpers 这是什么。它指的是什么? 我在app/helpers/areas_helper.rb定义了一个帮助app/helpers/areas_helper.rb : module AreasHelper def my_helper puts “Test from helper” end end 我想在app/controllers/locations_controller.rb使用这个帮助app/controllers/locations_controller.rb : class LocationsController < ApplicationController def show helpers.my_helper end end 但是,我得到一个方法未定义错误。 怎么这个.helpers应该被使用? 我知道还有其他方法可以访问控制器中的帮助程序,但我特别询问这篇文档以及它想说的内容。

Rails 4:保存子记录ID但不保存其属性,使用带有has_many关系的嵌套表单

我有3个模型与has_many通关系:食物(例如:巧克力),Sub(巧克力食物替代品),关节(联合表)。 说@food = Food.find(1); has_many通过关系允许我做@subs = @ food.subs,它返回与@food相关的所有替代品。 这工作正常,但只保存Sub id而不是它的属性:name和:description,因为你可以看到它在我的控制器中的create action中尝试保存@ food.subs时返回nil: => #<ActiveRecord::Associations::CollectionProxy [#]> 我想问题在于我的食物控制器中的创建动作,也许与我的嵌套forms有关。 我花了无数个小时试图解决这个问题,我非常渴望找到答案。 我真的不知道在哪里看。 我是rails的新手,所以非常感谢你的帮助和时间,我真的很感激。 请尽可能根据我的初学者水平调整你的答案:-)。 下面是我的控制器样品,表格和相关信息。 这是我的模特: class Food :joints accepts_nested_attributes_for :subs end class Sub :joints accepts_nested_attributes_for :foods end class Joint < ActiveRecord::Base belongs_to :food belongs_to :sub end 这是我的db-schema FYI: create_table “foods”, force: true do |t| t.string “name” t.text “description” […]

强制活动模型序列化程序返回关联

我有一个Active Model Serializer ,它具有以下function: class API::DashboardSerializer < ActiveModel::Serializer attributes :id, :name, :special def special x = object.check_ins.first prev = x.prev_ci_with_weigh_in end end special返回CheckIn类的记录,我希望它为该记录使用CheckInSerailizer 。 如何强制它使用special的CheckInSerializer ?

如何在Rails 4中为设计注册添加自定义字段?

我正在尝试将一个字段添加到使用设计的注册页面。 我按照Devise文档中的说明进行操作。 新字段是“角色”字段。 我已成功将其迁移到数据库。 我现在正努力使注册有效。 这是新的applicationcontroller: class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception def devise_parameter_sanitizer if resource_class == User User::ParameterSanitizer.new(User, :user, params) else super # Use the default one end end end 这是更改的用户模型: class User::ParameterSanitizer < Devise::ParameterSanitizer […]

Carrierwave + repage选项不起作用

在我的Carrerwave自定义处理方法中,我正在尝试使用+repage选项。 文档很难找到,但我发现我需要运行img.repage.+ 。 然而,这并没有成功,因为它甚至没有尝试将选项转换为+,但保持在-repage并抛出错误说明尽可能多。 然后我找到了一个说明的页面,将其写成: img.push ‘+repage’ img.+ img.repage 它使用了一个不同的例子(不是使用repage,而是使用带有两个参数的’+ profile’。无论哪种方式它都不起作用……它现在告诉我undefined method ‘gsub’ for nil:NilClass并指向img.+线。 这是我的方法: def custom_thumbnail manipulate! do |img| img.distort :srt, -30 img.repage.+ img.crop “#{model.crop_w}x#{model.crop_h}+#{model.crop_x}+#{model.crop_y}” img end end 其他人有运气吗? 编辑 – 完整堆栈跟踪直到控制器以及更新的方法: def custom_thumbnail manipulate! do |img| img.distort :srt, -30 img.push ‘+repage’ img.+ img.repage img.crop “#{model.crop_w}x#{model.crop_h}+#{model.crop_x}+#{model.crop_y}” img.resize “150×150” img end end mini_magick (3.6.0) […]