Tag: ruby on rails

Ruby on Rails命名为model的class属性

我有两个类Product和user ,它们具有与多态类型的has_many关系。 class User < ActiveRecord::Base has_many :pictures, as: :imageable end class Product < ActiveRecord::Base has_many :pictures, as: :imageable end class Picture < ActiveRecord::Base belongs_to :imageable, polymorphic: true end 我还想为profile_picture的用户模型添加一个新属性,因此@user.profile_picture将返回一个图片对象。 怎么能实现呢? 特别是向用户添加外键的迁移。 编辑:我已经更新了用户模型 has_one :profile_picture, class_name: “Picture”, foreign_key: “profile_picture_id” 并添加了以下迁移 add_column :users, :profile_picture_id, :integer 然而我似乎无法设置个人资料图片,如果有人可以解释我做错了什么,将不胜感激。 谢谢!

如何在Ruby on Rails中提供xls文件链接?

How to give the xls file link in ruby. This is my file path link_to “Excel”, “/#{RAILS_ROOT}/public/reports/10014_ByNetwork.xls”, :target=>”_blank” 当我给上面的链接转换像这样 Excel 所以它不起作用。 实际上我需要这样的 Excel 请给我一个确切的路径……

从Ruby字符串中提取信息

我在远程设备上有一个与此类似的文本文件: D0-23-DB-31-04-3E%20192.168.4.42%20dnat 68-A8-6D-0C-38-B2%20192.168.4.40%20dnat 我创建了一个小的Ruby脚本来将其转换为字符串并将其发布到我的Rails应用程序: def chilli_list hash = File.open(“/tmp/client_list”, “rb”).read return hash end 然后输出如下: “D0-23-DB-31-04-3E%20192.168.4.42%20dnat\n68-A8-6D-0C-38-B2%20192.168.4.40%20dnat\n” 我需要逐位提取信息并在视图中显示。 到目前为止,我还没有走得太远。 我尝试了以下哪个是好的: str = “D0-23-DB-31-04-3E%20192.168.4.42%20dnat\n68-A8-6D-0C-38-B2%20192.168.4.40%20dpass\n” str.each_line do |line| puts line.split(” “).first puts line.split(” “).second end 这是最好的方式还是有更好的方法? 最后也是最重要的是,我还需要对字符串执行一些计算。 我可以使用str.lines.count计算行str.lines.count ,但我需要的是第三个值== nat的行的计数,如上例所示。 我该怎么办呢?

为#

设计注册我得到了这个错误: undefined method `users_url’ for # 有了Omniauth facebook,登录,一切正常。 class User :destroy after_save :myprofile def myprofile if self.profile else Profile.create(user_id: self.id, user_name: self.name) end end end class Profile < ActiveRecord::Base belongs_to :user end 使用设计注册可以解决这个问题的解决方案是什么? 重要提示:它适用于omniauth facebook ,但它不适用于设计注册。 编辑:我在Profile.create中遇到此错误! 方法: NoMethodError – undefined method `users_url’ for #: actionpack (3.2.13) lib/action_dispatch/routing/polymorphic_routes.rb:129:in `polymorphic_url’ actionpack (3.2.13) lib/action_dispatch/routing/url_for.rb:150:in `url_for’ actionpack (3.2.13) […]

渲染另一个命名空间的部分

我有个问题。 我需要使用另一个命名空间的部分来渲染对象。 render complain.target 它尝试从当前命名空间渲染部分(当前是admin ) 缺少部分管理员/公告/公告…… 我不需要从admin /中呈现它。 我不能指定部分路径 render partial: ‘/bulletins/bulletin’, locals: { bulletin: complain.target } 但它是多态关联,并使用不同的部分路径。 这有什么办法吗? 提前致谢!

用管道连接link_to

我想用管道连接几个链接。 但所有链接都被if语句包围。 例: – if condition1 = link_to link1 – if condition2 = link_to link2 – if condition3 = link_to link3 如果条件1和2为真,则结果应为 link1 | link2 任何提示如何做到这一点?

轨道中的事件触发系统设计

我正在重新设计我的活动源,我已经使用redis和rails实现了逻辑(顺便说一下,这很好用)但我仍然不确定如何创建/触发事件。 在我的第一种方法中,我使用了观察者,其缺点是没有current_user可用。 并且,无论如何使用观察者是一个坏主意:) 我首选的方法是在控制器中创建/触发事件,它应该看起来像: class UserController < LocationController def invite … if user.save trigger! UserInvitedEvent, {creator: current_user, …}, :create …. end end end 触发方法应该 使用一些参数创建UserInvitedEvent。 (:create可以是默认选项) 可以停用(例如停用以进行测试) 可以用例如resque执行 我看了一些gem(fnordmetrics,…),但我找不到一个光滑的实现。

haml html属性中的正则表达式

是posibble有这样的东西? : %div{“data-regex”: “a/regular/expression”} 当我尝试这样做时,我收到此错误: syntax error, unexpected ‘:’, expecting tASSOC 我试过这个: %div{“data-regex”: #{“a/regular/expression”}} ,但是是一样的。

NGINX上的多个Rails应用程序(反向代理)

我的服务器上有两个rails应用程序。 它们中的每一个都在瘦服务器上运行。 我也在使用NGINX。 这是我的NGINX配置文件: server{ location /blog { proxy_pass http://127.0.0.1:8082; proxy_set_header Host $host; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection “upgrade”; } location /website1 { proxy_pass http://127.0.0.1:3000; proxy_set_header Host $host; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection “upgrade”; } } “ http:// HOST / blog ”=>我收到404错误(空白页) “ http:// [HOST] / website1 ”=>我的Rails应用程序出现404错误,在我的应用程序日志中得到: INFO — : […]

允许类别中的多个记录提交到列表

如果选择了多个列表,我希望将多个(数组……)category_id保存到每个列表中。 以下是所有内容的设置方式,包括类别如何与列表一起使用。 分类型号: class Category < ApplicationRecord validates_uniqueness_of :category has_many :listings 上市模型: has_and_belongs_to_many :categories, required: false attr_accessor :new_category_name before_save :create_category_from_name # has_many :categories 计划(类别和列表): create_table “categories”, force: :cascade do |t| t.string “name” t.datetime “created_at”, null: false t.datetime “updated_at”, null: false end create_table “listings”, force: :cascade do |t| t.string “name” t.text “description” t.decimal “price” t.datetime “created_at”, […]