Rails routing question

Hi,

Can anyone help… I am trying to understand how 2 different routes
like show and update seem to be the same. Both are
/questions/:id(.:format).

GET /questions(.:format)
questions#index
POST /questions(.:format)
questions#create
GET /questions/new(.:format)
questions#new
GET /questions/:id/edit(.:format)
questions#edit
GET /questions/:id(.:format)
questions#show
PATCH /questions/:id(.:format)
questions#update
PUT /questions/:id(.:format)
questions#update
DELETE /questions/:id(.:format)
questions#destroy

When I try to redirect an action to update, it actually goes to the show
method and I can not figure out how to redirect to the update action.

redirect_to :action => :update, :id => next_question_id …instead
redirects to action “show”.

Thanks,

Dave

On Dec 6, 2014, at 2:45 PM, Dave C. [email protected]
wrote:

When I try to redirect an action to update, it actually goes to the show
method and I can not figure out how to redirect to the update action.

Redirecting to a post simply makes no sense. I suspect maybe you’re
confusing the action that displays an edit form with the action that’s
run when the form is submitted? That what you really want to do is
redirect to edit?


Scott R.
[email protected]
http://www.elevated-dev.com/
(303) 722-0567 voice

Walter D. wrote in post #1164174:

Thanks! I was trying to either make changes to a form and then review it with
the show action, or use another button to go directly to the next record and edit
it. I was thinking I had to go to update. fixed thanks to your replies.

Dave C.

On Dec 6, 2014, at 4:45 PM, Dave C. [email protected]
wrote:

Hi,

Can anyone help… I am trying to understand how 2 different routes
like show and update seem to be the same. Both are
/questions/:id(.:format).

GET /questions(.:format)
questions#index
POST /questions(.:format)
questions#create

But the VERB (part in all caps) is different. REST uses these different
action verbs to mean something specific, even if the apparent URL is the
same.

The HTTP specification describes a number of different verbs – GET,
POST, PATCH, PUT, and DELETE (probably more, these are the ones Rails
cares about). Browsers, on the other hand, don’t natively do anything
except GET and POST. Rails-generated forms send along a form variable
named _method with the other request types, so that when the Rails
router receives them, it can translate an ordinary POST into a PUT or
PATCH or DELETE as needed.

When you post to the /questions URL, you are creating a new question and
persisting it in the database (modulo any errors halting the save and
redirect).

When you GET from that same URL, you are requesting the index list of
all questions.

GET /questions/new(.:format)
questions#new

This one is a special snowflake – it requests a object that does not
exist yet, so you get an empty form to fill out and give that object its
parameter values.

GET /questions/:id/edit(.:format)
questions#edit
GET /questions/:id(.:format)
questions#show
PATCH /questions/:id(.:format)
questions#update
PUT /questions/:id(.:format)
questions#update
DELETE /questions/:id(.:format)
questions#destroy

It gets a little more subtle when you have saved your question. Now
there are four different things you can do with the same saved object
(show, edit, update, or delete) and two of those use the same URL and
multiple verbs to differentiate what happens. GET with the ID (or
another identifier) in the URL will show you the object. GET with the ID
and /edit in the URL gets you a form populated with the current state of
the object, ready to send back to the server for updating. Both PATCH
and PUT will allow you to update an existing object with new data.
DELETE does what it says.

When I try to redirect an action to update, it actually goes to the show
method and I can not figure out how to redirect to the update action.

You don’t ever want to redirect to the update method* – you should
redirect to methods that respond with HTML content, like :index or :show
or :new or :edit. The form you would see on an :edit view would know to
PATCH or PUT to the /questions/:id path, which would trigger the update
method in your controller. Your controller cannot (easily) make a PATCH
request to another method in the same or a different controller.

redirect_to :action => :update, :id => next_question_id …instead
redirects to action “show”.

If you can describe what you’re trying to do here, maybe someone can
suggest a solution that doesn’t step outside of the REST conventions.

Thanks,

Dave

You’re welcome,

Walter

*There’s a really serious reason for this. You don’t ever want a URL in
your site to alter data just because it was requested.