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"
Related
I cannot find out why when that following route:
get "/instagram", to: "posts#instagram", as: "instagram"
is requested by this code:
<%= link_to "Instagram", instagram_path %>
and perfectly render:
posts/instagram.html.erb
via the PostsController and following action:
def instagram
end
it does render posts/instagram.html.erb but it also try to get to this weird URL GET "/..." that I did not set up in my routes.rb or any where in the rails app.
Everything works fine on the client side and if not looking at the logs I may never have noticed this, but it bother me that this error is triggered: ActionController::RoutingError (No route matches [GET] "/...") as it does not make any sense to me.
It is possible that any ajax function is called when you click on the link, first you should check jquery is not causing the issue.
Second you should check by disabling turbolink, for particular link you can data-no-turbolink to disable turbolink.
Also try in different browser.
I have a rails link that's using the POST method:
link_to "Run", forecast_run_path(#forecast), method: :post
to call a defined post route:
resources :forecasts do
post :run
end
The route appears in rake routes output as expected:
forecast_run POST /forecasts/:forecast_id/run(.:format) forecasts#run
And when I inspect the page, the expected post link appears in the page:
Run Forecast
In dev it works fine, and was good in production until sometime just a few days ago - I can't find any change that seems like it should have broken it. But when I click the link, I get the error ActionController::RoutingError (No route matches [GET] "/forecasts/20/run")
I agree that no GET route matches, but it should be using POST.
This is Ruby 2.1.5, Rails 4.2.0
To my own embarrassment and for the benefit of anyone else who has the same issue:
As I switched among different clients, I didn't notice that on one of them NoScript in Firefox was blocking javascript on that site. Doh!
When you post a run, the form will direct you to the show action where it displays the newly posted run, in your case you don't have a show action for run, so implement a show action and should work fine.
Trying to get the image full URL inside a view rendered by a mailer.
Is there a proper way in Rails 3+ to get the full url generated by Sprockets?
I know of the request object hack, but since the mailer is invoked inside a rake task,
the request data is not available, obviously.
I'm running Rails 4 beta1 (edge)
In Rails 4 you need to have the following in your production.rb
config.action_controller.asset_host = 'yourdomain.com'
config.action_mailer.asset_host = 'http://yourdomain.com'
Having one with the protocol (http) and one without is deliberate. Maybe it will be different before Rails 4 comes out of beta, but if you have the protocol in action_controller.asset_host then you get urls like http://http://yourdomain.com, and if you don't have the protocol in action_mailer.asset_host it is not recognised as a valid URL because it doesn't validate correctly against an internal Rails Regex.
Then you can use the following in your mailer template:
<%= image_url('mail/awesome.gif', only_path: false) %>
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? :)
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.