在Rails中,是否可以在模块中命名模型并仍然从中获得正确的行为url_for
?
例如,在这里,url_for
按预期工作:
# app/models/user.rb
class User < ActiveRecord::Base
end
# config/routes.rb
resources :users
# app/views/users/index.html.haml
= url_for(@user) # /users/1
Run Code Online (Sandbox Code Playgroud)
将User
模型放入模块后,url_for
抱怨一个未定义的方法m_user_path
:
# app/models/m/user.rb
module M
class User < ActiveRecord::Base
end
end
# config/routes.rb
resources :users
# app/views/users/index.html.haml
= url_for(@user) # undefined method 'm_users_path'
Run Code Online (Sandbox Code Playgroud)
是否有可能有url_for
忽略模块M::User
,并返回user_path
用于url_for(@user)
代替m_user_path
?
UPDATE
所以,经过近5年的时间,这是解决方案,感谢esad.这已在Rails 4.2中测试过.
# app/models/m/user.rb
module M
class User < ActiveRecord::Base
end
end
# app/models/m.rb
module M …
Run Code Online (Sandbox Code Playgroud) 我正在使用rails_admin和globalize3,并且无法使可搜索的关联工作.以下是模型(Person has_one/belongs_to Name has_many/belongs_to NameTranslation):
class Person < ActiveRecord::Base
has_one :name, inverse_of: :person
end
class Name < ActiveRecord::Base
belongs_to :person, inverse_of: :name
translates :first_name, :last_name
has_many :name_translations, inverse_of: :name, dependent: :destroy
end
class NameTranslation < ActiveRecord::Base
belongs_to :name, inverse_of: :name_translations
end
Run Code Online (Sandbox Code Playgroud)
该NameTranslation
模型来自globalize3,它包含与name(first_name
和last_name
)plus locale
和name_id
,相同的属性.
在config/initializers/rails_admin.rb
我有
config.model Person do
list do
field :name do
searchable name_translations: :last_name
end
end
end
Run Code Online (Sandbox Code Playgroud)
然后,在GUI中,当我添加过滤器时name
,我得到:
SQLite3::SQLException: no such column: name_translations.last_name: SELECT "people".* FROM …
Run Code Online (Sandbox Code Playgroud) 我想avconv
用来将一系列PNG图像转换为WebM视频,保持透明度.
我知道输出视频中使用的像素格式必须支持透明度.所以我尝试过:
$ avconv -framerate 25 -f image2 -i frames /%03d.png -pix_fmt yuva420p output.webm
不幸的是avconv
抱怨:
Incompatible pixel format 'yuva420p' for codec 'libvpx-vp9', auto-selecting format 'yuv420p'
我在用ffmpeg version 2.8.4-1+b1 Copyright (c) 2000-2015 the FFmpeg developers
.