Absolute URL's with link_to...Ruby on Rails - ruby-on-rails

Is it possible to generate absolute URL's in rails using link to? [NOTE: THIS IS IN A MAILER]
I tried to do:
<%= link_to root_url, root_url%>
But I get a runtime error:
*Missing host to link to! Please provide :host parameter or set default_url_options[:host]*
I need this to be dynamic because the application will run on a wildcard domain (*.domain.com)

If you use the _url suffix, the generated URL is absolute. Use _path to get a relative URL.
<%= link_to "Home", root_url %>
<%= link_to "Home", root_path %>

Depending on your use case, string interpolation might be a good solution:
link_to(body, "http://#{site_url}")

I found this plugin:
http://www.simonecarletti.com/blog/2009/10/actionmailer-and-host-value/
and it works great!

In routes.rb insert :
root :to => 'controller#action'
Or replace your current map.root with the correct one.
See documentation about this : routes.rb usage

Related

How to use link_to to go to a subdomain

I am having trouble using link_to to go to my posts/index page. I have the following link_to:
<%= link_to('NEWS', posts_path(subdomain:"news"),:class=>'navtext',:style=>"color:#bfbebc!important;") %>
My routes:
resources :posts , constraints: { subdomain: 'news' }
When I am at http://news.lvh.me:3000/posts (the actual page the link_to refers to) the link works, however when I place in on any other page (for example, the root_path) it does not work. I get the No route matches [GET] "/posts" error which I think means that the subdomain is not passed in my link_to. Any ideas on how I can do this? Thanks.
Try posts_url, instead of posts_path.
It works fine on my project even without (subdomain:"news"). Just use posts_url directly.
If nothing else works you could just skip the helper altogether:
<%= link_to('NEWS', subdomain_link,:class=>'navtext',:style=>"color:#bfbebc!important;") %>
To make it work in both development and production you could write a helper method in application_helper.rb:
def subdomain_link
Rails.env.development? ? '//whatever.localhost:3000' : '//news.whatever.com'
end
To go even further, you could configure subdomain_link to accept a string as an argument and interpolate that into the domain string. Another approach would be to configure a config variable in config/environments/development.rb and config/environments/production.rb.

How to get the route by helper name?

I have the following set of routes which point to the same view:
get 'mypath', to: 'home#mypath', as: 'mypath'
get 'mypath-v2', to: 'home#mypath', as: 'mypath_v2'
get 'mypath-v3', to: 'home#mypath', as: 'mypath_v3'
How can I check if I am using one route or the other inside the view?
For example if I want to get mypath-v2 or mypath_v2, how would I do it?
Well, as for me it is better to do such things using params. You can define your routes like this:
get "mypath/:version", :as => "mypath"
In this case you will be able to use params[:version] to clarify current path.
You would call it by appending _path or _url
For instance here is an example in a link:
<%= link_to 'Link to mypath-v2 page', mypath_v2_path %>
OR
<%= link_to 'Link to mypath-v2 page', mypath_v2_url %>
To compare them you can look at the request object. When you call request.path or request.fullpath it will bring in the actual address request path of the link.
So...
<%= if request.path.eql?('mypath-v2') ? "Used It" : "Other Route" %>

Move one page to another in Ruby on rails?

In my rails project there is a controller and view named "Welcome/index" where index is an action and another one named "home/page" . As i set root"home#page" as my root page. Now i want to transfer from "page.html.erb" into "index.html.erb" . How can i do that. And the code i written is below.Do i have to enter some thing in my controller class. please suggest.
these are the links that i tried. (How to create an anchor and redirect to this specific anchor in Ruby on Rails)
<a rel="nofollow" href="index.html.erb">Transfer to index</a>
You are not supposed to link to .html.erb files, you should link to the methods (not exactly the name of the method, but the name of the route) of a controller.
I strongly encourage you to review the ruby on rails MVC principles. You can read about routing and linking aswell.
Responding to your question, check out the command "rake routes". It will list the defined routes of your app and helps you to use them.
Try to replace your code by this:
<%= link_to 'welcome', welcome_path %>
<%= link_to "Link", controller:"controllername" %>
is the code you should use
You need to make sure a named route is defined for welcome/index and then can use the Rails helper link_to to automatically build your link for you in the view.
In routes.rb:
match '/welcome' => 'welcome#index', :as => :welcome
In your page.html.erb view:
<%= link_to 'Go to Welcome Page', welcome_path %>
If you want go for index action of Welcome controller then you can use:
<%= link_to "Transfer to index", welcome_path %>
Check the rake routes for path.
Plese refer link

Rails routing: id doesn't work

I have the following routing rule:
match ':controller/:action/:id'
However when I use
<%= link_to "Link", :action => "some_action", :id => 10 %>
Instead of redirecting to "some_action/10" it redirects to "some_action?id=10"
How can I fix that?
P.S. I know I should be using the path methods, but is there a way to avoid them?
As Matchu said, it should work. Try making your catch-all route the first one in routes.rb. If it works then, you'll know there's another route being evaluated first.
If this doesn't work, post your complete routes.rb file.

How to get link_to in Rails output an SEO friendly url?

My link_to tag is:
<%= link_to("My test title",{:controller=>"search", :action=>"for-sale", :id=> listing.id, :title => listing.title, :search_term => search_term}) %>
and produces this ugly URL:
http://mysite.com/search/for-sale/12345?title=premium+ad+%2B+photo+%5Btest%5D
How can I get link_to to generate:
http://mysite.com/search/for-sale/listing-title/search-term/12345
Been trying this a few different ways and cannot find much online, really appreciate any help!
Tahe a look at this
add this in your config/routes.rb
map.connect ':controller/:action/:title/search_item/:id', :controller=>'search', :action=>'for_sale'
restart your server and check.
Hope that helps :)
You need to change the URL structure in routes.rb to match what you want the URL to look like, and parse the parameters accordingly in your controller method's args.

Resources