Tag: ruby on rails 3

Google自定义搜索API与分页

我有这种方法将来自Google自定义搜索API的10个结果的链接放入一个数组中: require ‘json’ require ‘open-uri’ def create search = params[:search][:search] base_url = “https://www.googleapis.com/customsearch/v1?” stream = open(“#{base_url}key=XXXXXXXXXXXXX&cx=XXXXXXXXXX&q=#{search}&start=#{i}&alt=json”) raise ‘web service error’ if (stream.status.first != ‘200’) result = JSON.parse(stream.read) @new = [] result[‘items’].each do |r| @new << r['link'] end end 和我的观点: 我无法弄清楚如何添加分页,以便在第二页上返回接下来的10个结果。 我正在使用Kaminarigem进行分页。 我希望当用户点击指向其他网页的链接时,我会从Google的API中获取接下来的10个结果。 您可以使用API​​的start参数执行此操作,该参数指定要开始的第一个结果,我将其作为i传递。 我在考虑做这样的事情: i = (params[:page] – 1) * 10 + 1 其中params[:page]是当前的页码,但由于某种原因它是未定义的。 另外,我不确定如何为不是AR对象的数组设置分页,以及我认为会有什么。 […]

开始在Facebook上使用App?

现在,我看到很多应用程序会说“开始使用[App of App]”这只是对StreamPublish的调用,还是有新的函数调用来实现这一目标? 我目前正在使用Facebook允许人们使用类似于turntable.fm的facebook帐户登录,然后访问我的网页。 如何让其他朋友看到他们开始使用该应用程序,我无法在任何地方找到它。

捆绑安装失败 – 使用github url的Gem – 有什么问题?

在我的gemfile中我有: gem ‘capistrano-cook’, github: “subsis/capistrano-cook” 以下是在ruby / rails控制台中运行bundle install时的错误日志: http : //pastie.org/private/aftbq3rukjdthua4lfjzbw C:\rails\app>bundle install Unfortunately, a fatal error has occurred. Please see the Bundler troubleshooting documentation at http://bit.ly/bundler-issues. Thanks! C:/Ruby193/lib/ruby/gems/1.9.1/bundler/gems/capistrano-cook-d0d4f904a443/capistr ano-cook.gemspec:17:in “’: No such file or directory – git ls-files (Errno::ENO 在Git Bash控制台中运行bundle install时工作正常。 但是当需要capistrano部署文件中的gem时,它不会被加载。 好像它毕竟没有安装。 bundler gem是版本1.2.3 C:\rails\app>gem show capistrano-cook ERROR: While executing gem […]

无法启动Rails服务器

我正在研究Ubuntu 10.04 LTS,Lucid Lynx,已经安装了Ruby on Rails 3。 我无法启动Rails服务器。 我收到以下错误。 mah@mah-laptop:~/Desktop/projects/ver$ rails s /home/mah/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.8/lib/rails/commands.rb:29:in `block in ‘: undefined method `root’ for nil:NilClass (NoMethodError) from /home/mah/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.8/lib/rails/commands.rb:27:in `tap’ from /home/mah/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.8/lib/rails/commands.rb:27:in `’ from script/rails:6:in `require’ from script/rails:6:in `’ 我正在使用Rails 3.0.8和rake版本0.8.7 我尝试bundle exec但得到了同样的错误。 在此先感谢您的帮助 UPDATE 这是我在bundle exec rake db:migrate –trace mah@mah-laptop:~/Desktop/projects/ver$ bundle exec rake db:migrate –trace rake aborted! uninitialized constant […]

Devise&CanCan – CanCan 2.0 API的问题

我想为我的用户模型添加其他属性,并且不希望创建单独的配置文件模型。 我正在尝试使用RESTful操作集中的标准«更新»来更新自定义字段: class UsersController < ApplicationController before_filter :authenticate_user! # … def update @user = User.find(params[:id]) authorize! :update, @user respond_to do |format| if @user.update_attributes(params[:user]) format.html { redirect_to @user, notice: 'User was successfully updated.' } format.json { head :ok } else format.html { render action: "edit" } format.json { render json: @user.errors, status: :unprocessable_entity } end end […]

隐藏Rails模型属性

我有一个API的控制器,如下所示: def index respond_to do |format| format.json { render :json => @groups.to_json(:only => [:id, :name, :description, :created_at, :updated_at])} end end def show respond_to do |format| format.json { render :json => @group.to_json(:only => [:id, :name, :description, :created_at, :updated_at]) } end end # @todo add store to item def create if @group.save render :json => @group.to_json(:only => […]

为什么在Rails模型类中可以调用回调?

我有一个像这样的Rails模型类: class Something < ActiveRecord::Base before_create do self.key = SecureRandom.urlsafe_base64(8) end end 为什么我可以在这里打电话给before_create ? 我希望它是ActiveRecord::Base一种方法,但事实并非如此。 回调是ActiveRecord::Callbacks方法。 但为什么我可以在模型类中调用它们而不包含某些内容?

Rails 3 ActiveRecord where子句设置的子句或null

我有一个ActiveRecord,Annotation,有两列:user_id和post_id,可以为null。 具有null user_id和post_id的注释是适用于所有用户的所有post的“全局”注释。 我想检索与特定用户的post和所有全局注释相关的所有注释。 在MySQL中,SQL命令将是 select * from annotations where (user_id = 123 and post_id = 456) or (user_id is null and post_id is null) 在Rails 3中,什么是将其编码为Annotation.where子句的最佳方法?

DelayedJob:如何解决“作业无法加载”的问题?

我正在使用Ruby on Rails 3.1.0和DelayedJob。 网络上的很多人都得到了“ Job failed to load: uninitialized constant Syck::Syck ”错误,但我认为我至少发现了产生错误的内容(在我的情况下)。 我有一个类似以下的ActiveModel: class Contact include ActiveModel::Conversion include ActiveModel::Validations include ActiveModel::Dirty extend ActiveModel::Naming extend ActiveModel::Translation attr_accessor :full_name, :email, :subject, :message def initialize(attributes = {}) attributes.keys.each do |attr| instance_variable_set “@” + attr.to_s, attributes[attr.to_sym] end end validates_presence_of :full_name, :email, :subject, :message def persist @persisted = true […]

从日期x开始尝试为记录创建rails模型范围查询,使用lambda?

我想我会想要使用模型级查找器和lambda。 named_scope :recent_snaps, lambda {|since_when| {:conditions=>{:created_at >= since_when}}} 但我不确定,如果我的语法正确,特别是参数片,并且无法立即运行应用程序来检查控制台。 我不想使用find_by_sql或控制器查找,我希望我的finder在模型级别进行rspec测试。