I have currently set up a mailer which is notifying users that their invoice is ready.
The mailer is working correctly except the URL is adding a . instead of a slash in the url. For example - I am receiving the following which isn't directing to the intended link:
www.mydomain.com/invoices.14
Instead I would like it to say:
www.mydomain.com/invoices/14
My Set up is as follows:
invoice_mailer.rb
default_url_options[:host] = 'mydomain.com'
invoice_mailer/invoice_ready.html.erb
<p>Click the following link <strong><%= link_to #invoice.inv_num, invoices_url(#invoice) %></strong></p>
I'm not sure why but the redirection keeps adding the . in the URL. Any help would be appreciated.
Use a singular version of url helper:
<p>Click the following link <strong><%= link_to #invoice.inv_num, invoice_url(#invoice) %></strong></p>
Here is an example how it's works for instance resources :invoices
invoices_path returns /invoices # that's why you have invoices.14
new_invoice_path returns /invoices/new
edit_invoice_path(:id) returns /invoices/:id/edit (for instance,
edit_invoice_path(10) returns /invoices/10/edit)
invoice_path(:id) returns /invoices/:id (for instance, invoice_path(10)
returns/invoices/10)
Each of these helpers has a corresponding _url helper (such as
invoices_url) which returns the same path prefixed with the current
host, port and path prefix.
Read the documentation CRUD, Verbs, and Actions
Related
I want users to be able to edit a model through a different than the default URL /merchants/:id/edit(.:format), which will be using a token. This token is created randomly and stored in the database.
The link I want to create will be similar to this merchants/token-124512/edit.
Now I want to be able to send this link to users via e-mail. The default link with the id is link_to "link", edit_merchant_path(#merchant, :only_path => false).
How would the one with token be like? Also, how can I declare this one in the routes.rb?
Solution
First of all I would suggest to use _url instead of _path in your mailer because you want to resolve full path.
Try with:
link_to "link", edit_merchant_url(id: #merchant.token)
Why _url?
using _path will get you <a href=merchants/token-124512/edit'>link</a>, but in mail you want to know host as well so you should get this:
<a href='hostname.com/merchants/token-124512/edit'>link</a>
If you are using rails 4, you can use the new concept called 'param' in your routes, which will change the default route.
You can pass any field instead of your id.
# config/routes.rb
resources :merchants, param: :token_field
# app/controllers/merchants_controller.rb
#merchant = Merchant.find_by(token_field: params[:token_field])
Example
resources :merchants, param: :your_fleld
merchants GET /merchants(.:format) merchants#index
POST /merchants(.:format) merchants#create
new_merchants GET /merchants/new(.:format) merchants#new
edit_merchants GET /merchants/:your_field/edit(.:format) merchants#edit
Merchant.find_by(your_field: params[:your_field])
You can find the documentation here
Lets say I have a controler test.
In test I define 3 actions:
def zah
end
def zeh
end
def zih
end
I have the views:
zah.html.erb
zeh.html.erb
zih.html.erb
and under routes.rb I have:
get 'test/zah'
get 'test/zeh'
get 'test/zih'
If I write under zah.html.erb, using code automaticaly created from rubymine IDE, this:
<%= link_to test_zeh_path%>
I will get my page source code with this:
http://localhost:3000/test/zeh
which makes the redirection from zah be itself.
running rake routes returns this:
Prefix Verb URI Pattern Controller#Action
test_zah GET /test/zah(.:format) test#zah
test_zeh GET /test/zeh(.:format) test#zeh
test_zih GET /test/zih(.:format) test#zih
Can anyone explain to me why is the link going to itself (from zah to zah) instead of another page(from zah to zeh)?
Edit:
I have found out that adding a name to a link makes the generated code works right:
<%= link_to 'zeh', test_zeh_path%>
I have seen the first usage (link_to test_zeh_path) here at 22:45.
Ruby on rails api does says that if nil name is passed then "the value of the link itself will become the name.".
As for a mistake of myself I was wondering why Dave Jones was able to create a link without a name, but he wasnt and that can be seen on his source code.
Because you have written the url in the display part.
You can simply do
<%= link_to 'Goto Zeh', test_zeh_path %>
and you will be good to go.
I have a Link resource (Link as in url's). I have a method in my Links controller to determine whether or not a link that a user enters has the "http://" prefix, and if not, to append that prefix to the URL. Although I defined the method in my Links controller, I am getting an undefined method error.
Here is the relevant portion of my links controller:
helper_method :link_formatter
def link_formatter(url)
prefix = "http://"
url.include?(prefix)? url : prefix + url
end
Here is my links view:
<%= link_to link.description, link_formatter(link.url), :target => '_blank' %>
The error:
undefined method `link_formatter' for #<#<Class:0x007fbd51e2ea08>:0x007fbd5250cfa0>
The link_formatter is a generic function that can be used by any view in your Rails application. To make it accessible by all your views add it to the ApplicationHelper as #Damien Roche said. The application helper is located in app/helpers/application_helper.rb. You can also generate a new helper by running rails generate helper HELPER_NAME on the command line.
I'm new to Rails and had a doubt regarding the link_to method. The second argument should be the url of the link, which one can generate using the url helper methods. However, one may need to pass :id as an argument of the helper method, which can be done by passing an object (which has :id as one of its attributes).
Well, in one case I did not pass the object to the method (in one of the views). However, the url was still able to obtain the correct :id (presumably using an instance variable defined earlier).
How did Rails choose a value for :id when I didn't even pass in any object?
Thanks a lot!
Edit
Here's the relevant code:
link_to 'Find Movies With Same Director', same_dir_path
Here, I am on a "show" page with url /movies/1. The same_dir_path is the helper method for the URL /movies/same_dir/:id where :id would be that of the passed object and movie#same_dir is the controller#action. Note I did not pass any object to the helper method link_to and yet, it takes the :id from the previous url ('1' in this case). The URL isn't even relative to the previous one (the path is different).
This is the controller method (same_dir):
def same_dir
#movies = Movie.find(params[:id])
if (#movies.director.nil? || #movies.director == '')
flash.keep
redirect_to movies_path
flash[:warning]="'#{#movies.title}' has no director info"
return
end
#otherMovies = Movie.find_all_by_director(#movies.director)
end
This is the routes.rb code:
match 'movies/same_dir/:id'=> 'movies#same_dir', :as => :same_dir
resources :movies
After reading your updated question I can provide you with a better answer:
Rails controllers can have default url options via the url_options method. (Doesn't seem to be a very documented feature. (here and here))
By default this method returns the parameters from the current request and that is where the id is coming from.
You can override it, too:
def url_options
{ my_parameter: my_value }.merge(super)
end
Original answer (might still be useful):
What you are witnessing is most likely a browser feature. For example this code:
link_to "Show", ""
generates this HTML code:
Show
If you click that link in a browser it navigates to the empty url relative to the current url, which is in fact equal to the current url.
Another example:
link_to "Publish", :publish
generates:
Publish
Now if your current url is http://localhost/articles/1/edit that link will take you to http://localhost/articles/1/publish. (Notice that the final url contains the model ID even though you are not having it in the HTML source)
In both cases your current model ID is preserved by the browser because you are using relative urls.
This behaviour might give you the illusion of some magical model ID detection, especially because browsers preview the final (=absolute) url when hovering over the link.
Have a look at the source, I'll bet your generated links do not contain any model IDs.
I'd like to modify the behavior of the rails route helper *_url for a single route/page.
Here's what I'm try to do:
User visits:
http://test1.myapp.com/account
All the *_url routing helpers resolve to http://test1.myapp.com/ as normal.
But, then if the user goes to https://myapp.heroku.com/account/billing?id=test1
I'd like all the *_url routing helpers on that page to resolve to: http://test1.myapp.com/
instead of http://myapp.heroku.com/
So, is it possible to change the domain bit for all the *_url helper calls for a specific page?
For those interested, I'm trying to use heroku's piggyback ssl method for my app for just a secured billing page.
You can actually just modify the links that point to the billing area:
<%= link_to "Billing", my_helper_url(test1, :host => "myapp.heroku.com", :protocol => "https") %>