我正在使用Ruby on Rails 3,我正在尝试使用中间件来设置@variable_name稍后在控制器中可访问的变量.
例如,我的中间件是
class Auth
def initialize(app)
@app = app
end
def call(env)
@account ||= Account.find(1)
@app.call(env)
end
end
Run Code Online (Sandbox Code Playgroud)
上面的代码正确设置了@account变量,但是在我的应用程序中没有(在控制器,模型,视图......中).那么,我该怎么做呢?
我看到这个答案是一种做我需要的方法,但我希望@account变量"可以直接访问".也就是说,不使用那种方式,但使其可用,例如在我的视图中,像这样:
<%= debug @account %>
Run Code Online (Sandbox Code Playgroud) 我正在使用Ruby on Rails 3,我想覆盖(可能在模型文件中)as_json或to_json方法,以便在respond_to不包含某些信息的情况下进行HTTP请求.
在我的帐户模型中,我有
def as_json(options = {})
super(
:except => [
:password
]
)
end
Run Code Online (Sandbox Code Playgroud)
在我的控制器中我有
format.json {
render :json => @account, :status => 200
}
Run Code Online (Sandbox Code Playgroud)
例如,当我提出请求时,/account/1.json我还要回到密码属性,出于安全考虑,我不想要.
那么,如何防止包含指定信息?
我能做到这一点并且有效
format.json {
render :json => @account.to_json(:except => [:password]), :status => 200
}
Run Code Online (Sandbox Code Playgroud)
但我需要重构.
我试图在我的Mac Os Snow Leopard 10.6.7上设置git 但是我做了一些错误......
这时我有以下警告:
$ git config --global core.editor
EDITOR=/usr/bin/vim
error: More than one value for the key core.editor: mate
$ git config --global core.editor open
warning: core.editor has multiple values
Run Code Online (Sandbox Code Playgroud)
我怎么解决这个问题?而且,大多数情况下,我如何设置core.editorTextEdit并使其工作?
PS:我已经读过这个问题.
我正在使用MySQL数据库,我想知道我是否同时进行多个(500个或更多)查询以获取来自多个表的信息,如何处理这些查询?顺序还是并行?
我正在使用Ruby on Rails 3,我想覆盖该to_json方法.
这时我使用以下代码以避免导出重要信息.
def to_json
super(
:except => [
:password
]
)
end
Run Code Online (Sandbox Code Playgroud)
如果我想使用该方法更改值,我该怎么办?
例如,我想将用户名大写
:name => name.capitalize
Run Code Online (Sandbox Code Playgroud)
检索这个
@user.to_json
Run Code Online (Sandbox Code Playgroud) 我正在运行Ruby on Rails 3,我想使用Paperclip插件/ gem来降低上传图像的质量.我怎样才能做到这一点?
此时在我的模型文件中我有:
has_attached_file :avatar,
:styles => {
:thumb => ["50x50#", :jpg],
:medium => ["250x250#", :jpg],
:original => ["600x600#", :jpg] }
Run Code Online (Sandbox Code Playgroud)
将图像转换为.jpg格式并将设置尺寸.
我正在使用Ruby on Rails 3,我正在尝试实现API以从Web服务检索帐户信息.也就是说,我想连接到具有Account类的Web服务,并从showURI中路由的操作中获取信息http://<site_name>/accounts/1.
这时,在accounts_controller.rb我的Web服务文件中:
class AccountsController < ApplicationController
def show
@account = Account.find(params[:id])
respond_to do |format|
format.html
format.js
format.json { render :json => @account.to_json }
end
end
end
Run Code Online (Sandbox Code Playgroud)
现在我需要一些建议来连接到Web服务.在客户端应用程序中,我应该有一个HTTP GET请求,但这是我的问题:连接到发出HTTP请求的Web服务的"最佳"方法是什么?
客户端应用程序中的此代码有效:
url = URI.parse('http://<site_name>/accounts/1.json')
req = Net::HTTP::Get.new(url.path)
res = Net::HTTP.start(url.host, url.port) {|http|
http.request(req)
}
@output = JSON(res.body)["account"]
Run Code Online (Sandbox Code Playgroud)
但是,上面的代码是"实现API的方式"吗?
是否可以使用第三方插件和宝石?
我正在使用Ruby on Rails 3,我想知道在什么情况下我应该使用实例变量而不是其他变量类型,如果有安全原因那些.
例:
# Using an instance variable
@accounts = Account.find(...)
# Using a "local"\"normal" variable
account = Account.find(...)
Run Code Online (Sandbox Code Playgroud) 我正在使用Python,我有一个JSON文件,我想在其中更新与给定密钥相关的值.也就是说,我有my_file.json包含以下数据
{"a": "1", "b": "2", "c": "3"}
Run Code Online (Sandbox Code Playgroud)
我想只更改与b键相关的值2,9以便更新的文件看起来像:
{"a": "1", "b": "9", "c": "3"}
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
我尝试了以下但没有成功(更改未保存到文件中):
with open('my_file.json', 'r+') as f:
json_data = json.load(f)
json_data['b'] = "9"
f.close()
Run Code Online (Sandbox Code Playgroud) 我正在尝试正确使用Capistrano和RVM,以便将我的Ruby on Rails 3.2.2应用程序部署到运行Ubuntu 10.04 LTS的远程机器上.它接缝,我解决了我以前的相关问题" RVM -对Linux操作系统Ubuntu Capistrano的整合 ".但是,在部署时,我得到与DelayedJob gem相关的以下错误:
...
* executing "cd /srv/www/<APP_NAME>/releases/20120314135318 && bundle install"
servers: ["<DOMAIN>"]
[<DOMAIN>] executing command
[<DOMAIN>] rvm_path=/usr/local/rvm /usr/local/rvm/bin/rvm-shell 'ruby-1.9.3-p125' -c 'cd /srv/www/<APP_NAME>/releases/20120314135318 && bundle install' ** [out :: <DOMAIN>] Using rake (0.9.2.2)
** [out :: <DOMAIN>] Using builder (3.0.0)
...
** [out :: <DOMAIN>] Using daemons (1.1.8)
** [out :: <DOMAIN>] Using delayed_job (3.0.1)
** [out :: <DOMAIN>] Using delayed_job_active_record (0.3.2)
...
** [out :: <DOMAIN>] Your bundle is …Run Code Online (Sandbox Code Playgroud) ruby ×7
json ×3
variables ×2
api ×1
capistrano ×1
class ×1
connection ×1
delayed-job ×1
editor ×1
file ×1
git ×1
macos ×1
middleware ×1
mysql ×1
overriding ×1
paperclip ×1
performance ×1
python ×1
rvm ×1
security ×1
sql ×1
storage ×1
updating ×1
warnings ×1