ActionMailer doesn't recognise route - ruby-on-rails

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

Related

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

Rails named route and parameters

I want to send to my controller some data, and then in variables:
params[:oem_number]
params[:id]
and see there some data, but how can i send to method data? How to write this route?
Now i have such route:
match '/articles/by_oem/:id&:oem_number' => 'articles#articles_by_oem', :as => :articles_by_oem
And try to create link:
= link_to "аналоги", :articles_by_oem(:id => no.article_nr, :oem_number => no.oem)
But i get SyntaxError errors...
So how to solve my problem? Please don't send me to rails doc's...
Just how to create link, that will send this two params, and also before edit route...
first, you don't need that list of parameters on a route.
You can leave just this in your routers.rb:
/articles/by_oem/:id
And, then, what are you trying to do here?
:articles_by_oem(:id => no.article_nr, :oem_number => no.oem)
:articles_by_oem is a symbol, not a function. Use articles_by_oem_path method instead:
= link_to "аналоги", articles_by_oem_path(:id => no.article_nr, :oem_number => no.oem)
= link_to "name", articles_by_oem_path(no.article_nr, no.oem)

Rails implement an API using Routes pointing to existing controller actions

I have a Rails app that does everything I need it to do via a HTML interface, now I'd like to bolt on an API providing access to bits of the functionality.
How would I do this selective forwarding of some API controller actions to another controller's actions using the Routes.rb?
I have tried the following:
My regular controller routes work fine using:
match 'stuff' => 'stuff#index'
get 'stuff/index'
get 'stuff/some_get_action'
post 'stuff/some_post_action'
But then when I try for my API:
match 'api' => 'api#index'
match 'api/some_get_action' => 'stuff#some_get_action', :via => :get
match 'api/some_post_action' => 'stuff#some_post_action', :via => :post
or...
match 'api' => 'api#index'
get 'api/some_get_action', :to => 'stuff#some_get_action'
post 'api/some_post_action', :to => 'stuff#some_post_action'
I get an error. When I navigate to /api/index to server a HTML page that contains forms for testing the API URLs the url_for raises a 'Routing error' exception saying 'No route matches...'.
You may want to include ':as => ' and define your route names that you may be using as your link paths.
get 'api/some_get_action' => 'stuff#some_get_action', :as => 'some_get_action'
and the link path in your index file will have 'some_get_action_path'. Not sure that 'match' or 'get' automatically resolves to a path name, which by setting ':as' it definitely will.
I like your idea for setting up this page for testing. I'm always doing it in the console, which is surely more difficult than simply clicking a link. Your links probably need to infer that they are :json requests, not :http.

How can I include unix path information in a rails link_to

I have an application which needs to handle path information (it will be working with files on the server). Here is how I'm trying to do the route.
match "viewfile/file=:vFile" => "home#viewfile"
and here is how I'm trying to link to the file
link_to("file",
{ :controller => "home", :action => "viewfile", :vFile => "/this/is/a/test" })
This, however, throws errors and does not work.
How can I do this?
link_to("file", { ... url_encode("/this/is/a/test/") })
Is more likely to work.
the problem is the routing does not know which route to match because multiple routes could match your controller/action/vFile combination, so name the route like:
match "viewfile/file=:vFile" => "home#viewfile", :as => 'viewfile'
now you can use the viewfile_path helper
link_to 'file', viewfile_path("/this/is/a/test")
PS: I don't know if it works with the equal (=) sign in the url, what definitely works is:
match "viewfile/file/:vfile" => "home#viewfile", :as => 'viewfile'
link_to 'file', viewfile_path("/this/is/a/test")
but give it a try...

how can I add a new action to a controller?

I used the following in routes to add a new action to my Email controller:
map.resources :emails, :member => { :newfwd => :put}
The expected result was that newfwd_email_path(:id => 1) would generate the following urL: emails/1/newfwd
It does. But I get an error, it treats '1' as an action and 'newfwd' as an id. I want '1' to be interpreted as the id for emails, upon which the newfwd action acts.
I'm not sure what I'm doing wrong. (Note: I am using Rails 2.3.8)
Try
link_to newfwd_email_path(1), :method => :put
:id => 1 is as good as 1 ;)
you do not need to pass a hash to the newfwd_email_path method. Try
newfwd_email_path(1)
EDIT: you also need to use :method => :put to ensure that the PUT verb is used when request is received on the server and routing comes into effect.

Resources