Dmitri
Dmitri

Reputation: 2457

Ruby on Rails custom routes

I'm confused with the REST routes:

I have a user signup form where user can register. I'm using form_for as a form builder -

<%= form_for @user do |f| %>

The thing I want to achive: path like users/signup would lead to Users controllers and Signup (which is pretty obvious) action, instead of users/new (GET). And I also wanted to be able to POST to the same method (intead of "users"(POST)) => so, basically, signup action for both POST and GET.

Also I wanted to know, if I need to use new_users_path instead of "@user" (so it would look like this:

<%= form_for new_users_path do |f| %>

in the *form_for* - because, when I use new_users_path, for some reason for the field's name I get: name="/users/new[username]" instead of name="user[username]"

Could someone help me with this one ?

Thanks in advance!

Upvotes: 0

Views: 581

Answers (2)

jdoe
jdoe

Reputation: 15771

By using:

  match 'users/signup', via: [:post, :get]
  resources :users, except: [:new, :create]

the rake routes command gives:

users_signup POST|GET /users/signup(.:format)   users#signup
       users GET      /users(.:format)          users#index
   edit_user GET      /users/:id/edit(.:format) users#edit
        user GET      /users/:id(.:format)      users#show
             PUT      /users/:id(.:format)      users#update
             DELETE   /users/:id(.:format)      users#destroy

Usage:

<%= form_for User.new, url: users_signup_path do |f| %>

I hope this is what you wanted.

Upvotes: 1

Siwei
Siwei

Reputation: 21569

you have to check its api:

form_for(record_or_name_or_array, *args, &proc)

the 1st parameter should be an object(.e.g "record"), but not a "route".

if you want it accept a url, check form_tag

come to download a searchable API .

Upvotes: 0

Related Questions