Tag: ruby on rails

sqlite3 gem无法安装

我正在尝试在OS X 10.6上安装“sqlite3-ruby”gem(或“sqlite3”gem)。 我正在使用ruby-1.9.2,目前我得到以下内容: $ sqlite3 –version 3.7.4 $ sudo gem install sqlite3 Building native extensions. This could take a while… ERROR: Error installing sqlite3-ruby: ERROR: Failed to build gem native extension. /Users/folken/.rvm/rubies/ruby-1.9.2-head/bin/ruby extconf.rb checking for sqlite3.h… *** extconf.rb failed *** Could not create Makefile due to some reason, probably lack of necessary libraries and/or […]

如何避免NoMethodError在嵌套哈希中缺少元素,而不重复nil检查?

我正在寻找一种避免在深层嵌套哈希中检查每个级别的nil的好方法。 例如: name = params[:company][:owner][:name] if params[:company] && params[:company][:owner] && params[:company][:owner][:name] 这需要三次检查,并且会产生非常难看的代码。 有办法解决这个问题吗?

如何要求在rails之外工作的活动记录

我需要活动记录,但我在rails之外工作(这就是为什么: 简单的Ruby输入validation库 )。 我需要整个rails gem,还是我可以DRYer?

带有has_many的counter_cache:通过

我刚刚创建了一个counter_cache字段,控制器看起来像这样。 @users = User.where(:sex => 2).order(‘received_likes_count’) User.rb中的关联是 has_many :received_likes, :through => :attachments, :source => :likes, :dependent => :destroy 问题是counter_cache是​​在Like.rb的belongs_to中声明的,我不知道如何告诉它是为了has_many:通过关联。 belongs_to :user, :counter_cache => :received_likes

如何在Sass中使用Ruby / Rails变量?

有没有办法在Sass文件中使用我的Ruby应用程序中的变量?

自动加载常量用户时检测到循环依赖性

我已经按照本教程( http://railscasts.com/episodes/236-omniauth-part-2 )使用OmniAuth和Devise创建facebook登录,我收到此错误:在我的路由中自动加载常量用户时检测到循环依赖性。 RB devise_for :users , :controllers => {:registrations => ‘registrations’} registrations_controller.rb Class RegistrationsController < Devise::RegistrationsController def create super session[:omniauth] = nil unless @user.new_record? end private def build_resource(*args) super if session["devise.omniauth"] @user.apply_omniauth(session["devise.omniauth"]) session["devise.omniauth"] = nil end end end 这是来自AuthenticationsController的我的创建方法 def create omniauth = request.env[“omniauth.auth”] authentication = Authentication.find_by_provider_and_uid(omniauth[‘provider’], omniauth[‘uid’]) if authentication flash[:notice] = “Signed in […]

“gem install rails”因DNS错误而失败

$ rvm use Using /home/owner/.rvm/gems/ruby-2.1.2 $ gem install rails ERROR: While executing gem … (Gem::RemoteFetcher::FetchError) Errno::ECONNREFUSED: Connection refused – connect(2) for “your-dns-needs-immediate-attention.network” port 80 (http://your-dns-needs-immediate-attention.network/quick/Marshal.4.8/thread_safe-0.3.4.gemspec.rz) $ gem update –system … $ gem -v 2.4.1 $ gem install rails ERROR: While executing gem … (Gem::RemoteFetcher::FetchError) Errno::ECONNREFUSED: Connection refused – connect(2) for “your-dns-needs-immediate-attention.network” port 80 (http://your-dns-needs-immediate-attention.network/quick/Marshal.4.8/rails-4.1.5.gemspec.rz) 我试过了: […]

POST json到rails服务器

def create req = ActiveSupport::JSON.decode(request.body) if user = User.authenticate(req[“email”], req[“password”]) session[:user_id] = user.id render :json => “{\”r\”: \”t\”}” + req else render :json => “{\”r\”: \”f\”}” end end ‘create’方法在控制器中并映射到“/ login”,我正在设置正确的内容类型并从我的curl客户端接受标头。 我一直收到422 http状态响应。 有什么建议?

ROR +无法安装tiny_tds

在这里,我试图从MS-SQL Server 2008获取数据到Ubuntu 10上的我的Rails应用程序。但是我无法安装tiny_tds 。 我按照github给出的步骤进行操作。 但没有回应。 请指导我正确设置。 使用gem命令:: gem install tiny_tds 这个命令也是:: gem install tiny_tds –with-freetds-include=/usr/local/include/freetds –with-freetds-lib=/usr/local/lib 错误: Installing tiny_tds (0.4.5) with native extensions /home/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/rubygems/installer.rb:483:in `rescue in block in build_extensions’: ERROR: Failed to build gem native extension. (Gem::Installer::ExtensionBuildError) /home/.rvm/rubies/ruby-1.9.2-p180/bin/ruby extconf.rb looking for library directory /home/.rvm/gems/ruby-1.9.2-p180@rails3/lib … no looking for library directory /home/.rvm/gems/ruby-1.9.2-p180@rails3/lib/freetds … no […]

Ruby:IF语句中的Nils

我正在使用的rails应用程序中有以下非常难看的ruby代码: if params.present? if params[:search].present? if params[:search][:tags_name_in].present? … end end end 所有我想问的是是否已经定义了params [:search] [:tags_name_in],但因为params,params [:search]和params [:search] [:tags_name_in]可能都是nil,如果我使用… if params[:search][:tags_name_in].present? …如果没有参数或没有搜索参数,我会收到错误。 肯定有一个更好的方法来做到这一点……建议?