2015-08-27 49 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)上的幾個應用程序, –