Rails generate url for match named route - ruby-on-rails

How can i generate url for named match route:
match '/confirm_email' => 'landing_controller#index', :as => 'frontend_confirm_url', via: [:get]
using something like this:
<%= link_to 'frontend_confirm_url', frontend_confirm_url_url %>
Rails.application.routes.frontend_confirm_url_url
doesn't work, maybe because i'm using Spree as an engine, and trying to resolve paths from it's mailer?

OK, I figured out, you need to use:
<%= link_to 'frontend_confirm_url', main_app.frontend_confirm_url_url %>

Related

Better routes in rails with params

I've been working on a movie application in Rails. I have a controller/view for the actor. When passing params to the actors controller i want the URL to be pretty. Now it looks like this: http://localhost:3000/actors/show?actor=Hayley+Atwell and i want it to look like /actors/show/Hayley+Atwell or /actors/Hayley+Atwell.
How do i do this? My link in the movies view is:
<%= link_to a.name, {:controller => 'actors', :action => 'show', :actor => a.name}, :class => "label label-default" %>
My routes.rb is now like this:
get 'actors/show'
I recommend you use friendly_id gem. It perfectly satisfies your needs!
https://github.com/norman/friendly_id
You can use the following in your ./config/route.rb file:
get '/actors/:actor', to: 'actors#show'
I'll give a series of refactoring advice as follow:
Try to make use of the actor's id instead of the name
Change the route to: get '/actors/show/:id', to: 'actors#show'
Then, you can now change the link in your view to something like:
<%= link_to a.name, {:controller => 'actors', :action => 'show', :id => a.id}, :class => "label label-default" %>
Note: the : part in the :id of your route means that this could be interchanged to anything, and whatever comes in that place will be interpreted as the id. So, in /actors/show/7, the id of the actor is 7, you can now find the actor with this id in your controller action, and do anything you want with it.
If I'm right than you try to create RESTful routes anyway.
So I would recommend you to use the Rails' resources method in your routes:
Rails.application.routes.draw do
resources :actors # If you want only the :show action than add:
# , only: [:show]
end
This automatically creates the proper routes for you.
To get the names into the URL you should use the friendly_id gem. It has a great community and is very well tested.

Rails app internal links not working, but direct urls are

I have an app running on Ror4 but don't seem to get my links working. Whereever I have a
<%= link_to "Get Started", get_started_path %>
or similar in my code, clicking it doesn't do anything. but if I type the address localhost:3000/get-started it correctly brings me to my page
Here's my routes.rb
match '/get-started' => 'static#get-started', via: 'get'
root 'static#index'
match '/thank-you' => 'static#thank-you', via: 'get'
match '/how-it-works' => 'static#how-it-works', via: 'get'
match '/get-started' => 'static#get-started', via: 'get'
match '/get-newsletter' => 'static#newsletter-signup', via: 'get'
match '/privacy-policy' => 'static#privacy-policy', via: 'get'
match '/terms-and-conditions' => 'static#terms-and-conditions', via: 'get'
The issue is the same for all of the links as well as links generated by e.g. devise. Which leads me to believe that it's not a controller issue. A click on
<%= link_to "Logout", destroy_user_session_path, method: :delete %>
also doesn't work. This befaviour is the same in my local dev environment and on a server.
Clicks on direct links e.g. http://www.google.com do work and so does:
<%= f.submit "Next" %>
Actually no idea what the problem is. The dev console doesn't throw errors and the rails console doesn't either.
Any help appreciated. Thanks
As awendt pointed out in his comment it turned out to be a javascript issue. When I added a render :layout => false to a view the links did work, and I could eliminate the javascript by including them one by one.
try to set in routes.rb something like:
get '/get-started', to: 'static#get-started', as: 'get_started'
then do bundle exec rake routes in order to see right url variable (should be get_started_path)
in rails 4 it's better idea to use get or post rather then match in routes.rb

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.

How to make link_to and custom URL work together on Rails

I have a custom vanity URL setup, ala GitHub:
http://foo.com/:user/:stuff
I have routes to handle this as well:
match '/:user/:stuff', to: 'stuffs#show'
How can I make link_to work with this route for stuff?
link_to #stuff.name, #stuff
You can make it through a route alias:
match '/:user/:stuff' => 'stuffs#show', :as => 'users_stuff'
And in your view call it normally, passing the two parameters:
<%= link_to 'Show', users_stuff_path(current_user,3) %>

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