小编seb*_*ian的帖子

Rails url_for和命名空间模型

在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)

namespaces ruby-on-rails url-for ruby-on-rails-3

13
推荐指数
1
解决办法
4509
查看次数

rails_admin可搜索的关联

我正在使用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_namelast_name)plus localename_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)

rails-admin

8
推荐指数
1
解决办法
3212
查看次数

将PNG转换为具有透明度的webm视频

我想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.

video ffmpeg webm avconv

6
推荐指数
2
解决办法
1万
查看次数