Inherited app with strange routing error, No route matches {} - ruby-on-rails

Whenever one of the mailers fires off if the body contains something like link_to root_url it gives this incredibly unhelpful error:
ActionView::Template::Error: No route matches {}
The trace either is useless or points to that link_to method. Since it's also very unsearchable here I am asking on stack overflow.
UPDATE 1: To help, I've posted the terrifying routes.rb file: https://gist.github.com/2955610
UPDATE 2: In the console, app.root_url provides the correct return.
UPDATE 3: link_to "whatever", root_url works just fine. So strange!
UPDATE 4: It works fine in a regular rails view.
UPDATE 5: FIXED The image_tag now isn't providing a host in the email, outputting: http://assets/...png. Fffff.
UPDATE 6: I've narrowed it down to that I get the same error with (in console) app.url_for. I think it may be related.

So the real answer is that link_to works differently in ActionMailer than in ActionController. The difference is that apparently ActionController has an option called :script_name, and this makes all sorts of assumptions for you.
Now while I don't agree with that difference there's not much you can do about it, so here's what I ended up doing:
link_to root_url, root_url
Now, why would I use both? Well two reasons:
I wanted the url as the anchor text and the href.
If the URL changes, I don't want to have to update all those strings.

The problem with mailers is that maybe you miss "host" option? :)

Related

link_to with single arg broken in mailer

Just updated a Rails 3.2 application to Rails 4.2 and started getting a funny error. Anytime I try to call link_to with a single argument inside my mailer template, I get an error. However, I can make the same call just fine within one of my regular views.
Inside of my mailer view, I try to call it like this:
# user_mailer/notify.html.haml
...
= link_to "https://example.com"
But when the email gets processed by my job handler, it reports the error ActionView::Template::Error: No route matches {:action=>"index"}. (Interestingly enough, this is what you get when you try to call url_for without any parameters).
However, on my homepage I have no issue using the same thing:
# home/index.html.haml
...
= link_to "https://example.com"
Outputs:
https://example.com
As far as I can tell looking at the docs, nothing changed with link_to between Rails 3.2 and 4.2 so I'm confused why this would stop working... Also confused why it works in one place, but not the other.
link_to with just one parameter doesn't do what you think it does. If you look at the generated link in your view it just points to the site you're currently on. You have to provide two parameters, the body and the url.
= link_to "https://example.com"
# https://example.com
You will need this however
= link_to "https://example.com", "https://example.com"

ActiveResource error with "wrong constant name ENV.xxx"

I'm consuming a REST endpoint using ActiveResource which has Keys called among others ENV.OM_PRODUCER, ENV.UMS_PRODUCER.
These appear to be causing an issue with my view, I'm getting errors such as:
NameError in AppsController#index
wrong constant name ENV.UMSProducer
There is nothing else in the logs to help me, any suggestions.
Update: I was far from clear earlier.
I am not doing anything with the data yet, in my controller I have:
#apps = App.all
and in the view I have:
<%= #apps.inspect %>
It seems like ActiveResource is interpreting those ENV. keys and its causing an issue. It feels like I need them to be escaped but I don't know how.
Thanks to this post I determined it was due to the full stop in the key rather than the key name. Putting the code suggested in the post into a .rb file in initializers fixed the problem.

Rails path nomenclature not working

