2

我在路线中有这条线:

map.resources :questions, :new => {:vote_for => :put, :vote_against => :put}, :has_many => :replies, :shallow => true

在我看来,我使用以下助手:

link_to 'OK', vote_for_question_path(@question), :method => :put
link_to 'NO', vote_against_question_path(@question), :method => :put

但不幸的是,正如 Rails 所说,我的代码有问题:

未定义的方法 `vote_for_question_path' 用于#

怎么了?

4

1 回答 1

2

It looks like your route syntax is wrong.

If you want to add new member routes (i.e. ones that apply to a single instance of a resource), then you should do:

map.resources :questions,
              :member => { :vote_for => :put, :vote_against => :put },
              :has_many => :replies, :shallow => true

On the other hand, if you want to override the standard "new" URL segment, then it would be:

map.resources :questions, :path_names => { :new => 'vote_for' },
              :has_many => :replies, :shallow => true

—Note that the corresponding controller action would still be named "new". This would allow URLs such as:

/questions/vote_for

However, looking at what you appear to be trying to do you might want to consider creating a new Vote resource. This would get created when a user votes for a question and would fit within the standard Rails' RESTful routing conventions. Voting on a question could then have a URL something like:

/questions/22/votes/new

于 2009-06-02T11:08:10.097 回答