How to use url_helpers in rails 4.2?

元气小坏坏 提交于 2019-12-13 09:28:40

问题


I am trying to build some links for my datatable and been struggling with it for the past 4 hours.

This is my current code:

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

And this is the error I keep getting:

ArgumentError (arguments passed to url_for can't be handled. Please require routes or provide your own implementation)

Every thing I try to build my link from my model, doesn't work. I ran through rails issues on github and couldn't find nothing about this.

Any help?

edit:

I also tried the first solution proposed and still it doesn't work. I get the same error. This is what I did:

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

This is not a simple thing where you add the url inside a model and call it. This is different than the said duplication.


回答1:


You need to include this in you class:

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:



来源:https://stackoverflow.com/questions/32257545/how-to-use-url-helpers-in-rails-4-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!