我正在开发基于rails的REST api.要使用这个API,你必须登录.关于这一点,我想me在我的用户控制器中创建一个方法,它将返回登录用户信息的json.所以,我不需要:id在URL中传递.我只想打电话给http://domain.com/api/users/me
所以我尝试了这个:
namespace :api, defaults: { format: 'json' } do
scope module: :v1, constraints: ApiConstraints.new(version: 1, default: true) do
resources :tokens, :only => [:create, :destroy]
resources :users, :only => [:index, :update] do
# I tried this
match 'me', :via => :get
# => api_user_me GET /api/users/:user_id/me(.:format) api/v1/users#me {:format=>"json"}
# Then I tried this
member do
get 'me'
end
# => me_api_user GET /api/users/:id/me(.:format) api/v1/users#me {:format=>"json"}
end
end
end
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我的路线等待身份证,但我想得到像设计一样的东西.基于current_userid的东西.示例如下:
edit_user_password GET /users/password/edit(.:format) …Run Code Online (Sandbox Code Playgroud) 我不明白为什么会出现以下错误:
ActiveRecord::AssociationTypeMismatch: User(#29943560) expected, got Fixnum
Run Code Online (Sandbox Code Playgroud)
当我在rails控制台中执行此操作时: @game = Game.create(:player => 1060, :played => 1061)
我只是想在下面创建一个新Game的模型关联.
class User < ActiveRecord::Base
has_many :game_as_player, :class_name => 'Game', :foreign_key => 'player_id'
has_many :game_as_played, :class_name => 'Game', :foreign_key => 'played_id'
end
class Game < ActiveRecord::Base
belongs_to :player, :class_name => 'User'
belongs_to :played, :class_name => 'User'
attr_accessible :player, :played, :score, :details, :viewed, :read
end
Run Code Online (Sandbox Code Playgroud)
如果有人有想法......非常感谢!
我想在我的rails 3.2应用程序中使用gem"better_errors",但它仍然是显示的默认错误页面.
在我的Gemfile中:
group :development do
gem 'quiet_assets', '>= 1.0.1'
gem 'better_errors', '>= 0.3.2'
gem 'binding_of_caller', '>= 0.6.8'
end
Run Code Online (Sandbox Code Playgroud)
然后我做了"捆绑安装"
bundle show better_errors
/usr/local/lib/ruby/gems/1.9.1/gems/better_errors-0.5.0
Run Code Online (Sandbox Code Playgroud)
一切似乎都配置得很好.
我在开发环境中运行我的rails服务器
rails s -e development
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?非常感谢.
我想通过 activerecord-postgis-adapter gem 优化当前使用 postgis 的 Rails 3.2.13 应用程序。
问题是,当我在表中进行查询时,即使其中只有常规字段(没有地理/几何/这类东西),此查询之前还有对 postgis“geometry_columns”表的另一个查询。
例子:
(5.6ms) SELECT * FROM geometry_columns WHERE f_table_name='srlzd_infos'
SrlzdInfo Load (1.1ms) SELECT "srlzd_infos".* FROM "srlzd_infos" WHERE "srlzd_infos"."user_id" = 1009 LIMIT 1
Run Code Online (Sandbox Code Playgroud)
但我只在我的用户表/模型中使用 postgis。
有谁知道我怎样才能避免那些不必要的查询?
谢谢你们。
gis postgis ruby-on-rails rails-postgresql ruby-on-rails-3.2
我遵循官方的github存储库示例,但是当我在版本方法上定义条件时,我总是得到:
ArgumentError - wrong number of arguments (1 for 0):
(gem) carrierwave-0.8.0/lib/carrierwave/uploader/versions.rb:198:in `block in active_versions'
(gem) carrierwave-0.8.0/lib/carrierwave/uploader/versions.rb:192:in `active_versions'
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
class PhotoUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
version :thumb_75, :if => :is_user? do
process :resize_to_fill => [75, 75]
end
version :thumb_87, :if => :is_question? do
process :resize_to_fill => [87, 87]
end
protected
def is_user?
model.class.to_s == 'Photo'
end
protected
def is_question?
model.class.to_s == 'Question'
end
end
Run Code Online (Sandbox Code Playgroud)
如果我删除它的工作条件......任何想法它可能是什么?谢谢
我想过滤一下 { "query" : { "match_all" :{}}}弹性搜索,但我不明白......
这是我发送给ES _search方法的内容.
curl -XGET http://localhost:9200/users/location/_search '-H Accept: application/json' '-H Content-Type: application/json'
-d '{
"query":{
"match_all":{}
},
"filter":{
"and":{
"geo_distance":{
"distance":"500km",
"location":{
"lat":48.8,
"lon":2.33
}
},
"term":{
"status":1
}
}
},
"sort":[
{
"_geo_distance":{
"location":[
2.33,
48.8
],
"order":"asc",
"unit":"km"
}
}
]
}'
Run Code Online (Sandbox Code Playgroud)
但我总是得到这个错误:
nested: QueryParsingException[[users] [and] filter does not support [distance]]
Run Code Online (Sandbox Code Playgroud)
如果我删除该"and" :{}选项并仅对geo_distance进行过滤,则可以正常工作......任何帮助都会非常棒.
干杯