I am looking into generating urls which look like https://mywebsite.com/user/check_email_confirmation/46cee41bc2044104aff8cf7746687ea8
for the famous "please click this link to confirm your email".
So I have a route which I intend to use for this in my routes.rb:
resources :user do
collection do
get 'check_email_confirmation/:generated_url', :to => 'user#check_email_confirmation'
end
end
Now, my trouble is to generate the full address above. I cah generate the SHA (46cee41bc2044104aff8cf7746687ea8) part, but how do I write the "https://mywebsite.com/user/check_email_confirmation/" part in my view?
Added view's code:
<p>Please click <%= url_for(:action => 'check_email_confirmation', :controller => 'user', :only_path => false, :id => #confirmation_url) %> to confirm your e-mail address.</p>
Replace id: with generated_url:.
You could also simplify the url_foring with naming the route by as: in routes
get 'check_email_confirmation/:generated_url', to: 'user#check_email_confirmation', as: :confirmation
More on this at http://viget.com/extend/rails-named-routes-path-vs-url
get "check_email_confirmation/:id" => "users#check_email_confirmation", :as => "check_email_confirmation"
Related
I am trying to generate a url in an actionmailer template. An example if the url I want to generate is
http://0.0.0.0:3000/users/confirm/lNbQxzFukYtEEw2RMCA
Where the last segment is a hash to identify the user
However when I use this
<%= url_for(:controller => 'users', :action => 'confirm', :id => #user.confirmhash, :only_path => false) %>
It generates this
http://0.0.0.0:3000/assets?action=confirm&controller=users&id=ZOR3dNMls8533T8hJUfCJw
How can I get it to correctly format? I have no idea where 'assets' is coming from.
Is there an easier way to use named routes that I am missing?
I've found the answer. As I'm still learning I've missed the option to create a named route. So this this the path I've taken.
In config/routes.rb
match 'user/confirm/:id' => 'users#confirm', :as => :confirm_account
Then in my action mailer template I've used
<%= link_to "Confirm your account", confirm_account_url(#user.confirmhash) %>
Which passes the :id into the controller action.
I have following routes defined:
CyberTrackRails3::Application.routes.draw do
scope "(:locale)", :locale => /en|de|nl/ do
resources :login do
get 'index', on: :collection
get 'check', on: :collection
end
end
end
Now, url_for(:controller => 'login', :action => 'check'), gives me the correct url, en/login/check.
Using login_check_path however doesn't work. How do I make this work?
I've tried replacing get 'index', on: :collection with match 'check' => 'check' but that doesn't work. Neither does match 'check' => 'login#check'.
since you're adding an action to a resource, i think the automatically generated name is going to be reversed, ie 'check_logins_path' (see adding-more-restful-actions)
do you need login_check_path specifically? if so, you should be able to define the path outside of the resources :login block, ie
match '/login/check' => 'login#check', :via => :get, :as => 'login_check'
and like Fivell suggested, rake routes will show you the automatically generated name for a route.
I'm trying to set up rails to use both the ID and the Handle (which is just an URL safe version of the title) of a blog post in the route.
match '/articles/:id/:handle', :to => 'articles#show'
resources :articles
This works, of course -- but I can't seem to set up the to_param method in the model os the longer URL -- with the handle attached, is the default.
This doesn't work (not that I really expected it to):
def to_param
"#{id}/#{handle}"
end
I get a No route matches {:action=>"edit", :controller=>"articles", error. I also tried just using the handle, but then Rails generates links to the resource just using the handle and not the ID. I know I can do it with a - in stead of a /, but I prefer the /. Any way to make this work? If I have to add some extra paremeters to my link_to helpers, that's okay.
Did you try to pass a Hash to link_to?
link_to "Link", {:id => #article.id, :handle => #article.handle}
Update
You have to modify your routes:
match '/articles/:id/:handle', :to => 'articles#show', :as => :article_with_handle
and use the following helper to generate the link:
link_to "Link", article_with_handle_path(:id => #article.id, :handle => #article.handle)
You can override the helper to simplify things:
def article_with_handle_path(article)
super(:id => article.id, :handle => article.handle)
end
and use it like this:
link_to "Link", article_with_handle_path(#article)
Okay, here's what I did to remove the query string problem from the answer above:
Changed the route to this:
match '/articles/:id/:handle' => 'articles#show', :as => :handle
Removed the to_param method from the model and then generated the link like this:
link_to 'Show', handle_path(:handle => article.handle, :id => article.id) %>
That works, but could be condensed, obviously, with the helper above. Just change the one line to: args[1] = handle_path(:id => args[1].id, :handle => args[1].handle)
I'm trying to do something similar to Railscasts #255 but I'm getting a No Route error:
In Ryan's routes.rb file:
post "versions/:id/revert" => "versions#revert", :as => "revert_version"
In in the controller where he uses the route, versions_controller.rb
link = view_context.link_to(link_name, revert_version_path(#version.next, :redo => !params[:redo]), :method => :post)
redirect_to :back, :notice => "Undid #{#version.event}. #{link}"
In my routes.rb
post "/approve/:id" => "listings#approve", :as => "listing_approve"
and view where I use my link:
<%= link_to 'Approve Content', listing_approve_path(#listing), :method => :post %>
My tests return to me a ActionController::RoutingError: No route matches [GET] "/approve/1"
If I leave the method as a GET everything works.. Using rails 3.1.0rc5. Any guidance as to what I'm doing wrong here would be very much appreciated..
EDIT: routes.rb file (the last line is set as match right now to work)
RLR::Application.routes.draw do
root :to => "home#index"
devise_for :users, :controllers => { :registrations => "registrations" }
devise_for :users
match '/user' => "layouts#index", :as => :user_root
resources :users, :only => :show
resources :layouts, :only => [:index, :show]
resources :listings
resources :features
resources :orders
match "/preview/:id" => "listings#preview", :as => "listing_preview", :via => "get"
match "/approve/:id" => "listings#approve", :as => "listing_approve"
end
Hmmmm, it looks right to my eye. The test sounds like it is generating a GET instead of a POST though, so it might be a problem with the link_to call. You've got :method => :post there, so it should be fine. http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to seems to indicate that link_to will generate some javascript to make a POST call on click (and that users with javascript disabled will get a normal GET link unless you use :href="#"), so it might be because your test engine isn't running the javascript.
You can fix this by changing it to a button that submits a hidden form, but that might not be the visual representation you want.
It might be a precedence thing - the first matching route definition in routes.rb is used, so if you have a resources route or something like that it may be matching on that first.
I got the same problem in my rails application and I solved it the same way you did by doing a via: :get on the match instead of a via: :post. I think for some reason when you send a request in the format of /something/:id it will automatically assume its a [GET] request and search for a get route. This of course will cause problems in your routes if you have it as a :POST.
If anyone has a better solution or idea as to why you cannot send a post request in the format '/something/:id' let me know please.
I have a strange problem that I think has to do with routes.
In my "view" I have this:
<%= button_to "New Item", new_proposal_pitem_path(#proposal), :method => :get %>
I want to click the "New Item" button, and create a new pitem for a proposal. This generates the HTML I would expect:
<form method="get" action="/proposals/1234/pitems/new" ...><input ...></form>
However, what really happens is, when I click on the button it attempts a GET on /proposals/1234 rather than /proposals/1234/pitems/new. This gives me a "show" page rather than a "new" page. Interestingly, I can manually put the {site}/proposals/1234/pitems/new directly into the web browser HTTP address and get what I want (the "new" page). But rails is, on its own, translating it first to /proposals/1234 if I leave it to its own in response to the button click.
To make this more mysterious, I have a similar item on the same form which looks exactly the same way:
<%= button_to "New Payment", new_proposal_payment_path(#proposal), :method => :get %>
which generates the same HTML as the other case:
<form method="get" action="/proposals/1234/payments/new" ...><input ...></form>
But this one works! It takes me right to /proposals/1234/payments/new when I click the button, just like I'd expect. I just don't understand what makes these behave differently.
My full routes file looks like this:
TCoB::Application.routes.draw do
resources :proposals do
resources :pitems, :payments
get 'list', :on => :collection
end
resources :pitems do
get 'list', :on => :collection
end
resources :invoices do
resources :iitems, :payments
get 'list', :on => :collection
end
resources :iitems do
get 'list', :on => :collection
end
resources :payments do
get 'list', :on => :collection
end
resources :ids
resources :clients do
resources :proposals, :invoices
# Route GET /cients/list
get 'list', :on => :collection
get 'list_proposals', :on => :collection
get 'list_invoices', :on => :collection
end
get "home/index"
root :to => "home#index"
end
Can someone shed light on this issue?
Thanks!
Small helper method that uses JavaScript:
def button_link_to(name, url)
"<button type=\"button\" onclick=\"window.location.href='#{url}';\">#{h(name)}</button>".html_safe
end
button_link_to "New Item", new_proposal_pitem_path(#proposal)