ruby-on-rails-3.1

Simple way to always make a field lowercase in db

眉间皱痕 提交于 2019-12-22 05:11:49
问题 Currently I'm doing the following in the model: before_save :to_lower before_create :to_lower def to_lower self.name = self.name.downcase end Seems pretty repetitive to me. 回答1: You don't need the before_create if you already have before_save. before_save { |user| user.name = user.name.downcase } 回答2: I generally handle such cases by: def name= name super(name.try(:downcase)) end 回答3: def name=(val) write_attribute(:name, val.downcase) end 回答4: Why are you doing this? If it's to perform case

Rails Completed 406 Not Acceptable

谁说我不能喝 提交于 2019-12-22 05:04:29
问题 I don't know what the heck is going on here, I just updated my rails from 3.1.0rc4 to 3.1.0 and I suddenly find that all ajax won't work, It's causing 406 error all the time. Everything was perfectly fine before I update to 3.1.0 Guys, I am going to lose controll now, help me. 回答1: Make sure that you're setting the correct content-type and that your controllers respond to the specified format. This often happens if the request content-type is set to 'application/json' and your controller

How to order by a related model in default scope? — Rails 3.1

落爺英雄遲暮 提交于 2019-12-22 04:43:30
问题 How can I write the following default scope: class SimilarMerchant < ActiveRecord::Base # relationships belongs_to :merchant belongs_to :similar_merchant, :foreign_key => 'similar_merchant_id', :class_name => "Merchant" # scopes default_scope order('merchants.is_paid DESC').order('score DESC') end Basically I want to sort by a merchant.is_paid field (which belongs to a related model) Any advice? 回答1: Try this: default_scope joins(:merchant).order('merchants.is_paid DESC, score DESC') And

Rails 3.1 - find using count and select as

眉间皱痕 提交于 2019-12-22 04:26:11
问题 I am trying to do the following sql statement in rails: SELECT COUNT(downloads.title) AS total, downloads.title FROM `downloads` WHERE `downloads`.`member_id` = 60 Group by `downloads`.`title` I wrote this in rails like this: Download.where(:member_id => id).select("COUNT(downloads.title) AS total, downloads.title").group(:title) If I run the query straight from the sql server the sql executes correctly but if I run the activerecord version I only get the title back. I thought this might be

Rails assets - keep license comments

谁说我不能喝 提交于 2019-12-22 04:08:32
问题 How can I prevent Uglifier from removing some comments from some files? I want to have code minified and compressed but also I want the licensing comments to remain intact. 回答1: From the documentation of uglifyJS. -nc or --no-copyright — by default, uglifyjs will keep the initial comment tokens in the generated code (assumed to be copyright information etc.). If you pass this it will discard it. which is also an option of uglifier. 来源: https://stackoverflow.com/questions/8896969/rails-assets

Rails 3.1: javascripts not served correctly from vendor/assets directory?

天大地大妈咪最大 提交于 2019-12-22 01:58:36
问题 I have organized my javascript files in a couple of directories and I have found the following strange behavior. Given the following tree: + app + assets + javascripts + common + public + common + home - home.js home.js looks like this: //= require jquery //= require jquery_ujs //= require jquery-ui //= require_directory ../../jquery_plugins //= require_directory ../../common //= require_directory ../common //= require_self Now the trick lies in the jquery_plugins directory. I placed this

How to post html form data to controller using ruby on rails 3.1

萝らか妹 提交于 2019-12-22 01:23:10
问题 I am using ruby on rails 3.1. And am trying to post html form data to controller for saving the record in database. But I am getting routing error like this 'No route matches [POST] first/save' .But when I tried to run this link in address bar like '127.0.0.1:3000/first/save' it is working fine. Can any one please tell me where am doing wrong. My routes are like: Rails.application.routes.draw do root 'first#hello' get 'first/save' end And my html form is like: <form accept-charset="UTF-8"

How to make a respond_to by AJAX in Rails 3?

我是研究僧i 提交于 2019-12-22 01:22:41
问题 I have a form, that I send with using AJAX. The form is in the FirstsController . I need the form send to SecondsController . The form is sent and data saved. Then I wanna change the form by a text, I try to do by this way: def create ...saving... respond_to do |format| format.js { render :update do |page|; page << "$('#id_element').html('hellllloooooo');" end } format.html {} end end And I ma getting this error: ActionView::MissingTemplate (Missing template seconds/update, application/update

Rails - application design for 2 user types

寵の児 提交于 2019-12-22 00:35:59
问题 I am writing ruby on rails app, that will have 2 different types of users (let's say sellers and buyers). I was thinking about using single table inheritance, but ultimately I decided to crate 2 separate models (I believe it's better solution in my case). The problem is when I try to create private message model (both sellers and buyers can contact each other). Typically I would just generate model called message with 3 fields (from_user_id to_user_id and content). This will not work in my

How to convert this respond_to options to use Rails 3 version?

萝らか妹 提交于 2019-12-22 00:07:46
问题 respond_to do |format| if @user.save format.js { render :nothing => true, :status => :ok, :location => @user } else format.js { render :json => @user.errors, :status => :unprocessable_entity } end end All options I've tried (like putting respond_to :js at the top of controller, etc) don't quite work the way as in this. 回答1: Rails 3 Format: Use respond_to :json and respond_with (@user) respond_to :json # You can also add , :html, :xml etc. def create @user= User.new(params[:user]) #---For html