Rails app internal links not working, but direct urls are - ruby-on-rails

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

Related

Rails generate url for match named route

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

Rails: Generated URL has double backslash

In my Rails project, in my index view, I have a link
<%= link_to 'Show all posts', show_all_path %>
In routes.rb, I have a route:
match "show_all" => "Posts#show_all"
When I click on that link, it goes from
http://<domain name>/my_rails_project
to
http://<domain name>/my_rails_project//show_all
It works fine, but I'm wondering why there are two backslashes in front of show_all instead of one. And can I make it so that only one backslash appear?
I think your route needs more information:
`match "/:project_name/show_all" => "posts#show_all", :as => "show_all"
In your view:
link_to 'Show all posts', show_all_path(#project.name)
This assumes you have a #project variable in the page you're viewing.
try use get
get "show_all", :to => 'posts#show_all', as: 'show_all'

ActionMailer doesn't recognise route

I have a route called "settings_redirect", which I've defined as follows:
routes.rb
match "/settings/redirect" => "settings#redirect", :via => "get"
I want to link to this route in an email template:
mymail.html.erb
<%= link_to "Manage Settings", settings_redirect_url %>
Yet, when I get ActionMailer to send the email, I get the error
{undefined local variable or method `settings_redirect_url' for #<#:0x007ffa1153de38>
The same link works completely fine in any regular view, just not when I try to send it in an email. All other links in the same template don't cause any trouble either.
Any ideas as to what could cause the error?
You can use this form:
get "settings/redirect" => "settings#redirect", :as => :settings_redirect
match "/settings_redirect" => "settings#redirect", :via => "get"
Check out this documentation, http://guides.rubyonrails.org/action_mailer_basics.html#generating-urls-in-action-mailer-views

Rails 3.1 routing all URL's point to one action

I am trying to have any URL that starts with /capture point to one action in my controller. I have the following in my routes.rb file.
match '/capture' => 'requests#index', via: :get, as: :requests
match '/capture/*other' => 'requests#index', via: :get
This works for me. The /capture and /capture/foo (foo can be replaced with anything) URL's all point to the requests#index action.
Is there are more concise way to code this?
you mean like this?
match "/capture*tail" => 'requests#index'
so everything after capture will be available in params[:tail]

Change paths for link_to missing out the model name

I've managed to get my routes set up (with help from these questions Routing without the model name and Permalinks with Ruby on Rails (dynamic routes)) so that articles can be accessed via my-domain/permalink rather than my-domain/articles/permalink or, the original my-domain/articles/id
Now I would like to make the paths that the link_to helper gives point to /permalink rather than /articles/permalink. I've looked at http://guides.rubyonrails.org/routing.html#overriding-the-named-helpers and see how I could redirect to eg. /images/permalink, but can't see how to have no model name present.
Can anyone suggest a way to do this?
Using :as on a match ... line in your routes file will make this work (it operates a little differently from using :as on a resources ... line):
match '/:id' => 'articles#show', :as => "article_permalink", :via => 'get'
Then you can do:
link_to "Show", article_permalink_path(article)
See Naming Routes in the Rails Guides

Resources