开发者

Rails routing: how to mix "GET" and "PUT"

Not sure how to frame this question (I'm still wrapping my head around Rails).

Let's try this:

Say I wanted to implement the user side of Ryan Bates' excellent railscast on nested models. (He shows how to implement a survey where you can add and remove questions and answers dynamically). I want the user's side of this: to be able to answer questions and, not in the tutorial, be able to add comments.

It seems to me that you have to implement a view that shows the questions and answers, allow selection of the answers, and the input of comments. So there would need to be a way to show the information, but also update the model on input, right?

I know I'm not explaining this very well. I hope you understand what I'm getting at.

Is it 开发者_JAVA技巧just a question of setting up the right routes? Or is there some controller mojo that needs to happen?


The typical way to do this in Rails uses "resourceful" routing, which more or less naturally maps the standard CRUD actions to methods in your controller, using the appropriate HTTP verbs.

In the routes file (config/routes.rb), you set up the desired resources and actions. For example:

map.resources :questions, :has_many => :answers

Would set up a routing scheme for a question with multiple answers, mapping to the actions according to Rails' conventions:

index: GET /questions/1/answers # list of answers for question id=1
show: GET /questions/1/answers/2 # display answer 2
new: GET /questions/1/answers/new # render form for new answer for question id=1
create: POST /questions/1/answers # create a new answer for question id=1
edit: GET /questions/1/answers/2/edit # render form for answer for question id=1
update: PUT /questions/1/answers/2 # update answer 2
destroy: DELETE /questions/1/answers/2 # delete answer 2

In the controller you create methods mapping to these standard actions. You can also create your own methods and actions for things that don't fall into the CRUD paradigm (like a search for an AJAXified autocomplete field, for example)

Hope that answers some of your question.


You need a "question" resource, "answer" resource and "comment" resource. You also need to implement:

  • POST for "answer (which is "create" method in controller) to answer the question
  • POST for "comment" (which is "create" method in controller) to create comments
  • PUT for the "question" (which is "update" in controller) to "pick" answers, which is effectively changing the state of the "question" resource


In ASP.NET MVC there are two controller methods with the same name but different parameter signatures. One method is decorated with an attribute that tells it to service GETs, the other is decorated with an attribute that tells it to service POSTs. The GET method displays the view, the POST method updates the model.

I assume that it works in a similar fashion in Rails.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