如何在rails 4.2中使用url_helpers?

时间:2015-08-27 19:04:47

标签: ruby-on-rails ruby-on-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并调用它并不是一件简单的事情。这与上述重复不同。

1 个答案:

答案 0 :(得分:0)

您需要在课程中加入此内容:

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"
  

Check for refference: