2015-08-27 19 views
-2

我想为我的数据表建立一些链接,并且在过去的4个小时内一直在努力。如何在rails 4.2中使用url_helpers?

这是我当前的代码:

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并调用它。这与上述重复不同。

+0

事实并非如此。在rails 4.1中,每件事情都有效。在4.2中有所改变,没有文档,没有帮助,也没有关于这个错误的信息。我已经尝试了很多不同的东西和解决方案。我总是得到相同的url_for错误。 – renatojf

+0

从stackoverflow粘贴的每个解决方案,我已经尝试过。我仍然得到同样的错误。 – renatojf

+0

你粘贴的是一条评论,评论UrlHelper的包含。如果你阅读这个问题,你会发现我没有把它包含在任何地方。在粘贴随机解决方案之前,请阅读整个问题。 – renatojf

回答

0

您需要包括这在你的类:

class User < ActiveRecord::Base 
    include Rails.application.routes.url_helpers 

    def base_uri 
    user_path(self) 
    end 
end 

User.find(1).base_uri # => "https://stackoverflow.com/users/1" 

Check for refference:

+0

我更新了我的问题。我已经尝试过,并没有工作。 – renatojf

+1

这就是答案。这正是你需要将URL助手加入模型中。我使用的是Rails 3和Rails 4(4.0.1,4.1.2和4.2.3)上的几个应用程序, –