I have a Fixer model, but, unlike all the other models in my app, its routing isn't working right, even though there's a resource line for it in the routes file.
The problem is, when I try to link to the basic show path any number of ways, like
<%= link_to "Fixer", fixer_path(#fixer) %>
or
<%= link_to "Fixer", #fixer %>
or
<%= link_to "Fixer", fixer_path(#fixer.id) %> # I got desperate
it links to /fixers.[:id] (not a real page) instead of /fixers/[:id]. No idea what's happening, cause my resources line is there, and show is a basic resources action, and all the other similarly resourced models seem to be working just fine.
Any ideas? (I can put more code up if necessary. Just not sure what would be relevant).
EDIT -- The Fixers output in my rake routes:
fixers GET /fixers(.:format) fixers#index
POST /fixers(.:format) fixers#create
new_fixer GET /fixers/new(.:format) fixers#new
edit_fixer GET /fixers/:id/edit(.:format) fixers#edit
GET /fixers/:id(.:format) fixers#show
PUT /fixers/:id(.:format) fixers#update
DELETE /fixers/:id(.:format) fixers#destroy
Whoa. Just noticed after posting this that the 5th line is missing the "fixer" before the show action line that all my other models have. Why would that have happened? How do I fix it?
EDIT -- I figured it out! Really dumb issue. For some reason, back when I was learning how to do all this, I both included a resources line and added this line above it:
match '/fixer', to: 'fixers#new'
When I took that line out (cause it was redundant), the problem went away. I guess I was messing with the Rails routing automagic. They really do make those defaults the best option.
Hey I have had this same problem before.. and unfortunately have resorted to doing the following: link_to "link text", "/fixers/#{#fixer.id}" I would love to know the actual correct answer for this though.

What do I need to do to make this link work in Rails

I am trying to learn ruby and learn how to handle a while request round trip.
On my index.html.erb page I added this line:
<%= link_to "Alex Link", test_path(#test) %>
but I got an error:
undefined method `test_path' for #<#<Class:0x4064e80>:0x3c0b5c8>
As I understand it, I need to add a record to routes.rb, and then a controller. Correct? How do I do that?
I read the explanation for this in the Rails Guides, but just finding it a bit confusing doing it the first time.
For your purposes (learning) resources tests if fine.
It also gives you other routes for free, see RESTful routes.

Using Ruby on Rails link_to to link to controller action

I'd just started toying around with Ruby on Rails and had come across an issue with linking to another action in a controller from a particular view. I am almost certain it's an issue (or lack of code) in my routes.rb file, but I think I'm misunderstanding exactly how this file works & what I have to do. I've got a solution but pretty sure it's not the "best way" to do it.
I have one controller called home with two actions, index (which is the default) and newbill. Inside index.html.erb I have:
<h1>Home View</h1>
<%= link_to "new", :controller => "home", :action => "newbill" %>
However I was getting a routing error:
No route matches {:controller=>"home", :action=>"newbill"}
Doing rake routes gives me the following:
root / {:controller=>"home", :action=>"index"}
I then (following some Googling) added this code to routes.rb
match 'home/newbill' => 'home#newbill', :as => :newbill
And then in my index.html.erb I've got this:
<%= link_to "Name", newbill_path %>
And now this works as expected. My questions however are:
Why does this work? What exactly is going on behind the scenes?
Surely this is not the best way to do it? Adding another match 'home/newbill'... for every controller / action I want to link to seems a rubbish way of doing things.
I really like Ruby, but struggling a bit with this aspect of Rails...routing in general is messing up my head a bit I think!
Any help is much appreciated :D
Thanks,
Jack
I guess the first time your code didn't work because your home controller is defined as a resource.
If you define a controller as a resource in routes.rb file it will support only 7 standard methods (according to REST architecture):
index
new
create
show
edit
update
destroy
If you need any more custom routes you should add them manually, say in your case 'newbill', may go as:
resources :home do
collection do
get :newbill
end
end
But as per my understanding, your newbill method should go to bills controllers new, method not in the home controller.
You are right, Rails routes are little bit confusing (at least for me), but once you understand you can do lots of cool stuff.
Read here for the Rails official routes documentation:
http://guides.rubyonrails.org/routing.html.
You should check out the Rails Routing guide. A read through will help you understand what is going on behind the scenes.
This works becuase rails filters every request through the router looking for a match. This enables you to define custom routes such as domain.com/post when the path is actually blog#post. Prior to rails 3, a catch-all route was the last route in the routes file. This allowed you to define a controller and action and it would just work. I'm on my iPad and not near any projects, so I can't verify it, but I think that route is still there in rails 3.1, it just needs to be umcommented.

Resources