Rails - How To Get Full URL in Model - ruby-on-rails

In my Rails 5 app, I'm trying to get the full URL for my instance, but when I tried to use url_for as suggested in this SO question, I get an error, NoMethodError: undefined method 'url_for' for #<Product:0x8646fc0>.
Full URL with url_for in Rails
def get_url
url = url_for(self)
end
I also tried product_url(self) and received a similar error.
What's the proper way to get the URL for my instance? Thanks!

helper method is only available in view or decorator.
it's better to define url in the decorator.
if you want to use helper method in the model, try this
def url
Rails.application.routes.url_helpers.product_url(self)
end

Related

Rails get controller action url

I have an action, and I've defined it in the routes.rb
resources :statistics, only: [] do
get 'group_report/:id', to: 'statistics#group_report', as: 'group_report'
end
Then in the method in the model, I want to return the full URL.
Thus, I wrote this method
root_url + group_report_statistics_path(id)
The result is "http://localhost:3000//statistics/group_report/DHo6qzUzYXw"
I thought it was a dirty way, and I need to handle the double slash as well.
Does Rails provide some useful method which I can get the full URL of the action directly?
Rails provides path as well as url helpers for all the routes.
So if you use group_report_statistics_url(id), you will get the full URL.
See Path and URL Helpers on the Rails guides for detailed description.
try this
File.join(root_url + group_report_statistics_path(id))
or
group_report_statistics_url(id)

Creating full_url method in Rails

I'm trying to make a method to check if the url is an absolute or relative url. I'm calling the method from my view template, but it's raising undefined method error. I believe it's not calling the method, and that's why raising the error, but I'm not sure how to make it work now.
Anything helps, thank you.
UsersController
def url_checker(url)
/^http/i.match(url) ? url : "http://#{url}"
end
users/show.html.slim
span= link_to_if #profile.company_url.present?, #profile.company_url, url_checker(#profile.company_url), target: "_blank"
Error message

Get current path name in Rails

I would like to be able to get the name of the current route in a request in Ruby on Rails. I've found ways I can access the controller and action of the request, but I would like to access a string or symbol of the name.
For example, if I have a users resource;
If I go to /users/1 I would like to be able to get users_path
If I go to /users/1/edit I would like to be able to get edit_users_path
I simply want to retrieve the name of the current route on a given request.
The following code will give it to you.
Rails.application.routes.recognize_path(request.request_uri)
Note that there are a couple of exceptions that can get thrown in ActionDispatch::Routing::RouteSet (which is what is returned from Rails.application.routes) so be careful about those. You can find the implementation of the method here.
You can use the following methods to get the current url in your action but none of them will give you the name of the route like users_path
request.original_url # => www.mysite.com/users/1
request.request_uri # = > /users/1
I had to use request.original_fullpath (Rails 5.2)
You can use the current_page? method to achieve this.
current_page?(user_path(#user))
current_page?(edit_user_path(#user))
Here's a link:
current_page? method

determining viability of routes generated with polymorphic_url

I've got a generic partial that includes a link if the object is linkable.
The URL is generated with polymorphic_url. If polymorphic_url raises NoMethodError, then I don't include the link.
This mostly works great, except where a resource has an update or destroy action but not a show action. In that case polymorphic_url generates a valid URL, it's just not valid with GET.
Is there any way to restrict polymorphic_url to a specific request method, or a nice way to determine if the returned URL is valid for GET?
I took a second look at recognize_path, and got it to work.
begin
url = polymorphic_url(...)
# validate URL, since polymorphic URL may return a URL for a
# different method
Rails.application.routes.recognize_path(url, {:method => 'GET')
url
rescue NoMethodError => e # raised if polymorphic_url fails
nil
rescue ActionController::RoutingError => e # raised if recognize_path fails
nil
end
This might depend on your setting, but can't you find a path to the controller in question and thus find out, whether he knows the show action?

URL Helper - undefined method

rake routes:
api_v1_accounts GET /api/v1/accounts(.:format) {:action=>"index", :controller=>"api/v1/accounts"}
That means I should have a url helper method:
api_v1_accounts_url
I am trying to test creating an account with rspec and the following line fails:
route = api_v1_accounts_url(account)
Saying that api_v1_accounts_url is an undefined method
I guess I've really misunderstood something about URL helper methods and the scope of them. Please help
include Rails.application.routes.url_helpers
That did the trick

Resources