Tag: ruby on rails 3

如何获得包含条件的值?

我有三个表说shop,item和item_type 商店包含商店的名称,商品包含每个商店的商品,item_type包含不同类型的商品以及可用或不可用的状态。 现在我想渲染 format.json { render json: {:shop => @shops.as_json(:include => :items)}} 但根据条件,说item_type_id =’1’的项目和item_type_status =’available’的状态

nil的未定义方法`user’:NilClass

在我的模型电子邮件令牌我有 def self.token_valid(token, type) return unless token.present? token = EmailToken.where(“token = ? and verification_type = ? and confirmed = ‘false’ and created_at <= ?", token, type, EmailToken.expires).includes(:user).first user = token.user end 我从我的控制器中调用这个方法 def confirm_password_reset @user = EmailToken.token_valid(params[:pass_reset],”password”) render ‘reset_password’ if @user end 条件是有效的,因为我测试了它而没有尝试将它与用户关联,它的工作原理。 我要做的是获取token_valid方法来确认令牌并返回令牌及其关联用户。 belongs_to和has_many del也已定义。 Current error : undefined method user for nil:NilClass 谢谢。

为什么有些JS行为在开发中有效,但是当我推送到Heroku时却没有?

我正在尝试创建一个步骤表单 – 当选择其他内容时触发/显示某些字段。 这是一个例子 – http://jsfiddle.net/XnPZF/ – 它工作(它也适用于开发)。 只是无法弄清楚为什么当我推送到Heroku时这不起作用。 这是Heroku的一个版本 。 登录凭证:abc@test.com/test123 应该触发其下方其他字段的字段是Property_Type和Category。 以下是一些代码片段: /views/listings/new.html.erb { :class => ‘form-horizontal’ } do |f| %> Multi-Family Condo listings.js文件: $(‘#listing_property_type_id’).on(‘change’, function(){ var option = $(this).find(‘:selected’); $(‘[data-show-for-prop-type]’).hide(); //And maybe reset? $(‘[data-show-for-prop-type=’+ option.text() +’]’).show(); });

RSpec与设计

我刚接触到rails :)我试图运行我的第一个测试。 为什么这个测试通过? 用户名应该至少有2个字符,我的用户名有更多,它仍然通过测试。 user.rb: validates :username, :length => { :minimum => 2 } user_spec.rb require ‘spec_helper’ describe User do before do @user = User.new(username: “Example User”, email: “user@example.com”, password: “foobar”, password_confirmation: “foobar”) end describe “when name is not present” do before { @user.username=”aaaahfghg” } it { should_not be_valid } end end

使用rails3-autocomplete-jquery gem可以很好地处理带有多个输入的Simple_Form

所以我试图使用这个gem和simple_form实现多个自动完成,并且收到错误。 我试过这个: autocomplete_neighborhood_name_searches_path, :as => :autocomplete, ‘data-delimiter’ => ‘,’, :multiple => true, :class => “span8” %> 这是我得到的错误: undefined method `to_i’ for [“Alley Park, Madison”]:Array 在我的参数中,它是在neighborhood_id发送的: “search”=>{“neighborhood_id”=>[“Alley Park, Madison”], 所以它甚至没有使用这些值的ID。 有没有人有任何想法? 编辑1: 在回答@jvnill的问题时,我并没有明确地在控制器中使用params[:search]做任何事情。 搜索会创建新记录,并正在搜索listings 。 在我的搜索控制器中, create操作,我只是这样做: @search = Search.create!(params[:search]) 然后我的search.rb (即搜索模型)有这个: def listings @listings ||= find_listings end private def find_listings key = “%#{keywords}%” listings = […]

嵌套表单和has_many:通过

我有一个小样本应用程序,其中有3个模型:成员,组和订阅。 这个想法是会员可以订阅群组。 class Member < ActiveRecord::Base has_many :subscriptions, dependent: :delete_all has_many :groups, through: :subscriptions attr_accessible :email validates :email, presence: true end class Group < ActiveRecord::Base has_many :subscriptions, dependent: :delete_all has_many :members, through: :subscriptions accepts_nested_attributes_for :subscriptions attr_accessible :name, :subscriptions_attributes validates :name, presence: true, uniqueness: true end class Subscription < ActiveRecord::Base belongs_to :group belongs_to :member attr_accessible :group_id, […]

如何在rails 3中validation“file_fields”

我是rails的新手,请指导我。 我想validation“file_field”以进行图片上传。 只需jpg / png / gif即可上传,特定尺寸如最大尺寸(500×500) 这是我的_form.html.erb {:multipart => true} ) do |f| %> prohibited this photo from being saved: ‘115×20’ %> File 我试图像这样进行validation class Photo false, :with => %r{\.(gif|jpg|png)$}i, :message => ‘must be a URL for GIF, JPG ‘ + ‘or PNG image.’ end 而错误就是这样的 undefined method `upload’ for # 我错过了什么吗?

Ruby on Rails 3.2.13 – 使用I18n获取自定义错误页面的错误

我有一个Rails 3.2.13应用程序,我最初使用路由filtergem为I18n。 我计划在Rails 4中重写它。我删除了gem,因为没有可用的Rails 4生产版本​​,我无法让beta版本工作。 我成功地为大多数应用程序设置了路由。 我唯一的问题是我的自定义错误页面。 我在使用routing-filter gem时创建了自定义错误页面。 我需要有关如何为自定义错误页面设置路由的帮助。 以下是我在config / application.rb中进行配置的方法 config.exceptions_app = self.routes 这是我在application_controller.rb中的内容 before_filter :set_locale def default_url_options(options={}) { :locale => I18n.locale } end private def set_locale I18n.locale = (params[:locale] if params[:locale].present?) || cookies[:locale] || ‘en’ cookies[:locale] = I18n.locale if cookies[:locale] != I18n.locale.to_s end 这是我在使用路由filtergem时在路由文件中的内容。 match “/404”, to: ‘errors#error_404’ match “/500”, to: […]

协会不工作

我有三个型号: 部门 class Department :destroy has_many :waitingrooms, :dependent => :destroy end 等候室与字段patient_id:integer和department_id:integer class Waitingroom < ActiveRecord::Base belongs_to :patient end 有department_id:integer 患者 department_id:integer class Patient < ActiveRecord::Base belongs_to :department has_many :waitingrooms end 病人在候诊室后,我救了一间候诊室! 所以现在我试图找回那些在部门候诊室里的病人: def index @waited = @current_department.waitingrooms.patients end 不知何故,它没有工作,它返回此错误: undefined method `patients’ for # 但这很奏效:我错了什么? 谢谢! def index @waited = @current_department.waitingrooms end

public_activity rails undefined方法user_path

我正在慢慢地将public_activity添加到我的rails应用程序中。 它正在工作,但我在调用正确的方法和链接时遇到问题,使其看起来很漂亮 当我的activities.index.html刚刚有 在我看来,我可以看到 # # 当我的activities/index.html视图有 added guideline to 错误是 undefined method `user_path’ 这与我的路线有关吗? routes.rb是 Guidelines::Application.routes.draw do get “activities/index” # get “user/index” ActiveAdmin.routes(self) devise_for :admin_user, ActiveAdmin::Devise.config get “guidelines/topic” get “guidelines/topichospital” get “guidelines/topicspecialty” get “guidelines/favourite” get “profiles/show” get “guidelines/show” devise_for :users devise_scope :user do get ‘signup’, to: ‘devise/registrations#new’, as: :register get ‘login’, to: ‘devise/sessions#new’, as: […]