3

In my app I am working on allowing users to send invitations. The invites have tokens. And in the emails I am linking to a signup page with the token in the path. In the mailer's controller I am using:

new_user_registration_url(@invitation.token)

as I saw Ryan Bates do in this railscast. But it appears to be outputting this format:

http://localhost:3000/signup.4a4aebcde29738a39c7f447f58817e49cf9b4cf4

Why is there a "." instead of a "/"?

Update:

I'm using devise and here are the relevant routes. I am not to confident about these; I had struggled a little with it but these seemed to work:

  devise_scope :user do
    get '/signup/:invitation_token' => "registrations#new", :as => :new_user_registration
  end
  devise_for :users, :controllers => { :registrations => "registrations"}, :skip => [:registrations]
  as :user do
    get '/users/cancel' => 'devise/registrations#cancel', :as => :cancel_user_registration
    post '/users' => 'devise/registrations#create', :as => :user_registration
    get '/signup' => 'registrations#new', :as => :new_user_registration
    get '/users/edit' => 'devise/registrations#edit', :as => :edit_user_registration
    put '/users' => 'devise/registrations#update'
    delete '/users' => 'devise/registrations#destroy'
  end
John
  • 13,125
  • 14
  • 52
  • 73

2 Answers2

3

Wihout routes.rb it's hard to tell but it seems the author has something like:

#in routes.rb
get 'signup' => 'controller#action', as: :new_user_registration

but has to have:

get 'signup/:token' => 'controller#action', as: :new_user_registration

Checking:

# in console
app.new_user_registration_path('ToKeN') # => "/signup/ToKeN"
jdoe
  • 15,665
  • 2
  • 46
  • 48
  • See update. . I actually have both types of routes, perhaps that is my problem? I am using both so that I can get a regular signup and an invitation signup. – John Apr 23 '12 at 19:29
  • 1
    You have 2 paths named as `:new_user_registration`, see? Comment the second one and restart your app (routing changes sometimes are too tricky to be implemented on the fly). Tell us about the result. – jdoe Apr 23 '12 at 19:44
  • When I do this, the output of new_user_registration_url(@invitation.token) has a "/" as desired, but then my /signup route does not work, and more confusingly to me, neither does my users/sign_in route (get "No route matches {:controller=>"registrations", :action=>"new""}. Any idea why? – John Apr 23 '12 at 20:00
  • My bad, the users/sign_in route isn't broken but the link generated by "link_to "Sign up", new_registration_path(resource_name)" no longer works. I would like to get that working and also have both a regular signup path and one for invitation signup. How should I do this with Devise? May open new question. – John Apr 23 '12 at 20:39
  • Ok, changed the name of the invited route so it doesn't conflict with the regular sign up route and things are working fine. Thanks! – John Apr 23 '12 at 20:48
  • 1
    Glad to hear that you've solved your conflicts! For the future: 1) changing `routes.rb` (especially removing things) often requires restarting your app to reflect them; 2) if quick look at `routes.rb` file doesn't help -- use `rake routes` and redirect the output to a text file -- you'll be knowing about all the routes and all the path helpers. – jdoe Apr 24 '12 at 05:10
0

If you just pass a string or a symbol as an argument to a path helper, I think it gets interpreted as the format. Try something like the following:

new_user_registration_url(:token => @invitation.token)

or

new_user_registration_url + "/" + @invitation.token
tsherif
  • 11,502
  • 4
  • 29
  • 27
  • If I use the :token => @invitation.token structure, I get a url with a "?" instead of a "." which is better than where I was. What do you mean by "gets interpreted as the format?" – John Apr 23 '12 at 19:13
  • @John With standard RESTful routes in Rails, you can usually add `.xyz` at the end to tell Rails what format you want the response in. The default is `html`, but you can do something like `GET /users.xml` or `GET /users.js` to get your Users index in XML or JavaScript, respectively, as long as your controller action is set up to respond to the format. You can see these options as `(:format)` if you run `rake routes`. So I think when you pass a single string or symbol argument to your path helper, it interprets it as this `format` parameter. – tsherif Apr 23 '12 at 19:44