How to use url_helpers in rails 4.2?
Asked Answered
P

1

-2

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.

Plectron answered 27/8, 2015 at 19:4 Comment(6)
it is not. In rails 4.1 every thing works. Something changed in 4.2 and there is no documentation, no help and nothing about this error. I tried a lot of different things and solutions already. I always get the same url_for error.Plectron
Every solution you paste from stackoverflow, I already tried. I still get the same error.Plectron
What you've pasted is a comment saying to comment out the include of UrlHelper. If you read the question, you will see that I don't include it anywhere. Before pasting random solutions, please read the entire question.Plectron
You downvote a question without reading it, I was polite for the whole time and was pointing out that you didn't even read the question before saying it was a duplicate and now you imply you have an asnwer that I bet you don't have. If you are done, please go bother someone else. I'm really trying to get help from who understand things.Plectron
Are you getting the same ArgumentError even after using the updated code?Owlet
What do your routes look like? rake routes | grep userOwlet
C
0

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:

Creosol answered 27/8, 2015 at 19:12 Comment(2)
I updated my question. I tried this already and didn't work as well.Plectron
This is the answer. It is exactly what you need to get the URL helpers into a model. I use it is several apps on Rails 3 and Rails 4 (4.0.1, 4.1.2, and 4.2.3)Owlet

© 2022 - 2024 — McMap. All rights reserved.