[英]How to use url_helpers in rails 4.2?
我正在嘗試為我的數據表建立一些鏈接,並在過去的 4 小時內一直在努力解決這個問題。
這是我當前的代碼:
class UsersDatatable < TemplateDatatable
def data
users.map do |user|
[
user.id,
user.nome,
user.email,
user.provider.camelize,
links(user)
]
end
end
def links(user)
html = link_to url_helpers.edit_user_registration_path(user), class: "btn btn-mini btn-info", "data-rel" => "tooltip", title: "Editar", "data-placement" => "top" do
content_tag(:i, '', class: "icon-edit")
end.html_safe
end
end
class TemplateDatatable
include ActionView::Helpers::FormTagHelper
include ActionView::Context
delegate :params, to: :@view
delegate :url_helpers, to: 'Rails.application.routes'
end
這是我不斷收到的錯誤:
ArgumentError (arguments passed to url_for can't be handled. Please require routes or provide your own implementation)
我嘗試從我的模型建立鏈接的每件事都不起作用。 我在 github 上遇到了 Rails 問題,但一無所獲。
有什么幫助嗎?
編輯:
我也嘗試了提出的第一個解決方案,但仍然不起作用。 我犯了同樣的錯誤。 這就是我所做的:
class User < ActiveRecord::Base
include Rails.application.routes.url_helpers
def edit_registration_path
edit_user_registration_path(self)
end
end
def links(user)
html = link_to user.edit_registration_path, class: "btn btn-mini btn-info", "data-rel" => "tooltip", title: "Editar", "data-placement" => "top" do
content_tag(:i, '', class: "icon-edit")
end.html_safe
在模型中添加 url 並調用它並不是一件簡單的事情。 這與上述復制不同。
您需要將其包含在您的課程中:
class User < ActiveRecord::Base
include Rails.application.routes.url_helpers
def base_uri
user_path(self)
end
end
User.find(1).base_uri # => "/users/1"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.