How to get link_to in Rails output an SEO friendly url? - ruby-on-rails

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.

Related

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" %>

basic routing - how to create a link_to path from match

Ok, say you have this:
match "tutor_appointments/new_appt" => "tutor_appointments#new_appt"
How do I create a link_to path from it?
Would it be something like this: (it doesn't work, btw)
<%= link_to "New Appointments", controller:tutor_appointments, method:new_appt %>
I'm always confused on routing stuff when it comes to figuring out how to do link_to link.
I do understand that tutor_appointments is the controller and new_appt is the method.
Ideally, you would name the route:
http://guides.rubyonrails.org/routing.html#naming-routes
And then you can refer to the route by that name.
eg. if you had:
match "tutor_appointments/new_appt" => "tutor_appointments#new_appt", as: 'new_appointment'
Then you could do:
link_to 'New Appointments', new_appointment_path
However, in this case it sounds like what you actually want is resource routing:
http://guides.rubyonrails.org/routing.html#resources-on-the-web
And you want a 'new' action for your 'tutor_appointments' resource.

What Ruby construct does the link_to method in Rails use?

Say I have this route:
match '/somepage' => "home#somepage"
I can then do this on a different page to link to "somepage":
<%= link_to "Some Page", somepage_path %>
I'm new to both Ruby and Rails, and I'm struggling to understand how exactly the "somepage_path" part works. It's not a string, it's not a symbol, is it a method call? If so, where does that method exist? Seems like I'm missing something obvious...
On initialization all paths are generated according to the routes.rb definitions.
If you want to customize the routes names you can use
match '/somepage' => "home#somepage", :as => "foobar"
Later used as
<%= link_to "Some Page", foobar_path %>
Hope that clears some stuff up about custom routing :)
Link magic is all handled by ActionDispatch::URL::UrlFor, see ActionDispatch::URL::UrlFor

link_to a form in ruby

I want to use the link to function to point to a form.
i.e <%= link_to 'Reports', '/reports/index.html.erb' %>
But this gives me a error saying no route matches '/reports/index.html.erb.
Thanks,
Ramya.
Rails doesn't like having the document format in the URL unless it's necessary (like when one action can handle multiple request formats). If you have reports/index.html.erb, the route to it would look like one of these:
match 'reports' => 'reports#index' #=> 'yourdomain.com/reports'
match 'reports/index' => 'reports#index' #=> 'yourdomain.com/reports/index
Then your link would be:
<%= link_to 'Reports', 'reports' %>
or
<%= link_to 'Reports', 'reports/index' %>
If you really wanted to have the .html, you could probably do it like this:
match 'reports/index.html' => 'reports#index' #=> 'yourdomain.com/reports/index.html
or
match 'reports/index.:format' => 'reports#index' #=> 'yourdomain.com/reports/index.html
But the .html is meaningless in the first case and unnecessary in the second. I don't recommend doing it this way, as it's not standard Rails practice.
I highly recommend you read this tutorial on Routing, at least the first few sections, before you move forward. It's an absolutely essential part of Rails, and if you don't understand how it works, you will never be a productive Rails programmer.
The link should point to the form from the server's point of view. Try <%= link_to 'Reports', '/reports/index.html' %> (without the '.erb')
Make sure that your routes really define that url. I guess it may be '/reports/' instead of '/reports/index.html', but YMMV.
Consult the output of command rake routes to see what routes are defined.

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.

Resources