What does :new => {:start => :get} mean in routes?

Hey all,

I’m looking at this route below:

map.resources :sessions, :member => {:validate => :get}, :new => {:start
=> :get}, :only => %w(create new)

It’s clear the sessions has the 7 restful routes but each member of a
collection of records can also be influenced by the validate method.
Then I come across this line:

:new => {:start => :get}, :only => %w(create new)

and I’m not sure what this is doing.

The sessions controller has these two methods:

def new
redirect_to start_new_session_path and return if current_user
flash.now[:alert] = params[:error] if params[:error]
flash.now[:notice] = params[:notice] if params[:notice]
end

def start
end

Thanks for response.

I believe you only have two restful routes (create and new) because of
the line:

:only => %w(create new)

And the start action is defined as an alternative new action here,
probably because the original author wanted a GET instead of POST (/
sessions/start):

:new => {:start => :get}

Based on the SessionController it looks like you may have more routes
then you are actually using.

M Daubs wrote in post #995201:

I believe you only have two restful routes (create and new) because of
the line:

:only => %w(create new)

And the start action is defined as an alternative new action here,
probably because the original author wanted a GET instead of POST (/
sessions/start):

:new => {:start => :get}

Based on the SessionController it looks like you may have more routes
then you are actually using.

Thanks for response. I’m not understanding why someone may need to pass
an alternative new action, when both the new and start are declared as
methods in sessions controller. If the reason is because these are two
kinds of ‘new’ actions that have different purposes, then why does start
method silently return null since it doesn’t contain any functionality
within its scope? Maybe the answer isn’t possible given the context I
provided but in general terms, why might it be done?

Thanks for response.

Unless your routes file has a default :controller/:action route then
the start action would be unroutable without that entry. It could
even be redundant or unused.

The start method may return nil but the sessions/start view will still
render (if it exists).

Obviously

map.resources :sessions, :member => {:validate => :get}, :new => {:start =>
:get}, :only => %w(create new)
creates new route for you like the one you have in new method

redirect_to start_new_session_path and return if current_user

Thanks for response. I do notice this:

form_tag sessions_path

creates sessions/create

and so when the form is posted, create method of sessions controller is
executed.

One thing though. How does it know to post to create if it just says
sessions_path and not create_sessions_path? Since there is only a new
and create action, it just assumes that since a form is being posted,
you must want it to go to create?

Thanks for response.

Hi, John

Obviously

map.resources :sessions, :member => {:validate => :get}, :new => {:start =>
:get}, :only => %w(create new)
creates new route for you like the one you have in new method

redirect_to start_new_session_path and return if current_user

If you had

map.resources :sessions, :member => {:validate => :get}, :new => {:finish =>
:get, :brand => :get}, :only => %w(create new)
then the new routes would have been:

redirect_to finish_new_session_path and return if current_user
and

redirect_to brand_new_session_path and return if current_user

Makes sense?

On Apr 26, 11:57pm, John M. [email protected] wrote:

end

def start
end

Thanks for response.


Posted viahttp://www.ruby-forum.com/.


Sincerely,
Max R.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs