这是如何将字符串转换为Rails/Ruby中的类:
p = "Post"
Kernel.const_get(p)
eval(p)
p.constantize
Run Code Online (Sandbox Code Playgroud)
但是,如果我从数组/活动记录对象中检索方法,如下所示:
Post.description
Run Code Online (Sandbox Code Playgroud)
但它可能是
Post.anything
Run Code Online (Sandbox Code Playgroud)
什么都是字符串之类的anything = "description".
这很有用,因为我想重构一个非常大的类并减少代码和重复行.我怎样才能使它工作?
我的大多数应用程序都与Web服务有很大关系,并且通常由于第三方站点,我会遇到超时问题.
这是我得到的错误:
execution expired
/usr/lib/ruby/1.8/timeout.rb:54:in `rbuf_fill'
Run Code Online (Sandbox Code Playgroud)
如何在rails应用程序中挽救此类错误?
我有一个Rails 3应用程序准备好进行分期.
我还没有设置VPS主机.因为我计划在最初几个月在共享主机上拥有所有内容.
问题:
cd myapp包检查结果:
The Gemfile's dependencies are satisfied
Run Code Online (Sandbox Code Playgroud)
乘客错误:
Error message:
no such file to load -- bundler
Exception class:
LoadError
Run Code Online (Sandbox Code Playgroud)
关于共享主机的令人沮丧的事情是我必须在config.ru上添加这些行:
ENV['GEM_HOME'] = '/home/username/.gems'
ENV['GEM_PATH'] = '$GEM_HOME:/usr/lib/ruby/gems/1.8'
Run Code Online (Sandbox Code Playgroud)
仍然没有运气.同样没有这样的文件加载捆绑器错误出现.
有人有这个工作吗?Rails 3,Debian,共享主机(dreamhost)?
我可以继续在Slicehost/Fivebean上注册,但在此之前,我想知道为什么会出现这个错误.
谢谢.
我一直在寻找如何解决这个问题,但找不到任何真实有效的解决方案.
用户模型:
validates_attachment_content_type:avatar,:content_type => ['image/jpeg','image/pjpeg','image/x-png','image/png','image/gif']
上传无效文件类型时会显示不必要的错误:
Avatar /tmp/sample,23283,0.txt is not recognized by the 'identify' command.
Run Code Online (Sandbox Code Playgroud)
服务器细节:Ubuntu和Debian
它将结果标识为/ usr/bin/identify
指定命令路径无法解决问题:
Paperclip.options[:command_path] = "/usr/bin"
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我查看了geometry.rb文件,似乎有些问题.
def self.from_file file
file = file.path if file.respond_to? "path"
geometry = begin
Paperclip.run("identify", %Q[-format "%wx%h" "#{file}"[0]])
rescue PaperclipCommandLineError
""
end
parse(geometry) ||
raise(NotIdentifiedByImageMagickError.new("#{file} is not recognized by the 'identify' command."))
end
Run Code Online (Sandbox Code Playgroud)
如果有人解决了同样的问题,请告诉我.非常感谢.
我的environment.rb包含:
ENV['GEM_PATH'] = File.expand_path('~/.gems') + ':/usr/lib/ruby/gems/1.8'
Run Code Online (Sandbox Code Playgroud)
我的gemfile包含:
source 'http://rubygems.org'
source :gemcutter
gem 'rails', '3.0.0.rc'
gem 'mysql'
gem 'haml'
gem 'RedCloth'
gem "friendly_id", "~> 3.0"
gem 'agnostic-will_paginate', "~> 3.0.0"
# Use unicorn as the web server
# gem 'unicorn'
# Deploy with Capistrano
#gem 'capistrano'
# To use debugger
# gem 'ruby-debug'
# Bundle the extra gems:
# gem 'bj'
# gem 'nokogiri', '1.4.1'
# gem 'sqlite3-ruby', :require => 'sqlite3'
# gem 'aws-s3', :require => 'aws/s3'
Run Code Online (Sandbox Code Playgroud)
在dreamhost上部署Rails 3应用程序后出现的错误是:
您已经激活了机架1.1.0,但您的Gemfile需要机架1.2.1.考虑使用bundle exec.
/home/dreamhostuser/.gems/gems/bundler-1.0.0.rc.1/lib/bundler/runtime.rb …Run Code Online (Sandbox Code Playgroud) 这看起来像一个bug,但我不确定.任何人都可以告诉在不同的端口号上运行iex是否有效.它总是使用端口4000.
PORT=4001 iex -S mix phoenix.server
Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
[info] Running AppMessenger.Endpoint with Cowboy using http on port 4000
Interactive Elixir (1.2.5) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> 19 May 19:53:40 - info: compiled 5 files into 2 files, copied 3 in 714ms
Run Code Online (Sandbox Code Playgroud) 有没有办法在 Elixir 中重构它以使其更具可读性?
def validate(params) do
Repo.start_link
if validate_oauth_params(params) === true do
oauth_client = Repo.get_by(OauthClient, random_id: params["client_id"], secret: params["secret"])
if oauth_client != nil do
allowed_grant_types = Poison.Parser.parse!(oauth_client.allowed_grant_types)
if Map.has_key?(allowed_grant_types, params["grant_type"]) do
case params["grant_type"] do
"password" ->
process_password_grant(params, oauth_client)
"refresh_token" ->
process_refresh_token_grant(params["refresh_token"], oauth_client)
"client_credentials" ->
process_client_credentials_grant(oauth_client)
nil ->
%{message: "Invalid oauth credentials", code: 400}
end
end
else
%{message: "Invalid oauth credentials", code: 400}
end
else
%{message: "Invalid oauth credentials", code: 400}
end
end
Run Code Online (Sandbox Code Playgroud)
Elixir 的做法是什么,因为这段代码看起来像 PHP。不是我写的。